{"record":{"id":"ecd64aca14323244","repo":"donnemartin/interactive-coding-challenges","slug":"num-cannot-be-none-ecd64a","errorCode":null,"errorMessage":"num cannot be None","messagePattern":"num cannot be None","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"bit_manipulation/get_next/get_next_solution.ipynb","lineNumber":118,"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 Bits(object):\\n\",\n    \"\\n\",\n    \"    def get_next_largest(self, num):\\n\",\n    \"        if num is None:\\n\",\n    \"            raise TypeError('num cannot be None')\\n\",\n    \"        if num <= 0:\\n\",\n    \"            raise ValueError('num cannot be 0 or negative')\\n\",\n    \"        num_ones = 0\\n\",\n    \"        num_zeroes = 0\\n\",\n    \"        num_copy = num\\n\",\n    \"        # We'll look for index, which is the right-most non-trailing zero\\n\",\n    \"        # Count number of zeroes to the right of index\\n\",\n    \"        while num_copy != 0 and num_copy & 1 == 0:\\n\",\n    \"            num_zeroes += 1\\n\",\n    \"            num_copy >>= 1\\n\",\n    \"        # Count number of ones to the right of index\\n\",\n    \"        while num_copy != 0 and num_copy & 1 == 1:\\n\",\n    \"            num_ones += 1\\n\",\n    \"            num_copy >>= 1\\n\",\n    \"        # Determine index and set the bit\\n\",\n    \"        index = num_zeroes + num_ones\\n\",\n    \"        num |= 1 << index\\n\",\n    \"        # Clear all bits to the right of index\\n\",","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/bit_manipulation/get_next/get_next_solution.ipynb#L100-L136","documentation":"Bits.get_next_largest raises TypeError('num cannot be None') when num is None. It computes the next larger integer with the same number of 1 bits, and the bit-counting loops require an actual integer.","triggerScenarios":"Bits().get_next_largest(None), or passing a value from dict.get(), an optional parameter default of None, or an uninitialized variable.","commonSituations":"Feeding optional config numbers directly into the API; a refactor that made the parameter optional without adding a default at call sites.","solutions":["Supply a positive int, e.g. get_next_largest(6)","Use a safe default: get_next_largest(num if num is not None else 1)","Guard the data source so num is always set"],"exampleFix":"# before\nnxt = bits.get_next_largest(data.get('value'))\n\n# after\nval = data.get('value')\nif val is None:\n    raise ValueError(\"'value' missing\")\nnxt = bits.get_next_largest(val)","handlingStrategy":"validation","validationCode":"if num is None:\n    raise ValueError('num is required')\nbits.get_next_largest(num)","typeGuard":"def is_positive_int(v) -> bool:\n    return isinstance(v, int) and not isinstance(v, bool) and v > 0","tryCatchPattern":"try:\n    nxt = bits.get_next_largest(num)\nexcept TypeError:\n    nxt = None","preventionTips":["Require the field at parse time (raise on missing key)","Avoid None defaults for numeric algorithm inputs","Unit-test boundary values 0 and None at call sites"],"tags":["python","bit-manipulation","type-error","none-check"],"backgroundTag":"none-argument-type-error","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}