{"record":{"id":"0af39341429427e2","repo":"Textualize/textual","slug":"keyerror-key","errorCode":null,"errorMessage":"KeyError(key)","messagePattern":"KeyError\\(key\\)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/textual/css/styles.py","lineNumber":580,"sourceCode":"                easing=easing,\n                on_complete=(\n                    partial(self.node.app.call_later, on_complete)\n                    if on_complete is not None\n                    else None\n                ),\n                level=level,\n            )\n        return None\n\n    def __eq__(self, styles: object) -> bool:\n        \"\"\"Check that Styles contains the same rules.\"\"\"\n        if not isinstance(styles, StylesBase):\n            return NotImplemented\n        return self.get_rules() == styles.get_rules()\n\n    def __getitem__(self, key: str) -> object:\n        if key not in RULE_NAMES_SET:\n            raise KeyError(key)\n        return getattr(self, key)\n\n    def get(self, key: str, default: object | None = None) -> object:\n        return getattr(self, key) if key in RULE_NAMES_SET else default\n\n    def __len__(self) -> int:\n        return len(RULE_NAMES)\n\n    def __iter__(self) -> Iterator[str]:\n        return iter(RULE_NAMES)\n\n    def __contains__(self, key: object) -> bool:\n        return key in RULE_NAMES_SET\n\n    def keys(self) -> Iterable[str]:\n        return RULE_NAMES\n\n    def values(self) -> Iterable[object]:","sourceCodeStart":562,"sourceCodeEnd":598,"githubUrl":"https://github.com/Textualize/textual/blob/06dbeef4bb70fb718236aa418ed658ef4667a126/src/textual/css/styles.py#L562-L598","documentation":"Styles.__getitem__ raises KeyError when the key is not one of the known CSS rule names (RULE_NAMES_SET). The mapping-style access only supports actual style rule names like 'width', 'color', 'display', etc.","triggerScenarios":"styles['widht'] (typo), styles['some_custom_name'], or accessing via a name that is a Python attribute but not a registered rule. Note: use styles.get(key) for a None-safe lookup.","commonSituations":"Iterating/generic code that reads arbitrary keys from a Styles object; typos in rule names; assuming any Styles property is addressable via [] access.","solutions":["Use styles.get(key) which returns None/default for unknown keys","Verify the key against the known rule names (styles.get_rules() keys or textual.css.styles constants)","Fix typos in the rule name"],"exampleFix":"// before\nvalue = styles['background']  # wrong name\n// after\nvalue = styles.get('background-color', None)","handlingStrategy":"type-guard","validationCode":"from textual.css.styles import RULE_NAMES_SET\nkey = 'width'\nif key not in RULE_NAMES_SET:\n    raise ValueError(f'unknown style rule {key}')","typeGuard":"from textual.css.styles import RULE_NAMES_SET\ndef is_style_rule(key: str) -> bool:\n    return key in RULE_NAMES_SET","tryCatchPattern":"try:\n    value = styles[key]\nexcept KeyError:\n    value = None  # or styles.get(key)","preventionTips":["Prefer styles.get(key) for dynamic access","Whitelist keys against RULE_NAMES_SET before indexing","Fix typos in rule names"],"tags":["textual","styles","keyerror","css-rules"],"backgroundTag":"invalid-dict-key","analyzedSha":"06dbeef4bb70fb718236aa418ed658ef4667a126","analyzedAt":"2026-08-27T02:36:57.214Z","schemaVersion":2},"datasetVersion":"2026-08-27T03:17:27.898Z"}