{"record":{"id":"6c194b722267ce00","repo":"donnemartin/interactive-coding-challenges","slug":"max-num-cannot-be-none","errorCode":null,"errorMessage":"max_num cannot be None","messagePattern":"max_num cannot be None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"math_probability/generate_primes/check_prime_solution.ipynb","lineNumber":101,"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 PrimeGenerator(object):\\n\",\n    \"\\n\",\n    \"    def generate_primes(self, max_num):\\n\",\n    \"        if max_num is None:\\n\",\n    \"            raise TypeError('max_num cannot be None')\\n\",\n    \"        array = [True] * max_num\\n\",\n    \"        array[0] = False\\n\",\n    \"        array[1] = False\\n\",\n    \"        prime = 2\\n\",\n    \"        while prime <= math.sqrt(max_num):\\n\",\n    \"            self._cross_off(array, prime)\\n\",\n    \"            prime = self._next_prime(array, prime)\\n\",\n    \"        return array\\n\",\n    \"\\n\",\n    \"    def _cross_off(self, array, prime):\\n\",\n    \"        for index in range(prime*prime, len(array), prime):\\n\",\n    \"            # Start with prime*prime because if we have a k*prime\\n\",\n    \"            # where k < prime, this value would have already been\\n\",\n    \"            # previously crossed off\\n\",\n    \"            array[index] = False\\n\",\n    \"\\n\",\n    \"    def _next_prime(self, array, prime):\\n\",\n    \"        next = prime + 1\\n\",","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/math_probability/generate_primes/check_prime_solution.ipynb#L83-L119","documentation":"Raised by PrimeGenerator.generate_primes (Sieve of Eratosthenes) when max_num is None. The sieve immediately allocates [True] * max_num, which would raise a confusing TypeError on None; the guard makes the contract explicit: an integer upper bound is required.","triggerScenarios":"Calling PrimeGenerator().generate_primes(None), e.g. passing a limit read from argv/config that was never set, or int(os.environ.get('LIMIT')) where the env var is missing.","commonSituations":"CLI scripts and notebooks where the limit is optional and defaults to None; parameterized tests.","solutions":["Pass an integer upper bound, e.g. generate_primes(100)","Default the parameter at the call site: generate_primes(limit or 100)","Validate config/env-derived limits before calling"],"exampleFix":"# before\ngen.generate_primes(int(config.get('limit')))  # get returns None -> int(None) crashes earlier; direct pass-through raises here\n\n# after\nlimit = config.get('limit')\nif limit is None:\n    limit = 100\ngen.generate_primes(int(limit))","handlingStrategy":"validation","validationCode":"max_num = max_num if max_num is not None else DEFAULT_LIMIT\ngen.generate_primes(max_num)","typeGuard":"def is_positive_int(x): return isinstance(x, int) and x >= 0","tryCatchPattern":"try:\n    gen.generate_primes(limit)\nexcept TypeError:\n    gen.generate_primes(DEFAULT_LIMIT)","preventionTips":["Give limit parameters explicit defaults","Validate env/config-derived limits with int() and fallback"],"tags":["python","input-validation","none-check","primes","sieve"],"backgroundTag":"none-argument-validation","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}