Skip to content

Pydantic v2.11 model_fields-on-instance deprecation (models.py:117) #98

Description

@tavateva

Split out from #61 as a pre-existing warning that predates PR #96 and is out-of-scope for the coverage work.

Symptom

Observed in the test_coverage CI run (e.g. https://github.com/ChengLabResearch/ouroboros/actions/runs/28622389980):

test/helpers/test_models.py::test_model_with_json
  .../python/ouroboros/helpers/models.py:117: PydanticDeprecatedSince211: Accessing the 'model_fields' attribute on the instance is deprecated. Instead, you should access this attribute from the model class. Deprecated in Pydantic V2.11 to be removed in V3.0.
    for field in self.model_fields.keys():

Cause

copy_values_from_other (models.py:117) accesses self.model_fields.keys() on a BaseModel instance. Pydantic V2.11 deprecated instance-level model_fields; V3.0 will remove it entirely.

def copy_values_from_other(self: BaseModel, other: BaseModel):
    for field in self.model_fields.keys():          # <-- deprecated
        setattr(self, field, getattr(other, field))

Fix

Access model_fields off the class instead:

def copy_values_from_other(self: BaseModel, other: BaseModel):
    for field in type(self).model_fields.keys():
        setattr(self, field, getattr(other, field))

Or explicitly self.__class__.model_fields. Either satisfies Pydantic V3.0 while keeping subclass polymorphism.

Acceptance

  • models.py:117 uses class-level model_fields access.
  • test_coverage workflow run for the branch shows the PydanticDeprecatedSince211 warning gone.
  • Ripgrep confirms no other instance-level model_fields / model_fields_set / model_computed_fields access remains in python/ouroboros/.

Verification

cd python
poetry run pytest test/helpers/test_models.py -W error::pydantic.PydanticDeprecatedSince211

Should exit 0 after the fix (currently errors on the deprecation).

Out of scope

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions