{"record":{"id":"5a30a2a3fd807abd","repo":"donnemartin/interactive-coding-challenges","slug":"str-input-cannot-be-none","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_common_subsequence/longest_common_subseq_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_subseq(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 subsequence\\n\",","sourceCodeStart":108,"sourceCodeEnd":144,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/recursion_dynamic/longest_common_subsequence/longest_common_subseq_solution.ipynb#L108-L144","documentation":"Raised by StringCompare.longest_common_subseq when str0 or str1 is None. The method sizes its DP table as len(str0)+1 by len(str1)+1 in the very next lines, so None strings are rejected explicitly with a clear TypeError instead of 'object of type NoneType has no len()'.","triggerScenarios":"Calling longest_common_subseq(None, 'abc') or longest_common_subseq('abc', None). Empty strings '' are valid (they produce a table of zeros) and do not raise.","commonSituations":"Comparing user-supplied or database fields where one record's column is NULL; diffing files where one side failed to read; optional function parameters defaulting to None.","solutions":["Coalesce None to '' at the call site if an empty comparison is meaningful: (str0 or '')","Validate that both operands are str before calling (isinstance checks)","Fix upstream data loading so missing text fields become empty strings"],"exampleFix":"// before\nlcs = sc.longest_common_subseq(record_a.bio, record_b.bio)\n// after\nlcs = sc.longest_common_subseq(record_a.bio or '', record_b.bio or '')","handlingStrategy":"type-guard","validationCode":"str0 = str0 or ''\nstr1 = str1 or ''\nsc.longest_common_subseq(str0, str1)","typeGuard":"def is_str_pair(a, b):\n    return isinstance(a, str) and isinstance(b, str)","tryCatchPattern":"try:\n    sc.longest_common_subseq(a, b)\nexcept TypeError:\n    lcs = ''","preventionTips":["Coerce nullable text columns to ''","Validate with isinstance at ingestion"],"tags":["python","dynamic-programming","string","lcs","input-validation"],"backgroundTag":"none-argument-validation","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}