{"record":{"id":"54748ebc4ab3de0c","repo":"donnemartin/interactive-coding-challenges","slug":"data-cannot-be-none-54748e","errorCode":null,"errorMessage":"data cannot be None","messagePattern":"data cannot be None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sorting_searching/quick_sort/quick_sort_solution.ipynb","lineNumber":113,"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 QuickSort(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    \"        equal = []\\n\",\n    \"        left = []\\n\",\n    \"        right = []\\n\",\n    \"        pivot_index = len(data) // 2\\n\",\n    \"        pivot_value = data[pivot_index]\\n\",\n    \"        # Build the left and right partitions\\n\",\n    \"        for item in data:\\n\",\n    \"            if item == pivot_value:\\n\",\n    \"                equal.append(item)\\n\",\n    \"            elif item < pivot_value:\\n\",\n    \"                left.append(item)\\n\",\n    \"            else:\\n\",\n    \"                right.append(item)\\n\",","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/sorting_searching/quick_sort/quick_sort_solution.ipynb#L95-L131","documentation":"QuickSort.sort raises TypeError('data cannot be None') as an explicit guard before delegating to the recursive _sort. Without it, len(data) inside _sort would raise an AttributeError on None. Only None is rejected; an empty list is valid and returned as-is.","triggerScenarios":"Calling QuickSort().sort(None), or passing a None-valued variable (failed load, unset default parameter, conditional that never assigned a list).","commonSituations":"Optional config/data that defaults to None; empty results from a database query being confused with None; tests exercising invalid input.","solutions":["Pass a list (use data or [] if None means empty) before calling sort()","Guard the call site: if data is not None: result = qs.sort(data)","Fix the upstream code that produced None instead of a list"],"exampleFix":"// before\nresult = QuickSort().sort(data)  # data may be None\n\n// after\nresult = QuickSort().sort(data) if data is not None else []","handlingStrategy":"validation","validationCode":"if data is None:\n    data = []\nresult = QuickSort().sort(data)","typeGuard":"def is_sortable(x):\n    return isinstance(x, list)","tryCatchPattern":"try:\n    result = qs.sort(data)\nexcept TypeError as e:\n    if 'cannot be None' in str(e):\n        result = []\n    else:\n        raise","preventionTips":["Default optional data params to [] not None","Validate function arguments at call boundary","Normalize None to [] right after loading data"],"tags":["python","input-validation","typeerror","quick-sort"],"backgroundTag":"none-input-validation","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}