{"record":{"id":"b85a5050965f09e6","repo":"TheAlgorithms/Python","slug":"useful-years-must-be-an-integer","errorCode":null,"errorMessage":"Useful years must be an integer","messagePattern":"Useful years must be an integer","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"financial/straight_line_depreciation.py","lineNumber":56,"sourceCode":"    Calculate the depreciation expenses over the given period\n    :param useful_years: Number of years the asset will be used\n    :param purchase_value: Purchase expenditure for the asset\n    :param residual_value: Residual value of the asset at the end of its useful life\n    :return: A list of annual depreciation expenses over the asset's useful life\n    >>> straight_line_depreciation(10, 1100.0, 100.0)\n    [100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0]\n    >>> straight_line_depreciation(6, 1250.0, 50.0)\n    [200.0, 200.0, 200.0, 200.0, 200.0, 200.0]\n    >>> straight_line_depreciation(4, 1001.0)\n    [250.25, 250.25, 250.25, 250.25]\n    >>> straight_line_depreciation(11, 380.0, 50.0)\n    [30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0]\n    >>> straight_line_depreciation(1, 4985, 100)\n    [4885.0]\n    \"\"\"\n\n    if not isinstance(useful_years, int):\n        raise TypeError(\"Useful years must be an integer\")\n\n    if useful_years < 1:\n        raise ValueError(\"Useful years cannot be less than 1\")\n\n    if not isinstance(purchase_value, (float, int)):\n        raise TypeError(\"Purchase value must be numeric\")\n\n    if not isinstance(residual_value, (float, int)):\n        raise TypeError(\"Residual value must be numeric\")\n\n    if purchase_value < 0.0:\n        raise ValueError(\"Purchase value cannot be less than zero\")\n\n    if purchase_value < residual_value:\n        raise ValueError(\"Purchase value cannot be less than residual value\")\n\n    # Calculate annual depreciation expense\n    depreciable_cost = purchase_value - residual_value","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/financial/straight_line_depreciation.py#L38-L74","documentation":"Raised by straight_line_depreciation() in financial/straight_line_depreciation.py when useful_years is not an int (TypeError, not ValueError). The function loops range(useful_years), which requires an int, so a string or float is rejected up front. Note: True/False are ints in Python and pass this check — a latent quirk.","triggerScenarios":"Calling straight_line_depreciation(5.0, 1250.0, 50.0) (float years), straight_line_depreciation('5', ...) (string from CLI/JSON), or passing a Decimal. Booleans pass because bool subclasses int.","commonSituations":"Unparsed CLI arguments (always strings), JSON/YAML config where 5 is written as 5.0, or ORM/API fields typed as float. This is the first of six ordered checks, so it fires before any value checks.","solutions":["Coerce to int before calling: int(useful_years) after confirming no precision is lost.","Fix the source: parse CLI args with int(sys.argv[1]); tighten JSON schemas to integer type.","If fractional years are a real requirement, this function cannot model them — scale periods yourself."],"exampleFix":"# before\nstraight_line_depreciation(float(sys.argv[1]), 1250.0, 50.0)\n\n# after\nstraight_line_depreciation(int(sys.argv[1]), 1250.0, 50.0)","handlingStrategy":"type-guard","validationCode":"if not isinstance(useful_years, int) or isinstance(useful_years, bool):\n    raise TypeError('useful_years must be int')\nstraight_line_depreciation(useful_years, purchase, residual)","typeGuard":"def is_int_years(v: object) -> bool:\n    return isinstance(v, int) and not isinstance(v, bool)","tryCatchPattern":"try:\n    sched = straight_line_depreciation(y, p, r)\nexcept TypeError as exc:\n    if 'Useful years' in str(exc):\n        sched = straight_line_depreciation(int(y), p, r)\n    else:\n        raise","preventionTips":["Cast at the boundary: CLI/JSON values never reach business logic unparsed.","Remember all six checks raise TypeError for type errors and ValueError for range errors — catch accordingly."],"tags":["finance","type-checking","typeerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}