-
Notifications
You must be signed in to change notification settings - Fork 378
Pass data type as string representation to NestedField
#1860
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
861769e
to
940643b
Compare
940643b
to
87f2f2d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
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