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


##########
python/src/iceberg/types.py:
##########
@@ -209,25 +192,24 @@ class StructType(IcebergType):
         'struct<1: required_field: optional string, 2: optional_field: 
optional int>'
     """
 
-    _instances: Dict[Tuple[NestedField, ...], "StructType"] = {}
+    fields: List[NestedField] = field()
+
+    _instances: ClassVar[Dict[Tuple[NestedField, ...], "StructType"]] = {}
 
-    def __new__(cls, *fields: NestedField):
+    def __new__(cls, *fields: NestedField, **kwargs):
+        if "fields" in kwargs:

Review Comment:
   There seems to be a subtle behavioral change that requires this. Without 
this logic, the following test is failing:
   ```python
   assert str(type_var) == str(eval(repr(type_var)))
   ```
   
   When we dig into this:
   ```python
   >> repr(type_var)
   StructType(
       fields=(
           NestedField(field_id=1, name='optional_field', 
field_type=IntegerType(), is_optional=True), 
           NestedField(field_id=2, name='required_field', 
field_type=FixedType(length=5), is_optional=False), 
           ...
       )
   )
   ```
   It looks like it tries to initialize it again using a keyword argument:
   ```python
   >> eval(repr(type_var))
   {TypeError}__new__() got an unexpected keyword argument 'fields'
   ```
   Instead of variable unpacking.
   
   Using this logic we give priority to the keyword argument:
   ```
   StructField(
       NestedField(field_id=1, name='optional_field', field_type=IntegerType(), 
is_optional=True), 
       NestedField(field_id=2, name='required_field', 
field_type=FixedType(length=5), is_optional=False), 
   )
   or
   StructField(
       fields=(
           NestedField(field_id=1, name='optional_field', 
field_type=IntegerType(), is_optional=True), 
           NestedField(field_id=2, name='required_field', 
field_type=FixedType(length=5), is_optional=False), 
       )
   )
   ```
   I've added an additional check to see if fields is empty.



##########
python/src/iceberg/types.py:
##########
@@ -209,25 +192,24 @@ class StructType(IcebergType):
         'struct<1: required_field: optional string, 2: optional_field: 
optional int>'
     """
 
-    _instances: Dict[Tuple[NestedField, ...], "StructType"] = {}
+    fields: List[NestedField] = field()
+
+    _instances: ClassVar[Dict[Tuple[NestedField, ...], "StructType"]] = {}
 
-    def __new__(cls, *fields: NestedField):
+    def __new__(cls, *fields: NestedField, **kwargs):
+        if "fields" in kwargs:

Review Comment:
   There seems to be a subtle behavioral change that requires this. Without 
this logic, the following test is failing:
   ```python
   assert str(type_var) == str(eval(repr(type_var)))
   ```
   
   When we dig into this:
   ```python
   >> repr(type_var)
   StructType(
       fields=(
           NestedField(field_id=1, name='optional_field', 
field_type=IntegerType(), is_optional=True), 
           NestedField(field_id=2, name='required_field', 
field_type=FixedType(length=5), is_optional=False), 
           ...
       )
   )
   ```
   It looks like it tries to initialize it again using a keyword argument:
   ```python
   >> eval(repr(type_var))
   {TypeError}__new__() got an unexpected keyword argument 'fields'
   ```
   Instead of variable unpacking.
   
   Using this logic we give priority to the keyword argument:
   ```python
   StructField(
       NestedField(field_id=1, name='optional_field', field_type=IntegerType(), 
is_optional=True), 
       NestedField(field_id=2, name='required_field', 
field_type=FixedType(length=5), is_optional=False), 
   )
   or
   StructField(
       fields=(
           NestedField(field_id=1, name='optional_field', 
field_type=IntegerType(), is_optional=True), 
           NestedField(field_id=2, name='required_field', 
field_type=FixedType(length=5), is_optional=False), 
       )
   )
   ```
   I've added an additional check to see if fields is empty.



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