Skip to content

Commit 84ab979

Browse files
MehulBatraFokko
andauthored
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 <[email protected]> * revamp unit_test * revamp unit_test --------- Co-authored-by: Fokko Driesprong <[email protected]>
1 parent 98a61da commit 84ab979

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

pyiceberg/types.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,22 @@ def _parse_fixed_type(fixed: Any) -> int:
9292
return fixed
9393

9494

95+
def strtobool(val: str) -> bool:
96+
"""Convert a string representation of truth to true (1) or false (0).
97+
98+
True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
99+
are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if
100+
'val' is anything else.
101+
"""
102+
val = val.lower()
103+
if val in ("y", "yes", "t", "true", "on", "1"):
104+
return True
105+
elif val in ("n", "no", "f", "false", "off", "0"):
106+
return False
107+
else:
108+
raise ValueError(f"Invalid truth value: {val!r}")
109+
110+
95111
class IcebergType(IcebergBaseModel):
96112
"""Base type for all Iceberg Types.
97113

pyiceberg/utils/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
# under the License.
1717
import logging
1818
import os
19-
from distutils.util import strtobool
2019
from typing import List, Optional
2120

2221
import strictyaml
2322

2423
from pyiceberg.typedef import UTF8, FrozenDict, RecursiveDict
24+
from pyiceberg.types import strtobool
2525

2626
PYICEBERG = "pyiceberg_"
2727
DEFAULT = "default"

tests/test_types.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
TimestamptzType,
4444
TimeType,
4545
UUIDType,
46+
strtobool,
4647
)
4748

4849
non_parameterized_types = [
@@ -630,3 +631,21 @@ def test_deepcopy_of_singleton_fixed_type() -> None:
630631

631632
for lhs, rhs in zip(list_of_fixed_types, copied_list):
632633
assert id(lhs) == id(rhs)
634+
635+
636+
def test_strtobool() -> None:
637+
# Values that should return True
638+
true_values = ["y", "yes", "t", "true", "on", "1"]
639+
for val in true_values:
640+
assert strtobool(val) is True, f"Expected True for value: {val}"
641+
642+
# Values that should return False
643+
false_values = ["n", "no", "f", "false", "off", "0"]
644+
for val in false_values:
645+
assert strtobool(val) is False, f"Expected False for value: {val}"
646+
647+
# Values that should raise ValueError
648+
invalid_values = ["maybe", "2", "trueish", "falseish", "", " "]
649+
for val in invalid_values:
650+
with pytest.raises(ValueError, match=f"Invalid truth value: {val!r}"):
651+
strtobool(val)

0 commit comments

Comments
 (0)