Fokko commented on code in PR #6525:
URL: https://github.com/apache/iceberg/pull/6525#discussion_r1067871130
##########
python/pyiceberg/typedef.py:
##########
@@ -126,30 +135,41 @@ def json(self, exclude_none: bool = True, exclude:
Optional[Set[str]] = None, by
class PydanticStruct(IcebergBaseModel):
+ _position_to_field_name: Dict[int, str] = PrivateAttr()
+ _field_name_to_pydantic_field: Dict[str, Field] = PrivateAttr()
+
class Config:
frozen = False
- def __setitem__(self, pos: int, value: Any) -> None:
- positions = list(self.__fields__.values())
- field = positions[pos]
- if value is None:
- if field.default is not None:
- value = field.default
- elif field.default_factory is not None:
- value = field.default_factory()
+ @staticmethod
+ def _get_default_field_value(field: Field) -> Optional[Any]:
+ if field.default is not None:
+ return field.default
+ elif field.default_factory is not None:
+ return field.default_factory()
+ else:
+ return None
+
+ def set_record_schema(self, record_schema: StructType) -> None:
+ self._field_name_to_pydantic_field = {field.name: field for field in
self.__fields__.values()}
+ self._position_to_field_name = {idx: field.name for idx, field in
enumerate(record_schema.fields)}
+ for name, field in self.__fields__.items():
+ setattr(self, name, PydanticStruct._get_default_field_value(field))
Review Comment:
We can remove them here as well. I thought there was a difference between
the Pydantic Record and the generic one. I was under the assumption that the
fields would be pre-allocated for the Pydantic one, and this is true if you
initialize them through `PydanticRecord()`, but we use
`PydanticRecord.construct()` which creates an empty record, so the attributes
aren't set:
```python
➜ python git:(fd-refactor-loading-manifests) ✗ python3
Python 3.10.9 (main, Dec 15 2022, 17:11:09) [Clang 14.0.0
(clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class Record():
... c: str
... def __init__(self):
... self.a = 'a'
... self.b = 'b'
...
>>> Record().a
'a'
>>> Record().b
'b'
>>> Record().c
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Record' object has no attribute 'c'
```
And using Pydantic:
```python
>>> from pydantic import BaseModel, Field, Extra
>>>
>>> class PydanticRecord(BaseModel):
... c: str = Field()
... class Config:
... extra = Extra.allow
...
>>> PydanticRecord.construct(a='a', b='b')
PydanticRecord(a='a', b='b')
>>> PydanticRecord.construct(a='a', b='b').c
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'PydanticRecord' object has no attribute 'c'
```
--
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]