{"record":{"id":"370ee4cdf115fb07","repo":"donnemartin/interactive-coding-challenges","slug":"string-cannot-be-none","errorCode":null,"errorMessage":"string cannot be None","messagePattern":"string cannot be None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"online_judges/longest_substr_k_distinct/longest_substr_solution.ipynb","lineNumber":102,"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 Solution(object):\\n\",\n    \"\\n\",\n    \"    def longest_substr(self, string, k):\\n\",\n    \"        if string is None:\\n\",\n    \"            raise TypeError('string cannot be None')\\n\",\n    \"        if k is None:\\n\",\n    \"            raise TypeError('k cannot be None')\\n\",\n    \"        low_index = 0\\n\",\n    \"        max_length = 0\\n\",\n    \"        chars_to_index_map = {}\\n\",\n    \"        for index, char in enumerate(string):\\n\",\n    \"            chars_to_index_map[char] = index\\n\",\n    \"            if len(chars_to_index_map) > k:\\n\",\n    \"                low_index = min(chars_to_index_map.values())\\n\",\n    \"                del chars_to_index_map[string[low_index]]\\n\",\n    \"                low_index += 1\\n\",\n    \"            max_length = max(max_length, index - low_index + 1)\\n\",\n    \"        return max_length\"\n   ]\n  },\n  {\n   \"cell_type\": \"markdown\",\n   \"metadata\": {},","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/online_judges/longest_substr_k_distinct/longest_substr_solution.ipynb#L84-L120","documentation":"Raised by Solution.longest_substr (longest substring with at most k distinct chars) when string is None. The method enumerates the string; None would fail inside the loop, so the guard requires a str at the API boundary.","triggerScenarios":"Calling longest_substr(None, 2) — an unset variable, a None default parameter, or text fetched from a nullable field/API response.","commonSituations":"Processing nullable text fields from JSON payloads; tests asserting the guard; optional CLI args passed through unvalidated.","solutions":["Pass a string (empty string '' is fine and returns 0)","Default at the call site: s = s or ''","Fix the producer returning None for missing text"],"exampleFix":"# before\nsolution.longest_substr(payload.get('text'), k)  # text field may be null\n\n# after\nsolution.longest_substr(payload.get('text') or '', k)","handlingStrategy":"validation","validationCode":"string = string or ''\nsolution.longest_substr(string, k)","typeGuard":"def is_str(s): return isinstance(s, str)","tryCatchPattern":"try:\n    n = solution.longest_substr(s, k)\nexcept TypeError:\n    n = 0","preventionTips":["Coalesce nullable text fields with `or ''`","Validate API response fields before string algorithms"],"tags":["python","input-validation","none-check","strings","sliding-window"],"backgroundTag":"none-argument-validation","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}