{"record":{"id":"4adfd9cd30ec92b1","repo":"donnemartin/interactive-coding-challenges","slug":"source-or-dest-cannot-be-none","errorCode":null,"errorMessage":"source or dest cannot be None","messagePattern":"source or dest cannot be None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sorting_searching/merge_into/merge_into_solution.ipynb","lineNumber":159,"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 Array(object):\\n\",\n    \"\\n\",\n    \"    def merge_into(self, source, dest, source_end_index, dest_end_index):\\n\",\n    \"        if source is None or dest is None:\\n\",\n    \"            raise TypeError('source or dest cannot be None')\\n\",\n    \"        if source_end_index < 0 or dest_end_index < 0:\\n\",\n    \"            raise ValueError('end indices must be >= 0')\\n\",\n    \"        if not source:\\n\",\n    \"            return dest\\n\",\n    \"        if not dest:\\n\",\n    \"            return source\\n\",\n    \"        source_index = source_end_index - 1\\n\",\n    \"        dest_index = dest_end_index - 1\\n\",\n    \"        insert_index = source_end_index + dest_end_index - 1\\n\",\n    \"        while dest_index >= 0:\\n\",\n    \"            if source[source_index] > dest[dest_index]:\\n\",\n    \"                source[insert_index] = source[source_index]\\n\",\n    \"                source_index -= 1\\n\",\n    \"            else:\\n\",\n    \"                source[insert_index] = dest[dest_index]\\n\",\n    \"                dest_index -= 1\\n\",\n    \"            insert_index -= 1\\n\",\n    \"        return source\"","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/sorting_searching/merge_into/merge_into_solution.ipynb#L141-L177","documentation":"Raised by Array.merge_into when source or dest is None. The method walks both arrays backwards from the given end indices, so both must be real lists; None is rejected first with a TypeError, before the index validation on the following line.","triggerScenarios":"Calling merge_into(None, dest, i, j) or merge_into(source, None, i, j). Empty source or dest are handled by early returns; only None raises.","commonSituations":"Merging buffers where one array failed to allocate or was never populated; merging results from optional data sources; refactoring that leaves a buffer uninitialized.","solutions":["Initialize both buffers as lists before calling (e.g. dest = [None] * capacity)","Guard the call: proceed only when both source and dest are non-None","Ensure upstream allocation code cannot return None"],"exampleFix":"// before\narr.merge_into(src, None, len(src), total)\n// after\ndest = [None] * (len(src) + extra)\narr.merge_into(src, dest, len(src), len(dest))","handlingStrategy":"type-guard","validationCode":"if source is None or dest is None:\n    raise ValueError('source and dest buffers required')\narr.merge_into(source, dest, len(source), len(dest))","typeGuard":"def valid_buffers(a, b):\n    return isinstance(a, list) and isinstance(b, list)","tryCatchPattern":"try:\n    arr.merge_into(src, dest, i, j)\nexcept TypeError as e:\n    if 'None' in str(e):\n        dest = [None] * capacity\n        arr.merge_into(src, dest, i, capacity)","preventionTips":["Allocate dest buffer explicitly","Check both buffers before merging"],"tags":["python","merge","array","input-validation","typeerror"],"backgroundTag":"none-argument-validation","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}