{"record":{"id":"301ba363ccadad21","repo":"donnemartin/interactive-coding-challenges","slug":"data-cannot-be-none-301ba3","errorCode":null,"errorMessage":"data cannot be None","messagePattern":"data cannot be None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sorting_searching/insertion_sort/insertion_sort_solution.ipynb","lineNumber":103,"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 InsertionSort(object):\\n\",\n    \"\\n\",\n    \"    def sort(self, data):\\n\",\n    \"        if data is None:\\n\",\n    \"            raise TypeError('data cannot be None')\\n\",\n    \"        if len(data) < 2:\\n\",\n    \"            return data\\n\",\n    \"        for r in range(1, len(data)):\\n\",\n    \"            for l in range(r):\\n\",\n    \"                if data[r] < data[l]:\\n\",\n    \"                    temp = data[r]\\n\",\n    \"                    data[l+1:r+1] = data[l:r]\\n\",\n    \"                    data[l] = temp\\n\",\n    \"        return data\"\n   ]\n  },\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"## Unit Test\\n\",\n    \"\\n\"\n   ]","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/sorting_searching/insertion_sort/insertion_sort_solution.ipynb#L85-L121","documentation":"Raised by InsertionSort.sort when data is None. The very next line calls len(data), so None is rejected explicitly with a descriptive TypeError rather than the implicit 'object of type NoneType has no len()'.","triggerScenarios":"Calling sort(None). Lists of length 0 or 1 are returned unchanged; only None raises.","commonSituations":"Sorting a list produced by a filter that returned None; data fetched from a source with a missing array; a variable declared but never assigned before the sort call.","solutions":["Ensure the list is initialized to [] before passing","Coalesce: data = data or []","Have data-producing functions return empty collections on failure"],"exampleFix":"// before\nsorted_data = InsertionSort().sort(records)\n// after\nrecords = records or []\nsorted_data = InsertionSort().sort(records)","handlingStrategy":"validation","validationCode":"data = data if data is not None else []\nInsertionSort().sort(data)","typeGuard":"def is_list(x):\n    return isinstance(x, list)","tryCatchPattern":"try:\n    InsertionSort().sort(data)\nexcept TypeError:\n    data = []\n    InsertionSort().sort(data)","preventionTips":["Initialize collections to [] not None","Have producers return empty collections"],"tags":["python","sorting","insertion-sort","input-validation"],"backgroundTag":"none-argument-validation","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}