diff --git a/pyiceberg/types.py b/pyiceberg/types.py index e92056e082..cd662c7387 100644 --- a/pyiceberg/types.py +++ b/pyiceberg/types.py @@ -92,6 +92,22 @@ def _parse_fixed_type(fixed: Any) -> int: return fixed +def strtobool(val: str) -> bool: + """Convert a string representation of truth to true (1) or false (0). + + True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values + are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if + 'val' is anything else. + """ + val = val.lower() + if val in ("y", "yes", "t", "true", "on", "1"): + return True + elif val in ("n", "no", "f", "false", "off", "0"): + return False + else: + raise ValueError(f"Invalid truth value: {val!r}") + + class IcebergType(IcebergBaseModel): """Base type for all Iceberg Types. diff --git a/pyiceberg/utils/config.py b/pyiceberg/utils/config.py index 5eb9cfaa66..51ab200e10 100644 --- a/pyiceberg/utils/config.py +++ b/pyiceberg/utils/config.py @@ -16,12 +16,12 @@ # under the License. import logging import os -from distutils.util import strtobool from typing import List, Optional import strictyaml from pyiceberg.typedef import UTF8, FrozenDict, RecursiveDict +from pyiceberg.types import strtobool PYICEBERG = "pyiceberg_" DEFAULT = "default" diff --git a/tests/test_types.py b/tests/test_types.py index 0ffb1d0730..1e386bb748 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -43,6 +43,7 @@ TimestamptzType, TimeType, UUIDType, + strtobool, ) non_parameterized_types = [ @@ -630,3 +631,21 @@ def test_deepcopy_of_singleton_fixed_type() -> None: for lhs, rhs in zip(list_of_fixed_types, copied_list): assert id(lhs) == id(rhs) + + +def test_strtobool() -> None: + # Values that should return True + true_values = ["y", "yes", "t", "true", "on", "1"] + for val in true_values: + assert strtobool(val) is True, f"Expected True for value: {val}" + + # Values that should return False + false_values = ["n", "no", "f", "false", "off", "0"] + for val in false_values: + assert strtobool(val) is False, f"Expected False for value: {val}" + + # Values that should raise ValueError + invalid_values = ["maybe", "2", "trueish", "falseish", "", " "] + for val in invalid_values: + with pytest.raises(ValueError, match=f"Invalid truth value: {val!r}"): + strtobool(val)