Pass data type as string representation to NestedField#1860
Pass data type as string representation to NestedField#1860kevinjqliu merged 3 commits intoapache:mainfrom
NestedField#1860Conversation
861769e to
940643b
Compare
940643b to
87f2f2d
Compare
kevinjqliu
left a comment
There was a problem hiding this comment.
Thank you for the PR! I've added a few comments
tests/test_types.py
Outdated
|
|
||
| def test_nested_field_type_as_str_unsupported() -> None: | ||
| with pytest.raises(ValueError) as exc_info: | ||
| _ = (NestedField(1, "field", "list", required=True),) |
There was a problem hiding this comment.
nit: lets also test other complex type list/map/struct
pyiceberg/types.py
Outdated
| if isinstance(data["type"], str): | ||
| try: | ||
| data["type"] = IcebergType.handle_primitive_type(data["type"], None) | ||
| except ValueError as e: | ||
| raise ValueError(f"Unsupported field type: {data['type']}.") from e | ||
|
|
There was a problem hiding this comment.
nit: what do you think about using a pydantic field validator here?
https://docs.pydantic.dev/latest/concepts/validators/#field-before-validator
@validator("field_type", pre=True)
def convert_field_type(cls, v):
"""Convert string values into IcebergType instances."""
if isinstance(v, str):
try:
return IcebergType.handle_primitive_type(v, None)
except ValueError as e:
raise ValueError(f"Unsupported field type: {v}") from e
return v # If already an IcebergType, keep as is
tests/test_types.py
Outdated
| assert "Unsupported field type: list" in str(exc_info.value) | ||
|
|
||
|
|
||
| def test_nested_field_type_as_str_struct() -> None: |
There was a problem hiding this comment.
nit: include other types that are part of handle_primitive_type here as well
|
@kevinjqliu Thanks for the review! I've fixed all the comments. |
kevinjqliu
left a comment
There was a problem hiding this comment.
LGTM! I left a few nit comments to improve test readability
tests/test_types.py
Outdated
| assert "validation errors for NestedField" in str(exc_info.value) | ||
|
|
||
|
|
||
| def test_nested_field_type_as_str_unsupported() -> None: |
There was a problem hiding this comment.
nit:
| def test_nested_field_type_as_str_unsupported() -> None: | |
| def test_nested_field_complex_type_as_str_unsupported() -> None: |
tests/test_types.py
Outdated
|
|
||
|
|
||
| @pytest.mark.parametrize("type_str, type_class", primitive_types.items()) | ||
| def test_nested_field_type_as_str(type_str: str, type_class: type) -> None: |
There was a problem hiding this comment.
nit:
| def test_nested_field_type_as_str(type_str: str, type_class: type) -> None: | |
| def test_nested_field_primitive_type_as_str(type_str: str, type_class: type) -> None: |
There was a problem hiding this comment.
nit: parametrizing the test actually is slower compared to just running a for loop into the function
for type_str, type_class in primitive_types.items():
field_var = NestedField(
1,
"field",
type_str,
required=True,
)
assert isinstance(
field_var.field_type, type_class
), f"Expected {type_class.__name__}, got {field_var.field_type.__class__.__name__}"
| assert isinstance( | ||
| field_var.field_type, type_class | ||
| ), f"Expected {type_class.__name__}, got {field_var.field_type.__class__.__name__}" | ||
|
|
There was a problem hiding this comment.
nit can we add a test for this case too?
except ValueError as e:
raise ValueError(f"Unsupported field type: '{v}'") from e
6d0cdb1 to
a73dda5
Compare
|
@kevinjqliu Fixed all the comments. PTAL, thanks |
NestedField
|
Thanks for the contribution @sunxiaojian |
<!--
Thanks for opening a pull request!
-->
<!-- In the case this PR will resolve an issue, please replace
${GITHUB_ISSUE_ID} below with the actual Github issue id. -->
<!-- Closes #${GITHUB_ISSUE_ID} -->
# Rationale for this change
Closes apache#1851
# Are these changes tested?
test_types.py
# Are there any user-facing changes?
N/A
<!-- In the case of user-facing changes, please add the changelog label.
-->
Rationale for this change
Closes #1851
Are these changes tested?
test_types.py
Are there any user-facing changes?
N/A