Skip to content

Do not implicitly convert after model validators to class methods#11957

Merged
Viicos merged 2 commits intomainfrom
no-classmethod-aftervalidator
Jun 12, 2025
Merged

Do not implicitly convert after model validators to class methods#11957
Viicos merged 2 commits intomainfrom
no-classmethod-aftervalidator

Conversation

@Viicos
Copy link
Copy Markdown
Member

@Viicos Viicos commented Jun 10, 2025

Change Summary

Fixes #11905, closes #11906.

Technically this is a breaking change since the signature from the added test was unexpectedly working, but this wasn't documented at all and the fix is straightforward. What do you think?

Need to document the change (see #12110) in blog post and changelog.

Related issue number

Checklist

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Tests pass on CI
  • Documentation reflects the changes where applicable
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

@github-actions github-actions bot added the relnotes-fix Used for bugfixes. label Jun 10, 2025
@Viicos Viicos added relnotes-change Used for changes to existing functionality which don't have a better categorization. third-party-tests Add this label on a PR to trigger 3rd party tests and removed relnotes-fix Used for bugfixes. labels Jun 10, 2025
@Viicos Viicos closed this Jun 10, 2025
@Viicos Viicos reopened this Jun 10, 2025
@Viicos Viicos force-pushed the no-classmethod-aftervalidator branch from b2ee66f to 7620df4 Compare June 10, 2025 13:28
@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages bot commented Jun 10, 2025

Deploying pydantic-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 8a074b4
Status:⚡️  Build in progress...

View logs

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Jun 10, 2025

CodSpeed Performance Report

Merging #11957 will not alter performance

Comparing no-classmethod-aftervalidator (8a074b4) with main (bcd10f7)

Summary

✅ 46 untouched benchmarks

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Jun 10, 2025

Coverage report

This PR does not seem to contain any modification to coverable code.

Comment thread pydantic/functional_validators.py
@Viicos Viicos enabled auto-merge (squash) June 12, 2025 19:43
@Viicos Viicos merged commit f0e1d04 into main Jun 12, 2025
85 of 86 checks passed
@Viicos Viicos deleted the no-classmethod-aftervalidator branch June 12, 2025 19:49
@Viicos Viicos added the needs-blogpost-entry This PR needs to be documented in the release notes blog post label Aug 1, 2025
psychedelicious added a commit to invoke-ai/InvokeAI that referenced this pull request Oct 8, 2025
Fixes a test failure introduced by
pydantic/pydantic#11957

TL;DR: "after" model validators should be instance methods, not class
methods. Batch model updated to use an instance method, which fixes the
failing test.
psychedelicious added a commit to invoke-ai/InvokeAI that referenced this pull request Oct 8, 2025
Fixes a test failure introduced by
pydantic/pydantic#11957

TL;DR: "after" model validators should be instance methods, not class
methods. Batch model updated to use an instance method, which fixes the
failing test.
Copy link
Copy Markdown

@antazoey antazoey left a comment

Choose a reason for hiding this comment

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

The test is misleading with its use of cls instead of self for "after" mode

@model_validator(mode='after')
# This used to be converted into a classmethod, resulting
# in this inconsistent signature still accepted:
def validator(cls, model, info): ...
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Shouldn't this be self instead of cls in this case?

Copy link
Copy Markdown
Member Author

@Viicos Viicos Oct 9, 2025

Choose a reason for hiding this comment

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

This is wrapped in a pytest.raises() context manager, and was meant to test that this particular invalid signature is now rejected.

@martincpt
Copy link
Copy Markdown

When will be new release?

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

Labels

needs-blogpost-entry This PR needs to be documented in the release notes blog post relnotes-change Used for changes to existing functionality which don't have a better categorization. third-party-tests Add this label on a PR to trigger 3rd party tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

incorrect model_validator(mode="after") method definition?

4 participants