From 0914e524533cefbaf2bb193a49eb0c50821d0202 Mon Sep 17 00:00:00 2001 From: Sreekant Baheti <60787859+Sreekant13@users.noreply.github.com> Date: Wed, 1 Jul 2026 16:05:26 -0700 Subject: [PATCH] Accept two-letter byte units (KB/MB/GB/TB/PB) in parse_size The `hf cache ls --filter` help and docs advertise size thresholds like `size>1GB` and `size>10MB`, and the CLI's own --help example uses `size>1GB`. But parse_size only accepted single-letter units, so those documented filters raised `ValueError: Unknown unit 'gb'` while `size>30g` worked. The sibling parse_size_to_int() already supports KB/MB/GB/TB. Add the two-letter aliases so the advertised filters work, and cover them in the parsing tests (3KB/10MB/1GB/2TB were previously untested or wrongly marked invalid). --- src/huggingface_hub/utils/_parsing.py | 5 +++++ tests/test_utils_parsing.py | 5 ++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/huggingface_hub/utils/_parsing.py b/src/huggingface_hub/utils/_parsing.py index d3a2729a639..eeddee70f64 100644 --- a/src/huggingface_hub/utils/_parsing.py +++ b/src/huggingface_hub/utils/_parsing.py @@ -21,10 +21,15 @@ BYTE_UNITS: dict[str, int] = { "k": 1_000, + "kb": 1_000, "m": 1_000_000, + "mb": 1_000_000, "g": 1_000_000_000, + "gb": 1_000_000_000, "t": 1_000_000_000_000, + "tb": 1_000_000_000_000, "p": 1_000_000_000_000_000, + "pb": 1_000_000_000_000_000, } TIME_UNITS: dict[str, int] = { diff --git a/tests/test_utils_parsing.py b/tests/test_utils_parsing.py index 124c9584d5e..d39a1cfb95e 100644 --- a/tests/test_utils_parsing.py +++ b/tests/test_utils_parsing.py @@ -13,6 +13,10 @@ ("5M", 5_000_000), ("2G", 2_000_000_000), ("1T", 1_000_000_000_000), + ("3KB", 3_000), + ("10MB", 10_000_000), + ("1GB", 1_000_000_000), + ("2TB", 2_000_000_000_000), ("0", 0), ], ) @@ -24,7 +28,6 @@ def test_parse_size_valid(value, expected): "value", [ "1.5G", - "3KB", "-5M", "10X", "abc",