{"record":{"id":"1ae57a57de883ff9","repo":"Textualize/textual","slug":"child-with-id-id-r-is-the-wrong-type-expected-t","errorCode":null,"errorMessage":"Child with id={id!r} is the wrong type; expected type {expect_type.__name__!r}, found {child}","messagePattern":"Child with id=(.+?) is the wrong type; expected type (.+?), found (.+?)","errorType":"exception","errorClass":"WrongType","httpStatus":null,"severity":"error","filePath":"src/textual/widget.py","lineNumber":1113,"sourceCode":"\n        Args:\n            id: The ID of the child.\n            expect_type: Require the object be of the supplied type, or None for any type.\n\n        Returns:\n            The first child of this node with the ID.\n\n        Raises:\n            NoMatches: if no children could be found for this ID\n            WrongType: if the wrong type was found.\n        \"\"\"\n        child = self._get_dom_base()._nodes._get_by_id(id)\n        if child is None:\n            raise NoMatches(f\"No child found with id={id!r}\")\n        if expect_type is None:\n            return child\n        if not isinstance(child, expect_type):\n            raise WrongType(\n                f\"Child with id={id!r} is the wrong type; expected type {expect_type.__name__!r}, found {child}\"\n            )\n        return child\n\n    if TYPE_CHECKING:\n\n        @overload\n        def get_widget_by_id(self, id: str) -> Widget: ...\n\n        @overload\n        def get_widget_by_id(\n            self, id: str, expect_type: type[ExpectType]\n        ) -> ExpectType: ...\n\n    def get_widget_by_id(\n        self, id: str, expect_type: type[ExpectType] | None = None\n    ) -> ExpectType | Widget:\n        \"\"\"Return the first descendant widget with the given ID.","sourceCodeStart":1095,"sourceCodeEnd":1131,"githubUrl":"https://github.com/Textualize/textual/blob/06dbeef4bb70fb718236aa418ed658ef4667a126/src/textual/widget.py#L1095-L1131","documentation":"get_child_by_id found a child with the requested id, but its type does not match the expect_type argument, so WrongType is raised instead of returning a mistyped widget.","triggerScenarios":"get_child_by_id(\"list\", expect_type=ListView) when the child with id 'list' is actually a ListView subclass or a different widget type entirely.","commonSituations":"Refactoring markup that changed widget types while lookup code kept the old expect_type; subclass instances when an exact type was assumed.","solutions":["Correct expect_type to match the actual widget (or a base class of it)","Change the widget in compose() to the expected type","Drop expect_type and isinstance-check the result if flexible"],"exampleFix":"# before\nitem = self.get_child_by_id(\"list\", expect_type=OptionList)\n# after\nitem = self.get_child_by_id(\"list\", expect_type=OptionList)\n# or fix compose():\n# OptionList(id=\"list\") instead of ListView(id=\"list\")","handlingStrategy":"type-guard","validationCode":"child = self._get_dom_base()._nodes._get_by_id(id)\nif child is not None and not isinstance(child, expect_type):\n    # fix compose() or adjust expect_type","typeGuard":"def child_matches(child: object, expect_type: type) -> bool:\n    return isinstance(child, expect_type)","tryCatchPattern":"try:\n    w = self.get_child_by_id(\"x\", expect_type=T)\nexcept NoMatches:\n    ...\nexcept WrongType as e:\n    w = self.query_one(\"#x\")  # inspect actual type and adjust","preventionTips":["Keep expect_type in sync with compose() definitions","Prefer base-class expect_type for subclass tolerance"],"tags":["dom-tree","type-mismatch","widget","textual"],"backgroundTag":"wrong-element-type","analyzedSha":"06dbeef4bb70fb718236aa418ed658ef4667a126","analyzedAt":"2026-08-27T02:36:57.214Z","schemaVersion":2},"datasetVersion":"2026-08-27T03:17:27.898Z"}