{"record":{"id":"0def4d80292464e8","repo":"521xueweihan/HelloGitHub","slug":"input-error-must-be-number","errorCode":null,"errorMessage":"Input error: Must be number","messagePattern":"Input error: Must be number","errorType":"exception","errorClass":"InputError","httpStatus":null,"severity":"error","filePath":"script/make_content/make_content.py","lineNumber":91,"sourceCode":"    for fi_dir in dir_list:\n        # 忽略‘script’的目录\n        if os.path.isdir(fi_dir) and 'script' not in fi_dir:\n            make_content(fi_dir)\n\n\ndef main():\n    \"\"\"\n    入口方法\n    \"\"\"\n    input_list = sys.argv  # 获取输入的参数\n\n    if len(input_list) != 2:\n        raise InputError('Input error: Need a param')\n    else:\n        try:\n            input_arg = input_list[1]\n        except Exception:\n            raise InputError('Input error: Must be number')\n    if len(input_arg) == 1:\n        make_content('0' + input_arg)\n    elif input_arg == 'all':\n        make_all_content()\n    else:\n        make_content(input_arg)\n\nif __name__ == '__main__':\n    main()\n","sourceCodeStart":73,"sourceCodeEnd":101,"githubUrl":"https://github.com/521xueweihan/HelloGitHub/blob/1bbd16c33ee26440f1316ffb0285c603fa541497/script/make_content/make_content.py#L73-L101","documentation":"This InputError('Input error: Must be number') is raised in main() inside a try/except around `input_arg = input_list[1]` (script/make_content/make_content.py:88-91). Because the preceding check already guarantees len(sys.argv) == 2, indexing sys.argv[1] can never fail, so this branch is effectively dead code: the message suggests a non-numeric argument was rejected, but no numeric validation actually exists anywhere in main().","triggerScenarios":"Not reachable in the current code: the except only fires if input_list[1] raised, which cannot happen once len(input_list) == 2 is confirmed. It would only fire after a refactor that removes or weakens the length check while keeping the try/except. Passing a non-numeric argument (e.g. 'abc') today does NOT raise it — it falls through to make_content('abc') and fails later on missing paths.","commonSituations":"Developers see this string in the source and assume non-numeric input is validated; refactors that reorder the argv checks; copies of this script where the length guard was deleted, making the except suddenly live; debugging why 'abc' does not produce this error.","solutions":["If you intend to validate that the argument is a number, replace the useless try/except with an explicit check: `if not (input_arg == 'all' or input_arg.isdigit()): raise InputError('Input error: Must be number')`.","If refactoring main(), keep the strict `len(sys.argv) != 2` guard before any indexing so this dead branch stays dead or can be removed.","Delete the try/except entirely (indexing is provably safe) and rely on the length check plus the isdigit check from step 1."],"exampleFix":"# before (dead code: sys.argv[1] cannot raise after the length check)\ntry:\n    input_arg = input_list[1]\nexcept Exception:\n    raise InputError('Input error: Must be number')\n\n# after (actually validates the value)\ninput_arg = input_list[1]\nif input_arg != 'all' and not input_arg.isdigit():\n    raise InputError('Input error: Must be number')","handlingStrategy":"validation","validationCode":"import sys\n\narg = sys.argv[1] if len(sys.argv) == 2 else None\nif arg is not None and arg != 'all' and not arg.isdigit():\n    sys.stderr.write('Input error: Must be number\\n')\n    sys.exit(1)","typeGuard":"def is_valid_period_arg(arg):\n    \"\"\"True for 'all' or a digit-only period number like '05'.\"\"\"\n    return arg == 'all' or (isinstance(arg, str) and arg.isdigit())","tryCatchPattern":"from make_content import InputError, main\n\ntry:\n    main()\nexcept InputError as e:\n    sys.stderr.write(str(e.message) + '\\n')\n    sys.exit(2)","preventionTips":["Validate the argument value with str.isdigit() (or accept 'all') before calling into the script's logic.","Do not rely on the existing try/except around sys.argv[1] — it is dead code and performs no numeric validation.","Keep the argv length check ahead of any indexing so the failure mode stays a clear usage error."],"tags":["cli","dead-code","argument-validation","python"],"backgroundTag":null,"analyzedSha":"1bbd16c33ee26440f1316ffb0285c603fa541497","analyzedAt":"2026-08-14T15:59:01.911Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}