{"record":{"id":"91dc59ffc86ba705","repo":"pola-rs/polars","slug":"unit-must-be-one-of-b-kb-mb-gb-tb-got-unit-r","errorCode":null,"errorMessage":"`unit` must be one of {'b', 'kb', 'mb', 'gb', 'tb'}, got {unit!r}","messagePattern":"`unit` must be one of (.+?), got (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_utils/various.py","lineNumber":311,"sourceCode":"@overload\ndef scale_bytes(sz: Expr, unit: SizeUnit) -> Expr: ...\n\n\ndef scale_bytes(sz: int | Expr, unit: SizeUnit) -> int | float | Expr:\n    \"\"\"Scale size in bytes to other size units (eg: \"kb\", \"mb\", \"gb\", \"tb\").\"\"\"\n    if unit in {\"b\", \"bytes\"}:\n        return sz\n    elif unit in {\"kb\", \"kilobytes\"}:\n        return sz / 1024\n    elif unit in {\"mb\", \"megabytes\"}:\n        return sz / 1024**2\n    elif unit in {\"gb\", \"gigabytes\"}:\n        return sz / 1024**3\n    elif unit in {\"tb\", \"terabytes\"}:\n        return sz / 1024**4\n    else:\n        msg = f\"`unit` must be one of {{'b', 'kb', 'mb', 'gb', 'tb'}}, got {unit!r}\"\n        raise ValueError(msg)\n\n\ndef _cast_repr_strings_with_schema(\n    df: DataFrame, schema: dict[str, PolarsDataType | None]\n) -> DataFrame:\n    \"\"\"\n    Utility function to cast table repr/string values into frame-native types.\n\n    Parameters\n    ----------\n    df\n        Dataframe containing string-repr column data.\n    schema\n        DataFrame schema containing the desired end-state types.\n\n    Notes\n    -----\n    Table repr strings are less strict (or different) than equivalent CSV data, so need","sourceCodeStart":293,"sourceCodeEnd":329,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/_utils/various.py#L293-L329","documentation":"scale_bytes converts a byte size to a human-readable string using a unit keyword. If the unit is not one of b/kb/mb/gb/tb (or their long aliases like 'kilobytes'), it raises ValueError listing the accepted units. It's a strict unit-argument validation.","triggerScenarios":"Calling the internal scale_bytes (used in repr/table formatting and public helpers) with a unit like 'kib', 'KiB', 'B', 'kb ' with whitespace, or a typo such as 'kilobyte'.","commonSituations":"Configuring display/str formatting with a custom unit, wrapping scale_bytes in user tooling with nonstandard units, or case/whitespace mistakes.","solutions":["Use one of 'b','kb','mb','gb','tb' (or 'bytes','kilobytes','megabytes','gigabytes','terabytes'), lowercased and trimmed.","Normalize input: unit = unit.strip().lower() before calling.","If you need binary units (KiB/MiB), do the division yourself instead of scale_bytes."],"exampleFix":"// before\nscale_bytes(n, 'KiB')\n// after\nscale_bytes(n, 'kb')  # or n / 1024 for binary KiB","handlingStrategy":"validation","validationCode":"unit = unit.strip().lower()\nassert unit in {'b','kb','mb','gb','tb'}, f'unsupported unit: {unit!r}'","typeGuard":"def is_valid_unit(u) -> bool:\n    return u in {'b','kb','mb','gb','tb','bytes','kilobytes','megabytes','gigabytes','terabytes'}","tryCatchPattern":"try:\n    human = scale_bytes(sz, unit)\nexcept ValueError as e:\n    if 'unit' in str(e):\n        human = scale_bytes(sz, 'b')","preventionTips":["Normalize units with .strip().lower()","Only use the documented SI unit names","Wrap custom units in your own formatter"],"tags":["python","polars","units","invalid-argument"],"backgroundTag":"invalid-enum-value","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}