Skip to content

fix: access model_fields from class instead of instance#663

Open
sene03 wants to merge 1 commit intoapache:mainfrom
sene03:fix/update-pydantic-model-fields-usage
Open

fix: access model_fields from class instead of instance#663
sene03 wants to merge 1 commit intoapache:mainfrom
sene03:fix/update-pydantic-model-fields-usage

Conversation

@sene03
Copy link
Copy Markdown

@sene03 sene03 commented Feb 17, 2026

Fixes deprecation warning in Pydantic 2.11+ where accessing model_fields on an instance is deprecated. Changed to use type(model).model_fields in model_to_dict().

Closes #652

Changes

How I tested this

Notes

Checklist

  • PR has an informative and human-readable title (this will be pulled into the release notes)
  • Changes are limited to a single goal (no scope creep)
  • Code passed the pre-commit check & code is left cleaner/nicer than when first encountered.
  • Any change in functionality is tested
  • New functions are documented (with a description, list of inputs, and expected output)
  • Placeholder code is flagged / future TODOs are captured in comments
  • Project documentation has been updated if adding/changing functionality.

Fixes deprecation warning in Pydantic 2.11+ where accessing
model_fields on an instance is deprecated. Changed to use
type(model).model_fields in model_to_dict().

Closes apache#652

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@skrawcz
Copy link
Copy Markdown
Contributor

skrawcz commented Mar 1, 2026

@sene03 I think we need a minimum version pin in pyproject.toml with this change -- do you know what that would be?

@skrawcz
Copy link
Copy Markdown
Contributor

skrawcz commented Mar 3, 2026

you need to pair this with #654

Copy link
Copy Markdown
Collaborator

@andreahlert andreahlert left a comment

Choose a reason for hiding this comment

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

Tested locally with pydantic 2.12.5. This is the correct and minimal fix - model_to_dict is the only function that accesses model_fields on an instance. The other functions (subset_model, model_from_state, _validate_keys) already receive classes via Type[BaseModel] and never triggered the deprecation. Clean fix, no unnecessary changes.

Re @skrawcz's comment about version pins: type(model).model_fields works on all pydantic 2.x - no minimum bump is strictly required for this fix. If a pin is desired for other reasons, PR #654 already handles that separately, which is the right separation of concerns.

One suggestion: add a behavioral test that asserts no DeprecationWarning is emitted from model_to_dict, rather than just testing the output. This would catch regressions if someone reverts to instance access:

import warnings

def test_model_to_dict_no_deprecation_warning():
    model = OriginalModel(foo=1, bar="bar", nested=NestedModel(nested_field1=1))
    with warnings.catch_warnings():
        warnings.simplefilter("error")
        result = model_to_dict(model)
    assert "foo" in result

@andreahlert andreahlert added kind/bug Something is broken area/typing Mypy, type hints, pydantic labels Mar 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/typing Mypy, type hints, pydantic kind/bug Something is broken

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update pydantic usage before 3.0

3 participants