{"record":{"id":"e5712f36e726d9e1","repo":"Textualize/rich","slug":"invalid-value-for-align-expected-left-center-e5712f","errorCode":null,"errorMessage":"invalid value for align, expected \"left\", \"center\", \"right\" (not {align!r})","messagePattern":"invalid value for align, expected \"left\", \"center\", \"right\" \\(not (.+?)\\)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"rich/rule.py","lineNumber":37,"sourceCode":"        end (str, optional): Character at end of Rule. defaults to \"\\\\\\\\n\"\n        align (str, optional): How to align the title, one of \"left\", \"center\", or \"right\". Defaults to \"center\".\n    \"\"\"\n\n    def __init__(\n        self,\n        title: Union[str, Text] = \"\",\n        *,\n        characters: str = \"─\",\n        style: Union[str, Style] = \"rule.line\",\n        end: str = \"\\n\",\n        align: AlignMethod = \"center\",\n    ) -> None:\n        if cell_len(characters) < 1:\n            raise ValueError(\n                \"'characters' argument must have a cell width of at least 1\"\n            )\n        if align not in (\"left\", \"center\", \"right\"):\n            raise ValueError(\n                f'invalid value for align, expected \"left\", \"center\", \"right\" (not {align!r})'\n            )\n        self.title = title\n        self.characters = characters\n        self.style = style\n        self.end = end\n        self.align = align\n\n    def __repr__(self) -> str:\n        return f\"Rule({self.title!r}, {self.characters!r})\"\n\n    def __rich_console__(\n        self, console: Console, options: ConsoleOptions\n    ) -> RenderResult:\n        width = options.max_width\n\n        characters = (\n            \"-\"","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/Textualize/rich/blob/9d8f9a372cc5916fd4781fec207ced7ddac2f08f/rich/rule.py#L19-L55","documentation":"Rule.__init__ validates its align keyword against the literal set ('left', 'center', 'right') and raises ValueError with the offending value otherwise. The check happens before any rendering, at construction time, so a bad align fails immediately when Rule(...) is created.","triggerScenarios":"Rule('T', align='centre') (British spelling), Rule(align='middle'), Rule(align='Center') (capital C), or an align pulled from user/config data such as Rule(align=cfg['align']).","commonSituations":"Config files with 'centre'/'middle' spellings; case-sensitive values from user input ('Left'); passing constants from another library (e.g. matplotlib's 'center' vs a custom enum) without mapping.","solutions":["Use exactly 'left', 'center', or 'right' (lowercase, American spelling).","Normalize external values before constructing: align = str(align).lower() and validate against a whitelist, falling back to 'center'.","If align comes from config, add a validation/enum in your settings schema."],"exampleFix":"# before\nprint(Rule('Title', align='centre'))  # ValueError: invalid value for align\n\n# after\nalign = cfg.get('align', 'center').lower()\nif align not in ('left', 'center', 'right'):\n    align = 'center'\nprint(Rule('Title', align=align))","handlingStrategy":"validation","validationCode":"ALLOWED = ('left', 'center', 'right')\nalign = str(align).lower()\nif align not in ALLOWED:\n    align = 'center'\nrule = Rule('Title', align=align)","typeGuard":"def is_valid_align(value: str) -> bool:\n    \"\"\"Narrow a string to a rich Rule AlignMethod.\"\"\"\n    return isinstance(value, str) and value in ('left', 'center', 'right')","tryCatchPattern":null,"preventionTips":["Lowercase and whitelist external align values before use.","Remember only exact lowercase 'left'/'center'/'right' are accepted ('centre' fails)."],"tags":["rich","rule","alignment","validation","valueerror"],"backgroundTag":null,"analyzedSha":"9d8f9a372cc5916fd4781fec207ced7ddac2f08f","analyzedAt":"2026-08-15T03:30:11.781Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}