{"record":{"id":"d70bf7fb9aa0b847","repo":"donnemartin/interactive-coding-challenges","slug":"data-cannot-be-none-d70bf7","errorCode":null,"errorMessage":"data cannot be None","messagePattern":"data cannot be None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sorting_searching/merge_sort/merge_sort_solution.ipynb","lineNumber":112,"sourceCode":"   \"metadata\": {},\n   \"source\": [\n    \"## Code\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 1,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"from __future__ import division\\n\",\n    \"\\n\",\n    \"\\n\",\n    \"class MergeSort(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    \"        return self._sort(data)\\n\",\n    \"\\n\",\n    \"    def _sort(self, data):\\n\",\n    \"        if len(data) < 2:\\n\",\n    \"            return data\\n\",\n    \"        mid = len(data) // 2\\n\",\n    \"        left = data[:mid]\\n\",\n    \"        right = data[mid:]\\n\",\n    \"        left = self._sort(left)\\n\",\n    \"        right = self._sort(right)\\n\",\n    \"        return self._merge(left, right)\\n\",\n    \"\\n\",\n    \"    def _merge(self, left, right):\\n\",\n    \"        l = 0\\n\",\n    \"        r = 0\\n\",\n    \"        result = []\\n\",\n    \"        while l < len(left) and r < len(right):\\n\",\n    \"            if left[l] < right[r]:\\n\",","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/sorting_searching/merge_sort/merge_sort_solution.ipynb#L94-L130","documentation":"MergeSort.sort raises TypeError('data cannot be None') when the input to sort() is None. It is an explicit input-validation guard at the public API boundary before the recursive _sort runs. Passing None would otherwise fail deeper with a confusing 'len(None)' AttributeError.","triggerScenarios":"Calling MergeSort().sort(None), or passing a variable that was initialized to None / failed to load (e.g. a parsed dataset or API response that came back empty) into sort().","commonSituations":"Data pipelines where an upstream fetch or file read returns None; unit tests that exercise None handling; refactors that make a data argument optional without a default empty list.","solutions":["Ensure the caller passes a real list, e.g. data = data or [] before calling sort()","Add a None check in your own code and skip/short-circuit sorting when data is None","If None should be treated as empty, wrap: sorted_data = ms.sort(data) if data is not None else []"],"exampleFix":"// before\nresult = MergeSort().sort(data)  # data may be None\n\n// after\nresult = MergeSort().sort(data if data is not None else [])","handlingStrategy":"validation","validationCode":"if data is None:\n    data = []\nresult = MergeSort().sort(data)","typeGuard":"def is_sortable(x):\n    return isinstance(x, list) and x is not None","tryCatchPattern":"try:\n    result = ms.sort(data)\nexcept TypeError as e:\n    if 'cannot be None' in str(e):\n        result = []\n    else:\n        raise","preventionTips":["Initialize data variables to [] instead of None","Validate upstream loaders that can return None","Guard calls with 'if data is not None'"],"tags":["python","input-validation","typeerror","merge-sort"],"backgroundTag":"none-input-validation","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}