rdblue commented on a change in pull request #4016:
URL: https://github.com/apache/iceberg/pull/4016#discussion_r807058055



##########
File path: python/src/iceberg/types.py
##########
@@ -135,75 +179,122 @@ def doc(self) -> Optional[str]:
         return self._doc
 
     @property
-    def type(self) -> Type:
+    def type(self) -> IcebergType:
         return self._type
 
-    def __repr__(self):
-        return (
-            f"NestedField(is_optional={self._is_optional}, 
field_id={self._id}, "
-            f"name={repr(self._name)}, field_type={repr(self._type)}, 
doc={repr(self._doc)})"
-        )
 
-    def __str__(self):
-        return (
-            f"{self._id}: {self._name}: {'optional' if self._is_optional else 
'required'} {self._type}" ""
-            if self._doc is None
-            else f" ({self._doc})"
+class StructType(IcebergType):
+    """A struct type in Iceberg
+
+    Example:
+        >>> StructType(
+                NestedField(True, 1, "required_field", StringType()),
+                NestedField(False, 2, "optional_field", IntegerType())
         )
+    """
 
-    def __eq__(self, other):
-        if type(self) is type(other):
-            return (
-                self.is_optional == other.is_optional
-                and self.field_id == other.field_id
-                and self.name == other.name
-                and self.doc == other.doc
-                and self.type == other.type
-            )
-        return False
+    _instances: Dict[Tuple[NestedField, ...], "StructType"] = {}
 
+    def __new__(cls, *fields: NestedField):
+        cls._instances[fields] = cls._instances.get(fields) or 
object.__new__(cls)
+        return cls._instances[fields]
 
-class StructType(Type):
-    def __init__(self, fields: list):
+    def __init__(self, *fields: NestedField):
         super().__init__(
             f"struct<{', '.join(map(str, fields))}>",
-            f"StructType(fields={repr(fields)})",
+            f"StructType{repr(fields)}",
         )
         self._fields = fields
 
     @property
-    def fields(self) -> list:
+    def fields(self) -> Tuple[NestedField, ...]:
         return self._fields
 
-    def __eq__(self, other):
-        if type(self) is type(other):
-            return self.fields == other.fields
-        return False
 
+class ListType(IcebergType):
+    """A list type in Iceberg
+
+    Example:
+        >>> ListType(element_id=3, element_type=StringType(), 
element_is_optional=True)
+        ListType(element=NestedField(is_optional=True, field_id=3, 
name='element', field_type=StringType(), doc=None))
+    """
+
+    _instances: Dict[Tuple[bool, int, IcebergType], "ListType"] = {}
+
+    def __new__(
+        cls,
+        element_id: int,
+        element_type: IcebergType,
+        element_is_optional: bool,

Review comment:
       Can we default element_is_optional to True? Same with value_is_optional 
for maps.




-- 
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