fix(pandas): respect Optional pydantic fields as nullable in PydanticModel columns (closes #2406) - #2429
Open
feiiiiii5 wants to merge 1 commit into
Open
Conversation
…Model columns (closes unionai-oss#2406) When Config.dtype = PydanticModel(Model) is used in a DataFrameModel, _build_pydantic_column() created Column objects without checking whether the corresponding pydantic field is Optional. All columns defaulted to nullable=False, causing 'non-nullable series contains null values' errors for fields like 'rating: Optional[PositiveInt] = None'. Fix: _build_pydantic_column now inspects the pydantic field's annotation (Union with NoneType → nullable=True) and default value (None → nullable=True) to correctly set the nullable property. Supports both Pydantic v1 (__fields__) and v2 (model_fields). Regression tests cover: - Optional field is nullable in schema - Required field is not nullable in schema - Missing optional column accepted - Optional column with None values accepted - All 18 existing pydantic tests still pass Signed-off-by: fei <204683769+feiiiiii5@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Root Cause
When
Config.dtype = PydanticModel(Model)is used in aDataFrameModel,_build_pydantic_column()inpandera/api/pandas/container.pycreatedColumnobjects without checking whether the corresponding pydantic field is Optional. All columns defaulted tonullable=False, causing:for fields like
rating: Optional[PositiveInt] = None.This is a regression: worked correctly in 0.32.0 and 0.31.1.
Fix
_build_pydantic_columnnow inspects the pydantic field's annotation and default value:UnionwithNoneType(e.g.Optional[int],int | None) →nullable=TrueNone→nullable=Truenullable=False(unchanged)Supports both Pydantic v1 (
__fields__) and v2 (model_fields).Test
tests/pandas/test_pydantic_optional_nullable.py)strfield is not nullableDiff scope
2 files changed, +118/-3 lines (35 lines fix + 83 lines tests)
AI Disclosure
AI-assisted debug/initial draft/testing. Root cause identified from issue #2406 analysis. Tests verified locally. Human review of diff scope and correctness.