Fokko commented on code in PR #4767:
URL: https://github.com/apache/iceberg/pull/4767#discussion_r873960359


##########
python/src/iceberg/types.py:
##########
@@ -42,57 +42,63 @@ def __new__(cls, *args, **kwargs):
         return cls._instance
 
 
+@dataclass(frozen=True, eq=True, repr=True)
 class IcebergType:
-    """Base type for all Iceberg Types"""
-
-    _initialized = False
+    """Base type for all Iceberg Types
 
-    def __init__(self, type_string: str, repr_string: str):
-        self._type_string = type_string
-        self._repr_string = repr_string
-        self._initialized = True
+    Example:
+        >>> str(IcebergType())
+        'IcebergType()'
+        >>> repr(IcebergType())
+        'IcebergType()'
+    """
 
-    def __repr__(self):
-        return self._repr_string
+    type_string: str = field(init=False, repr=False)
 
     def __str__(self):
-        return self._type_string
+        if hasattr(self, "type_string"):
+            return self.type_string
+        return self.__repr__()
 
     @property
     def is_primitive(self) -> bool:
         return isinstance(self, PrimitiveType)
 
 
+@dataclass(frozen=True, eq=True)
 class PrimitiveType(IcebergType):
-    """Base class for all Iceberg Primitive Types"""
+    """Base class for all Iceberg Primitive Types
 
+    Example:
+        >>> str(PrimitiveType())
+        'PrimitiveType()'
+    """
 
+
+@dataclass(frozen=True, eq=True, repr=True)
 class FixedType(PrimitiveType):
     """A fixed data type in Iceberg.
 
     Example:
         >>> FixedType(8)
         FixedType(length=8)
-        >>> FixedType(8)==FixedType(8)
+        >>> FixedType(8) == FixedType(8)
         True
     """
 
-    _instances: Dict[int, "FixedType"] = {}
+    length: int = field()
+
+    _instances: ClassVar[Dict[int, "FixedType"]] = {}
 
     def __new__(cls, length: int):
         cls._instances[length] = cls._instances.get(length) or 
object.__new__(cls)
         return cls._instances[length]
 
-    def __init__(self, length: int):
-        if not self._initialized:
-            super().__init__(f"fixed[{length}]", f"FixedType(length={length})")
-            self._length = length
-
-    @property
-    def length(self) -> int:
-        return self._length
+    def __post_init__(self):
+        object.__setattr__(self, "type_string", f"fixed[{self.length}]")

Review Comment:
   Yes, that's exactly the reason :)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to