{"record":{"id":"aaaaf403cfd11834","repo":"donnemartin/interactive-coding-challenges","slug":"num-cannot-be-none-aaaaf4","errorCode":null,"errorMessage":"num cannot be None","messagePattern":"num cannot be None","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"bit_manipulation/pairwise_swap/pairwise_swap_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 pairwise_swap(self, num):\\n\",\n    \"        if num is None:\\n\",\n    \"            raise TypeError('num cannot be None')\\n\",\n    \"        if num == 0 or num == 1:\\n\",\n    \"            return num\\n\",\n    \"        odd = (num & int('1010101010101010', base=2)) >> 1\\n\",\n    \"        even = (num & int('0101010101010101', base=2)) << 1\\n\",\n    \"        return odd | even\"\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,\n   \"metadata\": {},","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/bit_manipulation/pairwise_swap/pairwise_swap_solution.ipynb#L100-L136","documentation":"Bits.pairwise_swap raises TypeError('num cannot be None') when num is None. The method swaps even and odd bit pairs using fixed 0xAAAA/0x5555 masks and requires an integer operand.","triggerScenarios":"Bits().pairwise_swap(None), or passing an optional parameter, a None-returning lookup, or an uninitialized variable.","commonSituations":"Optional numeric fields flowing into the call; refactor that changed a required parameter to optional; test fixtures missing a value.","solutions":["Pass an int: pairwise_swap(0b1010)","Default safely: pairwise_swap(num or 0)","Fix the upstream source of the None value"],"exampleFix":"# before\nswapped = bits.pairwise_swap(data.get('value'))\n\n# after\nvalue = data.get('value')\nif value is None:\n    raise ValueError(\"'value' required\")\nswapped = bits.pairwise_swap(value)","handlingStrategy":"validation","validationCode":"if num is None:\n    raise ValueError('num is required')\nbits.pairwise_swap(num)","typeGuard":"def is_int(v) -> bool:\n    return isinstance(v, int) and not isinstance(v, bool)","tryCatchPattern":"try:\n    swapped = bits.pairwise_swap(num)\nexcept TypeError:\n    swapped = None","preventionTips":["Default numeric params to 0 rather than None","Check lookups for None before bit operations","Keep test fixtures fully populated"],"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"}