rdblue commented on a change in pull request #4016:
URL: https://github.com/apache/iceberg/pull/4016#discussion_r800232808
##########
File path: python/src/iceberg/types.py
##########
@@ -135,53 +186,54 @@ 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
- 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
+ Example:
+ >>> StructType(
+ [
+ NestedField(True, 1, "required_field", StringType()),
+ NestedField(False, 2, "optional_field", IntegerType()),
+ ]
+ """
+ _instances: Dict[Tuple[NestedField, ...], "StructType"] = {}
-class StructType(Type):
- def __init__(self, fields: list):
+ def __new__(cls, fields: List[NestedField]):
+ key = tuple(fields)
+ cls._instances[key] = cls._instances.get(key) or object.__new__(cls)
+ return cls._instances[key]
+
+ def __init__(self, fields: List[NestedField] = []):
super().__init__(
f"struct<{', '.join(map(str, fields))}>",
f"StructType(fields={repr(fields)})",
)
self._fields = fields
@property
- def fields(self) -> list:
+ def fields(self) -> List[NestedField]:
Review comment:
Should we store and return a Tuple here so that these are unmodifiable?
--
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]