{"record":{"id":"5c2710502c962b4b","repo":"donnemartin/interactive-coding-challenges","slug":"a-or-b-cannot-be-none","errorCode":null,"errorMessage":"a or b cannot be None","messagePattern":"a or b cannot be None","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"bit_manipulation/bits_to_flip/bits_to_flip_solution.ipynb","lineNumber":101,"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 bits_to_flip(self, a, b):\\n\",\n    \"        if a is None or b is None:\\n\",\n    \"            raise TypeError('a or b cannot be None')\\n\",\n    \"        count = 0\\n\",\n    \"        c = a ^ b\\n\",\n    \"        while c:\\n\",\n    \"            count += c & 1\\n\",\n    \"            c >>= 1\\n\",\n    \"        return count\"\n   ]\n  },\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"## Unit Test\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 2,","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/bit_manipulation/bits_to_flip/bits_to_flip_solution.ipynb#L83-L119","documentation":"Bits.bits_to_flip raises TypeError when either integer a or b is None. The method counts differing bits (Hamming distance) via a ^ b, which would fail on None, so inputs are validated up front.","triggerScenarios":"Bits().bits_to_flip(None, 5), bits_to_flip(a, None), or calling with values obtained from optional fields, optional function params, or dict lookups that returned None.","commonSituations":"Comparing two config versions or flags where one side is missing a key; optional numeric parameters flowing straight into the comparison.","solutions":["Ensure both arguments are ints before calling; supply defaults (a or 0)","Add an upstream null check on the data producing a and b","Catch TypeError if None is a legitimate sentinel you handle elsewhere"],"exampleFix":"# before\nflips = bits.bits_to_flip(old_flags, new_flags)\n\n# after\nif old_flags is None or new_flags is None:\n    raise ValueError('both flag values are required')\nflips = bits.bits_to_flip(old_flags, new_flags)","handlingStrategy":"validation","validationCode":"if a is None or b is None:\n    raise ValueError('both a and b are required')\nBits().bits_to_flip(a, b)","typeGuard":"def both_ints(*vals) -> bool:\n    return all(isinstance(v, int) and not isinstance(v, bool) for v in vals)","tryCatchPattern":"try:\n    count = bits.bits_to_flip(a, b)\nexcept TypeError:\n    count = None  # one side missing; skip comparison","preventionTips":["Use data['key'] instead of data.get('key') for required fields","Give optional numeric params concrete int defaults","Validate payloads before comparison logic"],"tags":["python","hamming-distance","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"}