{"record":{"id":"74d6013d273136e6","repo":"Textualize/textual","slug":"no-name-r-key-in-component-classes","errorCode":null,"errorMessage":"No {name!r} key in COMPONENT_CLASSES","messagePattern":"No (.+?) key in COMPONENT_CLASSES","errorType":"exception","errorClass":"KeyError","httpStatus":null,"severity":"error","filePath":"src/textual/dom.py","lineNumber":618,"sourceCode":"\n    def get_component_styles(self, *names: str) -> RenderStyles:\n        \"\"\"Get a \"component\" styles object (must be defined in COMPONENT_CLASSES classvar).\n\n        Args:\n            names: Names of the components.\n\n        Raises:\n            KeyError: If the component class doesn't exist.\n\n        Returns:\n            A Styles object.\n        \"\"\"\n\n        styles = RenderStyles(self, Styles(), Styles())\n\n        for name in names:\n            if name not in self._component_styles:\n                raise KeyError(f\"No {name!r} key in COMPONENT_CLASSES\")\n            component_styles = self._component_styles[name]\n            assert component_styles.node is not None\n            styles._update_node(component_styles.node)\n            styles.base.merge(component_styles.base)\n            styles.inline.merge(component_styles.inline)\n            styles._updates += 1\n\n        return styles\n\n    def _post_mount(self):\n        \"\"\"Called after the object has been mounted.\"\"\"\n        _rich_traceback_omit = True\n        Reactive._initialize_object(self)\n\n    def notify_style_update(self) -> None:\n        \"\"\"Called after styles are updated.\n\n        Implement this in a subclass if you want to clear any cached data when the CSS is reloaded.","sourceCodeStart":600,"sourceCodeEnd":636,"githubUrl":"https://github.com/Textualize/textual/blob/06dbeef4bb70fb718236aa418ed658ef4667a126/src/textual/dom.py#L600-L636","documentation":"get_component_styles raises KeyError when asked for a component style name that is not listed in the widget's COMPONENT_CLASSES. Textual requires every component class referenced in code (or matched from CSS like `-border` styling hooks) to be declared up-front so styles can be resolved.","triggerScenarios":"Calling self.get_component_styles('foo--bar') where 'foo--bar' is not in the widget's COMPONENT_CLASSES list; CSS referencing a component class the widget never declared; copying CSS between widget types with mismatched COMPONENT_CLASSES.","commonSituations":"Typos or renaming a component class in CSS but not in COMPONENT_CLASSES; creating a custom widget subclass and forgetting to extend COMPONENT_CLASSES; version upgrades that rename component classes (e.g. tabs, buttons).","solutions":["Add the component class string to the widget's COMPONENT_CLASSES = [...] class variable exactly as referenced","Fix typos/dashes in the name passed to get_component_styles so it matches an existing COMPONENT_CLASSES entry","If subclassing a built-in widget, remember COMPONENT_CLASSES must be re-declared/extended since it is not inherited additively"],"exampleFix":"# before\nclass MyWidget(Static):\n    def render_style(self):\n        return self.get_component_styles('mywidget--accent')\n\n# after\nclass MyWidget(Static):\n    COMPONENT_CLASSES = {'mywidget--accent'}\n    def render_style(self):\n        return self.get_component_styles('mywidget--accent')","handlingStrategy":"validation","validationCode":"name = 'mywidget--accent'\nif name not in self._component_styles:\n    # or: name not in getattr(type(self), 'COMPONENT_CLASSES', ())\n    raise LookupError(f'{name!r} missing from COMPONENT_CLASSES')\nstyles = self.get_component_styles(name)","typeGuard":"def has_component_class(widget, name: str) -> bool:\n    return name in widget._component_styles","tryCatchPattern":"try:\n    styles = self.get_component_styles('mywidget--accent')\nexcept KeyError as e:\n    styles = RenderStyles(self, Styles(), Styles())  # graceful default","preventionTips":["Declare every component class used in CSS/code in COMPONENT_CLASSES","Keep CSS and COMPONENT_CLASSES in one commit when renaming","Add a startup test iterating COMPONENT_CLASSES-referencing CSS"],"tags":["textual","css","component-classes","widget"],"backgroundTag":"missing-declared-class-key","analyzedSha":"06dbeef4bb70fb718236aa418ed658ef4667a126","analyzedAt":"2026-08-27T02:36:57.214Z","schemaVersion":2},"datasetVersion":"2026-08-27T03:17:27.898Z"}