Skip to content

Commit 88c4bad

Browse files
authored
Add _missing_ to make FileFormat case insensitive (#1411)
* Add _missing_ to FileFormat Enum to make it case insensitive * Combine the manifest test to existing test_manifest.py file * Fix linting
1 parent d82f8f7 commit 88c4bad

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

pyiceberg/manifest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
Optional,
3131
Tuple,
3232
Type,
33+
Union,
3334
)
3435

3536
from cachetools import LRUCache, cached
@@ -97,6 +98,13 @@ class FileFormat(str, Enum):
9798
PARQUET = "PARQUET"
9899
ORC = "ORC"
99100

101+
@classmethod
102+
def _missing_(cls, value: object) -> Union[None, str]:
103+
for member in cls:
104+
if member.value == str(value).upper():
105+
return member
106+
return None
107+
100108
def __repr__(self) -> str:
101109
"""Return the string representation of the FileFormat class."""
102110
return f"FileFormat.{self.name}"

tests/utils/test_manifest.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,3 +604,26 @@ def test_write_manifest_list(
604604
assert entry.file_sequence_number == 0 if format_version == 1 else 3
605605
assert entry.snapshot_id == 8744736658442914487
606606
assert entry.status == ManifestEntryStatus.ADDED
607+
608+
609+
@pytest.mark.parametrize(
610+
"raw_file_format,expected_file_format",
611+
[
612+
("avro", FileFormat("AVRO")),
613+
("AVRO", FileFormat("AVRO")),
614+
("parquet", FileFormat("PARQUET")),
615+
("PARQUET", FileFormat("PARQUET")),
616+
("orc", FileFormat("ORC")),
617+
("ORC", FileFormat("ORC")),
618+
("NOT_EXISTS", None),
619+
],
620+
)
621+
def test_file_format_case_insensitive(raw_file_format: str, expected_file_format: FileFormat) -> None:
622+
if expected_file_format:
623+
parsed_file_format = FileFormat(raw_file_format)
624+
assert parsed_file_format == expected_file_format, (
625+
f"File format {raw_file_format}: {parsed_file_format} != {expected_file_format}"
626+
)
627+
else:
628+
with pytest.raises(ValueError):
629+
_ = FileFormat(raw_file_format)

0 commit comments

Comments
 (0)