{"record":{"id":"a2220c3c9134c5b8","repo":"donnemartin/interactive-coding-challenges","slug":"str-input-cannot-be-none-a2220c","errorCode":null,"errorMessage":"str input cannot be None","messagePattern":"str input cannot be None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"recursion_dynamic/longest_substring/longest_common_substr_solution.ipynb","lineNumber":126,"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 StringCompare(object):\\n\",\n    \"\\n\",\n    \"    def longest_common_substr(self, str0, str1):\\n\",\n    \"        if str0 is None or str1 is None:\\n\",\n    \"            raise TypeError('str input cannot be None')\\n\",\n    \"        # Add one to number of rows and cols for the dp table's\\n\",\n    \"        # first row of 0's and first col of 0's\\n\",\n    \"        num_rows = len(str0) + 1\\n\",\n    \"        num_cols = len(str1) + 1\\n\",\n    \"        T = [[None] * num_cols for _ in range(num_rows)]\\n\",\n    \"        for i in range(num_rows):\\n\",\n    \"            for j in range(num_cols):\\n\",\n    \"                if i == 0 or j == 0:\\n\",\n    \"                    T[i][j] = 0\\n\",\n    \"                elif str0[j-1] != str1[i-1]:\\n\",\n    \"                    T[i][j] = max(T[i][j-1],\\n\",\n    \"                                  T[i-1][j])\\n\",\n    \"                else:\\n\",\n    \"                    T[i][j] = T[i-1][j-1] + 1\\n\",\n    \"        results = ''\\n\",\n    \"        i = num_rows - 1\\n\",\n    \"        j = num_cols - 1\\n\",\n    \"        # Walk backwards to determine the substring\\n\",","sourceCodeStart":108,"sourceCodeEnd":144,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/recursion_dynamic/longest_substring/longest_common_substr_solution.ipynb#L108-L144","documentation":"Raised by StringCompare.longest_common_substr when str0 or str1 is None. The DP table is sized from len(str0)+1 and len(str1)+1 immediately after the guard, so None is rejected with a descriptive TypeError rather than the implicit 'NoneType has no len()' error.","triggerScenarios":"Calling longest_common_substr(None, s) or longest_common_substr(s, None). Empty strings are valid inputs and yield a result of 0; only None raises.","commonSituations":"Comparing nullable database text columns; one side of the comparison coming from a failed fetch; optional parameters that were never supplied.","solutions":["Normalize inputs: (str0 or '') and (str1 or '') before calling","Add isinstance(x, str) validation at your API boundary","Ensure upstream loaders substitute '' for missing text"],"exampleFix":"// before\nn = sc.longest_common_substr(row_a.text, row_b.text)\n// after\nn = sc.longest_common_substring = sc.longest_common_substr(row_a.text or '', row_b.text or '')","handlingStrategy":"type-guard","validationCode":"str0 = str0 or ''\nstr1 = str1 or ''\nsc.longest_common_substr(str0, str1)","typeGuard":"def is_str_pair(a, b):\n    return isinstance(a, str) and isinstance(b, str)","tryCatchPattern":"try:\n    sc.longest_common_substr(a, b)\nexcept TypeError:\n    result = 0","preventionTips":["Map NULL columns to ''","Validate before comparing"],"tags":["python","dynamic-programming","string","substring","input-validation"],"backgroundTag":"none-argument-validation","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}