{"record":{"id":"c060500684864742","repo":"Textualize/textual","slug":"before-argument-must-be-an-index-or-a-treenode-o","errorCode":null,"errorMessage":"`before` argument must be an index or a TreeNode object to add before","messagePattern":"`before` argument must be an index or a TreeNode object to add before","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/textual/widgets/_tree.py","lineNumber":405,"sourceCode":"            provided a `AddNodeError` will be raised.\n        \"\"\"\n        if before is not None and after is not None:\n            raise AddNodeError(\"Unable to add a node both before and after a node\")\n\n        insert_index: int = len(self.children)\n\n        if before is not None:\n            if isinstance(before, int):\n                insert_index = before\n            elif isinstance(before, TreeNode):\n                try:\n                    insert_index = self.children.index(before)\n                except ValueError:\n                    raise AddNodeError(\n                        \"The node specified for `before` is not a child of this node\"\n                    )\n            else:\n                raise TypeError(\n                    \"`before` argument must be an index or a TreeNode object to add before\"\n                )\n\n        if after is not None:\n            if isinstance(after, int):\n                insert_index = after + 1\n                if after < 0:\n                    insert_index += len(self.children)\n            elif isinstance(after, TreeNode):\n                try:\n                    insert_index = self.children.index(after) + 1\n                except ValueError:\n                    raise AddNodeError(\n                        \"The node specified for `after` is not a child of this node\"\n                    )\n            else:\n                raise TypeError(\n                    \"`after` argument must be an index or a TreeNode object to add after\"","sourceCodeStart":387,"sourceCodeEnd":423,"githubUrl":"https://github.com/Textualize/textual/blob/06dbeef4bb70fb718236aa418ed658ef4667a126/src/textual/widgets/_tree.py#L387-L423","documentation":"TypeError from TreeNode.add(): the `before` argument is neither an int nor a TreeNode — only those two types encode an insertion position.","triggerScenarios":"`node.add(\"x\", before=\"first\")`, `before=[0]`, or passing a label/other object where a position was expected.","commonSituations":"API confusion — passing a label string or an index wrapped in a list/tuple; refactors that changed the parameter type.","solutions":["Pass an int index (`before=0`) or a TreeNode instance.","Check the argument order: positional args are (label, data, before, after) — a misordered call can land a string in `before`."],"exampleFix":"# before\nnode.add(\"item\", before=\"node-1\")\n# after\nnode.add(\"item\", before=tree.get_node_by_id(\"node-1\"))","handlingStrategy":"type-guard","validationCode":"from textual.widgets._tree import TreeNode\nif not isinstance(before, (int, TreeNode)):\n    raise TypeError('before must be int or TreeNode')","typeGuard":"from textual.widgets._tree import TreeNode\ndef is_position(n: object) -> bool:\n    return isinstance(n, (int, TreeNode))","tryCatchPattern":"try:\n    node.add(label, before=pos)\nexcept TypeError:\n    node.add(label)","preventionTips":["Convert user-supplied indices with int()","Run a type checker (mypy) over tree insertion code"],"tags":["tree","type-error","insert","textual"],"backgroundTag":"wrong-argument-type","analyzedSha":"06dbeef4bb70fb718236aa418ed658ef4667a126","analyzedAt":"2026-08-27T02:36:57.214Z","schemaVersion":2},"datasetVersion":"2026-08-27T03:17:27.898Z"}