{"record":{"id":"7a89c8ec4f5f5e80","repo":"donnemartin/interactive-coding-challenges","slug":"word-cannot-be-none-7a89c8","errorCode":null,"errorMessage":"word cannot be None","messagePattern":"word cannot be None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"graphs_trees/trie/trie_solution.ipynb","lineNumber":199,"sourceCode":"    \"\\n\",\n    \"\\n\",\n    \"class Node(object):\\n\",\n    \"\\n\",\n    \"    def __init__(self, key, parent=None, terminates=False):\\n\",\n    \"        self.key = key\\n\",\n    \"        self.terminates = False\\n\",\n    \"        self.parent = parent\\n\",\n    \"        self.children = {}\\n\",\n    \"\\n\",\n    \"\\n\",\n    \"class Trie(object):\\n\",\n    \"\\n\",\n    \"    def __init__(self):\\n\",\n    \"        self.root = Node('')\\n\",\n    \"\\n\",\n    \"    def find(self, word):\\n\",\n    \"        if word is None:\\n\",\n    \"            raise TypeError('word cannot be None')\\n\",\n    \"        node = self.root\\n\",\n    \"        for char in word:\\n\",\n    \"            if char in node.children:\\n\",\n    \"                node = node.children[char]\\n\",\n    \"            else:\\n\",\n    \"                return None\\n\",\n    \"        return node if node.terminates else None\\n\",\n    \"\\n\",\n    \"    def insert(self, word):\\n\",\n    \"        if word is None:\\n\",\n    \"            raise TypeError('word cannot be None')\\n\",\n    \"        node = self.root\\n\",\n    \"        parent = None\\n\",\n    \"        for char in word:\\n\",\n    \"            if char in node.children:\\n\",\n    \"                node = node.children[char]\\n\",\n    \"            else:\\n\",\n    \"                node.children[char] = Node(char, parent=node)\\n\",","sourceCodeStart":181,"sourceCodeEnd":217,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/graphs_trees/trie/trie_solution.ipynb#L181-L217","documentation":"Trie.find raises TypeError('word cannot be None') because iterating over None with 'for char in word' would fail; the guard clause rejects None input explicitly. find returns the terminal node if the word exists (and terminates) or None otherwise, but only for string inputs.","triggerScenarios":"Calling trie.find(None), or passing a variable that is None because a lookup, parse, or user input produced no value.","commonSituations":"Processing user input or file tokens where a line/field is missing and becomes None; reusing a variable across find/insert/remove without checking it was set.","solutions":["Check the word is a non-None string before calling find: if word: node = trie.find(word)","Validate at the boundary where the string enters your program","Catch TypeError when None is an expected test input"],"exampleFix":"// before\nnode = trie.find(word)  # TypeError if word is None\n// after\nif word is not None:\n    node = trie.find(word)\nelse:\n    node = None","handlingStrategy":"validation","validationCode":"if not isinstance(word, str):\n    return None\nnode = trie.find(word)","typeGuard":"def is_word(word) -> bool:\n    return isinstance(word, str)","tryCatchPattern":"try:\n    node = trie.find(word)\nexcept TypeError:\n    node = None","preventionTips":["Validate word type at input boundaries (CLI, API, file parsing)","Treat None and empty string differently: empty string is valid, None is not"],"tags":["trie","string","none-guard","python"],"backgroundTag":"none-argument-guard","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}