{"record":{"id":"667e48e2b0d968aa","repo":"donnemartin/interactive-coding-challenges","slug":"number-cannot-be-none","errorCode":null,"errorMessage":"number cannot be None","messagePattern":"number cannot be None","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"bit_manipulation/bit/bit_solution.ipynb","lineNumber":204,"sourceCode":"    \"    def validate_index_wrapper(self, *args, **kwargs):\\n\",\n    \"        for arg in args:\\n\",\n    \"            if arg < 0:\\n\",\n    \"                raise IndexError('Invalid index')\\n\",\n    \"        return func(self, *args, **kwargs)\\n\",\n    \"    return validate_index_wrapper\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 2,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"class Bit(object):\\n\",\n    \"\\n\",\n    \"    def __init__(self, number):\\n\",\n    \"        if number is None:\\n\",\n    \"            raise TypeError('number cannot be None')\\n\",\n    \"        self.number = number\\n\",\n    \"\\n\",\n    \"    @validate_index\\n\",\n    \"    def get_bit(self, index):\\n\",\n    \"        mask = 1 << index\\n\",\n    \"        return self.number & mask != 0\\n\",\n    \"\\n\",\n    \"    @validate_index\\n\",\n    \"    def set_bit(self, index):\\n\",\n    \"        mask = 1 << index\\n\",\n    \"        self.number |= mask\\n\",\n    \"        return self.number\\n\",\n    \"\\n\",\n    \"    @validate_index\\n\",\n    \"    def clear_bit(self, index):\\n\",\n    \"        mask = ~(1 << index)\\n\",\n    \"        self.number &= mask\\n\",\n    \"        return self.number\\n\",","sourceCodeStart":186,"sourceCodeEnd":222,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/bit_manipulation/bit/bit_solution.ipynb#L186-L222","documentation":"The Bit class constructor raises TypeError('number cannot be None') when instantiated without a valid integer. This is defensive input validation ensuring self.number always holds an integer before bit operations run.","triggerScenarios":"Bit(None), Bit(number) where number came from an uninitialized variable or a dict.get() that returned None, or forgetting the constructor argument entirely (which raises TypeError for the missing param instead).","commonSituations":"Loading values from JSON/config where a key is missing (dict.get returns None), passing an optional parameter straight through, or refactoring that changed the constructor signature.","solutions":["Pass an actual int, e.g. Bit(12)","Default the value: Bit(number or 0)","Validate upstream data source so number is never None"],"exampleFix":"# before\nb = Bit(data.get('flags'))\n\n# after\nflags = data.get('flags')\nif flags is None:\n    raise ValueError(\"missing 'flags' in data\")\nb = Bit(flags)","handlingStrategy":"type-guard","validationCode":"if number is None:\n    raise ValueError('number is required')\nbit = Bit(number)","typeGuard":"def is_int(v) -> bool:\n    return isinstance(v, int) and not isinstance(v, bool)","tryCatchPattern":"try:\n    bit = Bit(number)\nexcept TypeError as e:\n    if 'cannot be None' in str(e):\n        number = 0\n        bit = Bit(number)\n    else:\n        raise","preventionTips":["Avoid dict.get() without default for required constructor values","Initialize numeric variables to 0, not None","Type-check inputs at API boundaries"],"tags":["python","constructor","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"}