{"record":{"id":"c04028f7ed52fc35","repo":"donnemartin/interactive-coding-challenges","slug":"data-cannot-be-none-c04028","errorCode":null,"errorMessage":"data cannot be None","messagePattern":"data cannot be None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sorting_searching/selection_sort/selection_sort_solution.ipynb","lineNumber":105,"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 SelectionSort(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 i in range(len(data) - 1):\\n\",\n    \"            min_index = i\\n\",\n    \"            for j in range(i + 1, len(data)):\\n\",\n    \"                if data[j] < data[min_index]:\\n\",\n    \"                    min_index = j\\n\",\n    \"            if data[min_index] < data[i]:\\n\",\n    \"                data[i], data[min_index] = data[min_index], data[i]\\n\",\n    \"        return data\\n\",\n    \"\\n\",\n    \"    def sort_iterative_alt(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 i in range(len(data) - 1):\\n\",\n    \"            self._swap(data, i, self._find_min_index(data, i))\\n\",","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/sorting_searching/selection_sort/selection_sort_solution.ipynb#L87-L123","documentation":"SelectionSort.sort raises TypeError('data cannot be None') as the first statement; the algorithm then indexes data[i] and compares elements, so None input is rejected up front with a clear message. Empty and single-element lists are valid and returned unchanged.","triggerScenarios":"Calling SelectionSort().sort(None); passing an unassigned or failed-to-load list variable.","commonSituations":"Upstream data loaders returning None on error; optional parameters defaulting to None; notebook cells run out of order so the data variable is still None.","solutions":["Initialize the variable to [] or guard with 'if data is not None' before sorting","Fix the upstream producer so it returns a list (or raises its own clear error) instead of None","Substitute an empty list when None means no data: sort(data or [])"],"exampleFix":"// before\nresult = SelectionSort().sort(data)  # data may be None\n\n// after\nresult = SelectionSort().sort(data or [])","handlingStrategy":"validation","validationCode":"if data is None:\n    data = []\nresult = SelectionSort().sort(data)","typeGuard":"def is_sortable(x):\n    return isinstance(x, list)","tryCatchPattern":"try:\n    result = ss.sort(data)\nexcept TypeError as e:\n    if 'cannot be None' in str(e):\n        result = []\n    else:\n        raise","preventionTips":["Never let data variables stay None before sort calls","Use 'data or []' normalization after optional loads","Validate inputs once at the pipeline entry point"],"tags":["python","input-validation","typeerror","selection-sort"],"backgroundTag":"none-input-validation","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}