This is an automated email from the ASF dual-hosted git repository. fokko pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/iceberg-python.git
The following commit(s) were added to refs/heads/main by this push: new 84ab9791 `strtobool` support for Python 3.12 (#880) 84ab9791 is described below commit 84ab9791a65dc60a38f886193c93519b5bce5d5f Author: Mehul Batra <66407733+mehulba...@users.noreply.github.com> AuthorDate: Tue Jul 2 17:09:42 2024 +0530 `strtobool` support for Python 3.12 (#880) * pyiceberg strtobool function to replace distutil strtobool function * unit_test for strtobool * Update pyiceberg/types.py Co-authored-by: Fokko Driesprong <fo...@apache.org> * revamp unit_test * revamp unit_test --------- Co-authored-by: Fokko Driesprong <fo...@apache.org> --- pyiceberg/types.py | 16 ++++++++++++++++ pyiceberg/utils/config.py | 2 +- tests/test_types.py | 19 +++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/pyiceberg/types.py b/pyiceberg/types.py index e92056e0..cd662c73 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 5eb9cfaa..51ab200e 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 0ffb1d07..1e386bb7 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -43,6 +43,7 @@ from pyiceberg.types import ( 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)