{"record":{"id":"ccdbf1f0993ae8c2","repo":"donnemartin/interactive-coding-challenges","slug":"num-cannot-be-none-ccdbf1","errorCode":null,"errorMessage":"num cannot be None","messagePattern":"num cannot be None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"math_probability/check_prime/check_prime_solution.ipynb","lineNumber":97,"sourceCode":"   \"metadata\": {},\n   \"source\": [\n    \"## Code\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 1,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"import math\\n\",\n    \"\\n\",\n    \"\\n\",\n    \"class Math(object):\\n\",\n    \"\\n\",\n    \"    def check_prime(self, num):\\n\",\n    \"        if num is None:\\n\",\n    \"            raise TypeError('num cannot be None')\\n\",\n    \"        if num < 2:\\n\",\n    \"            return False\\n\",\n    \"        for i in range(2, num):\\n\",\n    \"            if num % i == 0:\\n\",\n    \"                return False\\n\",\n    \"        return True\\n\",\n    \"\\n\",\n    \"    def check_prime_optimized(self, num):\\n\",\n    \"        if num is None:\\n\",\n    \"            raise TypeError('num cannot be None')\\n\",\n    \"        if num < 2:\\n\",\n    \"            return False\\n\",\n    \"        for i in range(2, int(math.sqrt(num)+1)):\\n\",\n    \"            if num % i == 0:\\n\",\n    \"                return False\\n\",\n    \"        return True\"\n   ]\n  },","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/math_probability/check_prime/check_prime_solution.ipynb#L79-L115","documentation":"Raised by Math.check_prime when num is None. The primality loop compares num < 2 and computes num % i, which would crash on None; the guard converts that into a clear, intentional TypeError at the API boundary.","triggerScenarios":"Calling Math().check_prime(None), typically from an uninitialized variable, an optional argument, or a value fetched with dict.get()/list.pop() from an empty collection.","commonSituations":"Unit tests covering the None edge case; pipelines where the input number comes from an external source (file, API) that can yield None.","solutions":["Pass an integer: check_prime(29)","Guard the call site: if num is not None: math.check_prime(num)","Trace and fix the producer of the None value"],"exampleFix":"# before\nmath.check_prime(numbers.get(key))  # get returns None if missing\n\n# after\nvalue = numbers.get(key)\nif value is None:\n    raise KeyError(key)\nmath.check_prime(value)","handlingStrategy":"type-guard","validationCode":"if num is None: raise ValueError('num is required')\nmath.check_prime(num)","typeGuard":"def is_int(n): return isinstance(n, int)","tryCatchPattern":"try:\n    math.check_prime(num)\nexcept TypeError as e:\n    # handle missing input\n    pass","preventionTips":["Avoid dict.get() defaults of None flowing into math APIs","Assert types at integration boundaries"],"tags":["python","input-validation","none-check","primes"],"backgroundTag":"none-argument-validation","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}