Skip to content

Conversation

sunxiaojian
Copy link
Contributor

Rationale for this change

Closes #1851

Are these changes tested?

test_types.py

Are there any user-facing changes?

N/A

@sunxiaojian sunxiaojian changed the title fixed Pass in the type as a string Mar 28, 2025
@sunxiaojian sunxiaojian force-pushed the pass-type-as-string branch from 861769e to 940643b Compare March 28, 2025 08:03
@sunxiaojian sunxiaojian force-pushed the pass-type-as-string branch from 940643b to 87f2f2d Compare March 28, 2025 08:39
Copy link
Contributor

@kevinjqliu kevinjqliu left a 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


def test_nested_field_type_as_str_unsupported() -> None:
with pytest.raises(ValueError) as exc_info:
_ = (NestedField(1, "field", "list", required=True),)
Copy link
Contributor

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

Comment on lines 343 to 348
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

Copy link
Contributor

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

assert "Unsupported field type: list" in str(exc_info.value)


def test_nested_field_type_as_str_struct() -> None:
Copy link
Contributor

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

@sunxiaojian
Copy link
Contributor Author

@kevinjqliu Thanks for the review! I've fixed all the comments.

Copy link
Contributor

@kevinjqliu kevinjqliu left a 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

assert "validation errors for NestedField" in str(exc_info.value)


def test_nested_field_type_as_str_unsupported() -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
def test_nested_field_type_as_str_unsupported() -> None:
def test_nested_field_complex_type_as_str_unsupported() -> None:



@pytest.mark.parametrize("type_str, type_class", primitive_types.items())
def test_nested_field_type_as_str(type_str: str, type_class: type) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
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:

Copy link
Contributor

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__}"

Copy link
Contributor

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

@sunxiaojian sunxiaojian force-pushed the pass-type-as-string branch from 6d0cdb1 to a73dda5 Compare March 30, 2025 15:13
@sunxiaojian
Copy link
Contributor Author

@kevinjqliu Fixed all the comments. PTAL, thanks

Copy link
Contributor

@kevinjqliu kevinjqliu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@kevinjqliu kevinjqliu changed the title Pass in the type as a string Pass data type as string representation to NestedField Mar 30, 2025
@kevinjqliu kevinjqliu merged commit 4b15fb6 into apache:main Mar 30, 2025
7 checks passed
@kevinjqliu
Copy link
Contributor

Thanks for the contribution @sunxiaojian

gabeiglio pushed a commit to Netflix/iceberg-python that referenced this pull request Aug 13, 2025
<!--
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.
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pass in the type as a string

2 participants