Skip to content

fix(dataframe-model): preserve Annotated Field metadata in DataFrameModel#2302

Closed
RedZapdos123 wants to merge 3 commits into
unionai-oss:mainfrom
RedZapdos123:fix/issue-2110-annotated-field-metadata
Closed

fix(dataframe-model): preserve Annotated Field metadata in DataFrameModel#2302
RedZapdos123 wants to merge 3 commits into
unionai-oss:mainfrom
RedZapdos123:fix/issue-2110-annotated-field-metadata

Conversation

@RedZapdos123
Copy link
Copy Markdown
Contributor

@RedZapdos123 RedZapdos123 commented Apr 21, 2026

Description:

Issue #2110 reported that metadata provided through typing.Annotated[..., pa.Field(...)] could be dropped when a DataFrameModel field omitted explicit assignment, because init_subclass created a default empty Field() instead of reusing annotation metadata.

This PR updates shared DataFrameModel annotation handling to preserve FieldInfo from Annotated metadata when initializing omitted fields. It also updates dtype-kwargs extraction to ignore FieldInfo metadata entries, so mixed Annotated[pd.DatetimeTZDtype, ..., pa.Field(...)] annotations continue to parse dtype args correctly.

It adds regression tests that cover:

  • metadata propagation from omitted Annotated[..., pa.Field(...)] fields
  • mixed Annotated extension dtype parameters plus pa.Field(...) metadata

Closes #2110.

Checklist:

  • I have reviewed all changes in this PR myself.
  • I have added relevant regression test cases for this fix.
  • I have run linting and formatting checks in WSL using prek run --files pandera/api/dataframe/model.py tests/pandas/test_model.py.
  • I have run focused tests in WSL using pytest -q tests/pandas/test_model.py -k 'annotated_field_metadata or annotated_dtype_with_annotated_field_metadata' and pytest -q tests/pandas/test_typing.py -k 'annotated_dtype or invalid_annotated_dtype or redundant_field'.

The validation screenshots of the tests run, locally on WSL:

image

Extract FieldInfo from typing.Annotated metadata when a DataFrameModel field omits explicit assignment so field properties (description/title/unique/etc.) are preserved.

Also ignore FieldInfo entries when deriving dtype kwargs from Annotated metadata, keeping dtype parameter parsing correct for extension dtypes.

Add pandas regression tests for omitted-field metadata propagation and mixed Annotated dtype + Field metadata.

Signed-off-by: Mridankan Mandal <xerontitan90@gmail.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 21, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.67%. Comparing base (1b8313c) to head (57f60cc).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2302      +/-   ##
==========================================
+ Coverage   82.64%   82.67%   +0.02%     
==========================================
  Files         179      179              
  Lines       15163    15177      +14     
==========================================
+ Hits        12532    12548      +16     
+ Misses       2631     2629       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Add a focused regression test that exercises the get_dtype_kwargs inspect.signature exception fallback path to improve patch coverage for issue unionai-oss#2110 changes.

Signed-off-by: Mridankan Mandal <xerontitan90@gmail.com>
Replace the Annotated[type(None)] fallback coverage test with a monkeypatched inspect.signature stub so the get_dtype_kwargs exception branch is exercised consistently across Python versions.

Signed-off-by: Mridankan Mandal <xerontitan90@gmail.com>
@RedZapdos123
Copy link
Copy Markdown
Contributor Author

Closed this as the issue was fixed already.

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.

Pandera cannot handle metadata through Annotated types

2 participants