Skip to content

Change "after" model validators to instance methods#601

Open
dc-almeida wants to merge 1 commit into
IAMconsortium:mainfrom
dc-almeida:fix/pydantic-model-validator-deprecation
Open

Change "after" model validators to instance methods#601
dc-almeida wants to merge 1 commit into
IAMconsortium:mainfrom
dc-almeida:fix/pydantic-model-validator-deprecation

Conversation

@dc-almeida

Copy link
Copy Markdown
Contributor

Closes #596

@dc-almeida dc-almeida added the bug Something isn't working label Jul 8, 2026
@dc-almeida
dc-almeida requested a review from danielhuppmann July 8, 2026 07:49
@dc-almeida dc-almeida self-assigned this Jul 8, 2026

@phackstock phackstock left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for tackling this one @dc-almeida. There's some more changes required though from changing the model_validator from a classmethod to a normal instance method. I've outlined it for the first occurrence of the change but it applies to all of them.

@model_validator(mode="after")
@classmethod
def check_native_or_common_regions(
cls, v: "RegionAggregationMapping"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since this is no longer a classmethod, the implementation needs to change slightly, see: https://pydantic.dev/docs/validation/latest/concepts/validators/#model-validators

Suggested change
cls, v: "RegionAggregationMapping"
self

cls, v: "RegionAggregationMapping"
) -> "RegionAggregationMapping":
"""Check that at least one of the following is provided: native or common regions."""
if not v.native_regions and not v.common_regions:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
if not self.native_regions and not self.common_regions:

@@ -201,7 +200,6 @@
return v

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
return v
return self

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update the usage of pydantic.model_validator due to deprecation warnings

2 participants