{"record":{"id":"727c9dd885d8a2ac","repo":"donnemartin/interactive-coding-challenges","slug":"argument-cannot-be-none","errorCode":null,"errorMessage":"Argument cannot be None","messagePattern":"Argument cannot be None","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"bit_manipulation/insert_m_into_n/insert_m_into_n_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 insert_m_into_n(self, m, n, i, j):\\n\",\n    \"        if None in (m, n, i, j):\\n\",\n    \"            raise TypeError('Argument cannot be None')\\n\",\n    \"        if i < 0 or j < 0:\\n\",\n    \"            raise ValueError('Index cannot be negative')\\n\",\n    \"        left_mask = -1 << (j + 1)\\n\",\n    \"        right_mask = (1 << i) - 1\\n\",\n    \"        n_mask = left_mask | right_mask\\n\",\n    \"        # Clear bits from j to i, inclusive\\n\",\n    \"        n_cleared = n & n_mask\\n\",\n    \"        # Shift m into place before inserting it into n\\n\",\n    \"        m_mask = m << i\\n\",\n    \"        return n_cleared | m_mask\"\n   ]\n  },\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"## Unit Test\"\n   ]","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/bit_manipulation/insert_m_into_n/insert_m_into_n_solution.ipynb#L100-L136","documentation":"Bits.insert_m_into_n raises TypeError('Argument cannot be None') when any of m, n, i, j is None. The routine builds masks and shifts m into a cleared window of n, so all four values must be present integers.","triggerScenarios":"insert_m_into_n(None, 15, 2, 4), or calling with values unpacked from a tuple/dict that had fewer entries; optional parameters left as None.","commonSituations":"Parsing 'insert m into n between bits i and j' specs from config where a field is missing; parameter defaults introduced by refactoring.","solutions":["Supply all four values explicitly, e.g. insert_m_into_n(0b10, 0b1000, 2, 4)","Validate the payload/spec object for missing fields before calling","Use dict.get with required-check or raise your own clearer error upstream"],"exampleFix":"# before\nbits.insert_m_into_n(spec.get('m'), spec.get('n'), spec.get('i'), spec.get('j'))\n\n# after\ntry:\n    m, n, i, j = spec['m'], spec['n'], spec['i'], spec['j']\nexcept KeyError as e:\n    raise ValueError(f'missing field {e}')\nbits.insert_m_into_n(m, n, i, j)","handlingStrategy":"type-guard","validationCode":"if None in (m, n, i, j):\n    raise ValueError('m, n, i, j are all required')\nbits.insert_m_into_n(m, n, i, j)","typeGuard":"def all_present(*vals) -> bool:\n    return all(v is not None for v in vals)","tryCatchPattern":"try:\n    result = bits.insert_m_into_n(m, n, i, j)\nexcept TypeError as e:\n    if 'cannot be None' in str(e):\n        raise ValueError('incomplete insert spec') from e\n    raise","preventionTips":["Validate spec dicts for required keys before unpacking","Avoid *args splats from possibly-short sequences","Add required-field checks where you parse bit-window specs"],"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"}