{"record":{"id":"652e4212c014de07","repo":"TheAlgorithms/Python","slug":"parameter-n-must-be-int-or-castable-to-int-652e42","errorCode":null,"errorMessage":"Parameter n must be int or castable to int.","messagePattern":"Parameter n must be int or castable to int\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"critical","filePath":"project_euler/problem_003/sol2.py","lineNumber":48,"sourceCode":"    ValueError: Parameter n must be greater than or equal to one.\n    >>> solution(-17)\n    Traceback (most recent call last):\n        ...\n    ValueError: Parameter n must be greater than or equal to one.\n    >>> solution([])\n    Traceback (most recent call last):\n        ...\n    TypeError: Parameter n must be int or castable to int.\n    >>> solution(\"asd\")\n    Traceback (most recent call last):\n        ...\n    TypeError: Parameter n must be int or castable to int.\n    \"\"\"\n\n    try:\n        n = int(n)\n    except TypeError, ValueError:\n        raise TypeError(\"Parameter n must be int or castable to int.\")\n    if n <= 0:\n        raise ValueError(\"Parameter n must be greater than or equal to one.\")\n    prime = 1\n    i = 2\n    while i * i <= n:\n        while n % i == 0:\n            prime = i\n            n //= i\n        i += 1\n    if n > 1:\n        prime = n\n    return int(prime)\n\n\nif __name__ == \"__main__\":\n    print(f\"{solution() = }\")\n","sourceCodeStart":30,"sourceCodeEnd":65,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/project_euler/problem_003/sol2.py#L30-L65","documentation":"Documented TypeError from project_euler/problem_003/sol2.py:solution when int(n) fails, e.g. solution('asd'). CAVEAT: this file cannot even be parsed by Python 3 - its 'except TypeError, ValueError:' is Python 2 tuple-less syntax, producing SyntaxError at import (verified with ast.parse), so the documented TypeError is dead code until fixed.","triggerScenarios":"After the syntax fix: solution('asd'), solution(None), or any non-numeric object. As written today: python3 project_euler/problem_003/sol2.py (or any import of it) exits with SyntaxError before main() runs.","commonSituations":"Automated test/doctest runners that walk the whole repo under Python 3; porting the algorithms repository to py3; copying the validation idiom into new code without noticing the py2 syntax.","solutions":["Change 'except TypeError, ValueError:' to 'except (TypeError, ValueError):' at project_euler/problem_003/sol2.py:48.","Then pass int or int-castable n (e.g. 600851475143).","Pre-validate with isinstance(n, (int, float)) or str(n).strip().lstrip('-').isdigit() in callers.","Run python -m py_compile (or ast.parse) over legacy modules in CI to surface py2 syntax early."],"exampleFix":"# before\nexcept TypeError, ValueError:\n    raise TypeError('Parameter n must be int or castable to int.')\n\n# after\nexcept (TypeError, ValueError):\n    raise TypeError('Parameter n must be int or castable to int.') from None","handlingStrategy":"type-guard","validationCode":"if not isinstance(n, (int, str)):\n    raise TypeError(f'unsupported n type: {type(n).__name__}')\nsolution(n)","typeGuard":"def is_int_castable(v) -> bool:\n    try:\n        int(v)\n        return True\n    except (TypeError, ValueError):\n        return False","tryCatchPattern":"try:\n    solution(n)\nexcept TypeError:\n    ...","preventionTips":["Fix 'except TypeError, ValueError:' at problem_003/sol2.py:48 - module currently SyntaxErrors under py3.","Do not pass None or lists as n.","Add py_compile checks for the repo's euler directory."],"tags":["python-euler","python2-syntax","typeerror","validation","prime-factorization"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}