{"record":{"id":"033974ed09782f1f","repo":"Textualize/rich","slug":"invalid-value-for-align-expected-left-center","errorCode":null,"errorMessage":"invalid value for align, expected \"left\", \"center\", or \"right\" (not {align!r})","messagePattern":"invalid value for align, expected \"left\", \"center\", or \"right\" \\(not (.+?)\\)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"rich/align.py","lineNumber":59,"sourceCode":"            p = Panel(\"Hello, [b]World[/b]!\", style=\"on green\", width=20)\n\n            # Renders the panel centered in the terminal\n            console.print(Align(p, align=\"center\"))\n    \"\"\"\n\n    def __init__(\n        self,\n        renderable: \"RenderableType\",\n        align: AlignMethod = \"left\",\n        style: Optional[StyleType] = None,\n        *,\n        vertical: Optional[VerticalAlignMethod] = None,\n        pad: bool = True,\n        width: Optional[int] = None,\n        height: Optional[int] = None,\n    ) -> None:\n        if align not in (\"left\", \"center\", \"right\"):\n            raise ValueError(\n                f'invalid value for align, expected \"left\", \"center\", or \"right\" (not {align!r})'\n            )\n        if vertical is not None and vertical not in (\"top\", \"middle\", \"bottom\"):\n            raise ValueError(\n                f'invalid value for vertical, expected \"top\", \"middle\", or \"bottom\" (not {vertical!r})'\n            )\n        self.renderable = renderable\n        self.align = align\n        self.style = style\n        self.vertical = vertical\n        self.pad = pad\n        self.width = width\n        self.height = height\n\n    def __repr__(self) -> str:\n        return f\"Align({self.renderable!r}, {self.align!r})\"\n\n    @classmethod","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/Textualize/rich/blob/9d8f9a372cc5916fd4781fec207ced7ddac2f08f/rich/align.py#L41-L77","documentation":"Align.__init__ validates its align argument against the allowed set (\"left\", \"center\", \"right\") and raises ValueError for anything else. Rich uses these literal strings (AlignMethod) instead of an enum, so no casing variants or synonyms (e.g. \"Left\", \"centre\", \"justify\") are accepted. The check happens eagerly at construction time, before any rendering occurs.","triggerScenarios":"Calling Align(renderable, align=\"centre\"), Align(renderable, \"middle\"), or passing a user-supplied config value (e.g. align=cfg['align'] from YAML/CLI) that is misspelled or capitalized differently. Also passing an enum member or a non-string such as None or an int.","commonSituations":"Config files or CLI flags with British spelling \"centre\"; capitalization from user input (\"Center\"); passing None expecting a default; dynamic alignment values from a data schema that uses \"justify\" or \"start\"/\"end\".","solutions":["Correct the value to exactly one of the lowercase strings \"left\", \"center\", or \"right\"","If the value comes from user config, normalize it before passing: strip whitespace and lower() it, then check membership","Validate/normalize at the config boundary so the error surfaces at input parsing, not during rendering"],"exampleFix":"// before (Python)\nAlign(table, align=\"Centre\")  # ValueError\n// after\n_ALIGN = {\"left\", \"center\", \"right\"}\nalign = str(cfg[\"align\"]).strip().lower()\nif align not in _ALIGN:\n    raise ValueError(f\"config 'align' must be one of {sorted(_ALIGN)}, got {cfg['align']!r}\")\nAlign(table, align=align)","handlingStrategy":"type-guard","validationCode":"align = str(value).strip().lower()\nif align not in (\"left\", \"center\", \"right\"):\n    raise ValueError(f\"align must be left/center/right, got {value!r}\")\nrender = Align(panel, align=align)","typeGuard":"from typing import Literal, TypeGuard\n\nAlignMethod = Literal[\"left\", \"center\", \"right\"]\n\ndef is_align_method(v: object) -> TypeGuard[AlignMethod]:\n    return v in (\"left\", \"center\", \"right\")","tryCatchPattern":null,"preventionTips":["Normalize external align values with strip().lower() before constructing Align","Keep a single Literal type alias for AlignMethod and use it in your function signatures so type checkers catch bad values","Validate config values once at load time instead of during rendering"],"tags":["python","rich","align","validation","valueerror"],"backgroundTag":null,"analyzedSha":"9d8f9a372cc5916fd4781fec207ced7ddac2f08f","analyzedAt":"2026-08-15T03:30:11.781Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}