Skip to content

Fix Pydantic 2.11 model_fields instance-access deprecation - #190

Open
edwardxtg wants to merge 1 commit into
mainfrom
fix-pydantic-model-fields-deprecation
Open

edwardxtg wants to merge 1 commit into
mainfrom
fix-pydantic-model-fields-deprecation

Conversation

@edwardxtg

Copy link
Copy Markdown
Contributor

What

Pydantic 2.11 deprecated accessing model_fields on a model instance (and Pydantic 3.0 removes it). This replaces the 8 self.model_fields reads in the schema compose() methods with type(self).model_fields.

Files: commodity.py, impact.py, region.py (×2), storage.py, technology.py (×2), trade.py.

Why it's safe

  • model_fields is a class-level attribute — class vs instance access returns the same mapping, so behaviour is identical.
  • Every affected loop only uses the field name (then getattr(self, field)); the FieldInfo value is discarded.
  • The cls.model_fields sites (in classmethods) were already correct and are left untouched.

Verification

  • pytest tests/test_construction/35 passed, 2 skipped; the PydanticDeprecatedSince211: … 'model_fields' … on the instance warnings (previously emitted on every model construction) are gone.
  • grep -rn 'self\.model_fields' tz/ → no results.

Scope

Standalone cleanup, independent of the in-flight linopy 0.7 migration (disjoint files). Completes Pydantic-3.0 readiness for this symbol — no other deprecated instance-access (__fields__, model_computed_fields) exists in the package.

🤖 Generated with Claude Code

Pydantic 2.11 deprecated accessing `model_fields` on a model instance
(removed in Pydantic 3.0). Replace the 8 `self.model_fields` reads in the
schema `compose()` methods with `type(self).model_fields`.

`model_fields` is a class-level attribute, so class vs instance access
returns the same mapping and behaviour is unchanged; these loops only use
the field names (via `getattr(self, field)`). The `cls.model_fields` sites
in classmethods were already correct and are left untouched.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.

1 participant