Skip to content

Commit a68add0

Browse files
committed
Fix deepcopy of primitive types
1 parent a29491a commit a68add0

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

pyiceberg/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def is_struct(self) -> bool:
160160
return isinstance(self, StructType)
161161

162162

163-
class PrimitiveType(IcebergRootModel[str], IcebergType, Singleton):
163+
class PrimitiveType(Singleton, IcebergRootModel[str], IcebergType):
164164
"""Base class for all Iceberg Primitive Types."""
165165

166166
root: Any = Field()

pyiceberg/utils/singleton.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,7 @@ def __new__(cls, *args, **kwargs): # type: ignore
4747
if key not in cls._instances:
4848
cls._instances[key] = super().__new__(cls)
4949
return cls._instances[key]
50+
51+
def __deepcopy__(self, memo: Dict[int, Any]) -> Any:
52+
"""Return a deep copy of the Singleton class."""
53+
return self

tests/test_types.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,3 +619,14 @@ def test_types_singleton() -> None:
619619
assert id(BooleanType()) == id(BooleanType())
620620
assert id(FixedType(22)) == id(FixedType(22))
621621
assert id(FixedType(19)) != id(FixedType(25))
622+
623+
624+
def test_deepcopy_of_singleton_fixed_type() -> None:
625+
"""FixedType is a singleton, so deepcopy should return the same instance"""
626+
from copy import deepcopy
627+
628+
list_of_fixed_types = [FixedType(22), FixedType(19)]
629+
copied_list = deepcopy(list_of_fixed_types)
630+
631+
for lhs, rhs in zip(list_of_fixed_types, copied_list):
632+
assert id(lhs) == id(rhs)

0 commit comments

Comments
 (0)