pola-rs/polars · error
invalid alignment
Error message
invalid alignment: {format!r} What it means
Config.set_tbl_cell_alignment only accepts 'LEFT', 'CENTER', or 'RIGHT' (case-insensitive, uppercased internally) for the POLARS_FMT_TABLE_CELL_ALIGNMENT setting. Any other string raises a ValueError. Passing None resets the setting to its default.
Solutions
- Use one of 'LEFT', 'CENTER', 'RIGHT' (any casing).
- Pass None to reset to the default alignment.
- Validate against {'LEFT','CENTER','RIGHT'} before calling.
Example fix
// before
pl.Config.set_tbl_cell_alignment("middle")
// after
pl.Config.set_tbl_cell_alignment("CENTER") Defensive patterns
Strategy: validation
Validate before calling
VALID = {"LEFT", "CENTER", "RIGHT"}
if alignment is not None and alignment.upper() not in VALID:
raise ValueError(f"alignment must be one of {sorted(VALID)}")
pl.Config.set_tbl_cell_alignment(alignment) Type guard
def is_valid_alignment(v) -> bool:
return v is None or (isinstance(v, str) and v.upper() in {"LEFT", "CENTER", "RIGHT"}) Try / catch
try:
pl.Config.set_tbl_cell_alignment(alignment)
except ValueError as e:
logging.warning("bad alignment %r, using default", alignment)
pl.Config.set_tbl_cell_alignment(None) Prevention
- Keep alignment values as a module-level constant set.
- Never free-form string alignment values from user config without whitelisting.
- Remember comparison is case-insensitive, but the vocabulary is fixed.
When it happens
Trigger: pl.Config.set_tbl_cell_alignment('middle'), 'centre', 'justify', or any string not in {LEFT, CENTER, RIGHT}.
Common situations: Typing 'CENTER' variants like 'MIDDLE' or 'CENTRE'; loading alignment from a config file with unexpected casing is fine, but unsupported keywords are not.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- invalid engine
- invalid table format name
- {credential_provider}
- engine must be one of
- limit should be positive
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/777a96990029ab03.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/config.py:1004
┌────────────┬────────────┐
│ column_abc ┆ column_xyz │
│ --- ┆ --- │
│ f64 ┆ bool │
╞════════════╪════════════╡
│ 1.0 ┆ true │
│ 2.5 ┆ false │
│ 5.0 ┆ true │
└────────────┴────────────┘
Raises
------
ValueError: if alignment string not recognised.
"""
if format is None:
os.environ.pop("POLARS_FMT_TABLE_CELL_ALIGNMENT", None)
elif (format := format.upper()) not in {"LEFT", "CENTER", "RIGHT"}: # type: ignore[assignment]
msg = f"invalid alignment: {format!r}"
raise ValueError(msg)
else:
os.environ["POLARS_FMT_TABLE_CELL_ALIGNMENT"] = format
plr.config_reload_env_var("POLARS_FMT_TABLE_CELL_ALIGNMENT")
return cls
@classmethod
def set_tbl_cell_numeric_alignment(cls, format: Alignment | None) -> type[Config]:
"""
Set table cell alignment for numeric columns.
Parameters
----------
format : str
* "LEFT": left aligned
* "CENTER": center aligned
* "RIGHT": right aligned
ExamplesView on GitHub (pinned to fe841f959e)