{"record":{"id":"b0239e0d1315f8a7","repo":"donnemartin/interactive-coding-challenges","slug":"nums-or-target-cannot-be-none","errorCode":null,"errorMessage":"nums or target cannot be None","messagePattern":"nums or target cannot be None","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"arrays_strings/two_sum/two_sum_solution.ipynb","lineNumber":166,"sourceCode":"  },\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"## Code\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 1,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"class Solution(object):\\n\",\n    \"\\n\",\n    \"    def two_sum(self, nums, target):\\n\",\n    \"        if nums is None or target is None:\\n\",\n    \"            raise TypeError('nums or target cannot be None')\\n\",\n    \"        if not nums:\\n\",\n    \"            raise ValueError('nums cannot be empty')\\n\",\n    \"        cache = {}\\n\",\n    \"        for index, num in enumerate(nums):\\n\",\n    \"            cache_target = target - num\\n\",\n    \"            if num in cache:\\n\",\n    \"                return [cache[num], index]\\n\",\n    \"            else:\\n\",\n    \"                cache[cache_target] = index\\n\",\n    \"        return None\"\n   ]\n  },\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"## Unit Test\"\n   ]","sourceCodeStart":148,"sourceCodeEnd":184,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/arrays_strings/two_sum/two_sum_solution.ipynb#L148-L184","documentation":"Raised by Solution.two_sum when nums or target is None. The method caches complements while scanning nums, so both must be valid; None is rejected with TypeError before any logic runs. Note the separate ValueError for empty nums lists.","triggerScenarios":"Calling two_sum(None, 9) or two_sum([2,7], None); forwarding unset request parameters.","commonSituations":"API handlers passing optional nums/target params unvalidated; data pipelines where the array or target comes from an optional field.","solutions":["Validate both parameters at the call boundary before invoking two_sum","Return an explicit error/empty result to the caller when either is missing","Fix the producer so target is always computed (e.g. not data.get('target'))"],"exampleFix":"# before\nSolution().two_sum(nums, params.get('target'))\n\n# after\ntarget = params.get('target')\nif nums is not None and target is not None:\n    result = Solution().two_sum(nums, target)","handlingStrategy":"validation","validationCode":"if nums is not None and target is not None:\n    Solution().two_sum(nums, target)","typeGuard":"def valid_two_sum_input(nums, target) -> bool:\n    return nums is not None and target is not None","tryCatchPattern":"try:\n    Solution().two_sum(nums, target)\nexcept TypeError:\n    return []  # or surface a validation error","preventionTips":["Validate API params before algorithm calls","Distinguish TypeError (None input) from ValueError (empty list) in handlers"],"tags":["two-sum","none-check","typeerror","validation"],"backgroundTag":"none-argument-rejected","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}