Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pyiceberg/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion pyiceberg/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
19 changes: 19 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
TimestamptzType,
TimeType,
UUIDType,
strtobool,
)

non_parameterized_types = [
Expand Down Expand Up @@ -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)