Skip to content

fix Support Pydantic validators in Annotated fields #4256 - #4282

Closed
asukaminato0721 wants to merge 1 commit into
meta:mainfrom
asukaminato0721:4256
Closed

asukaminato0721 wants to merge 1 commit into
meta:mainfrom
asukaminato0721:4256

Conversation

@asukaminato0721

@asukaminato0721 asukaminato0721 commented Jul 24, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes #4256

BeforeValidator and PlainValidator constructor input inference.
Support across reusable TypeAlias annotations.
Correct right-to-left ordering for chained before validators, matching Pydantic's behavior (https://pydantic.dev/docs/validation/latest/concepts/validators/#ordering-of-validators).
Support for fields with defaults while preserving the stored field type.

Test Plan

add test

@github-actions

This comment has been minimized.

@meta-codesync

meta-codesync Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D114356273. (Because this pull request was imported automatically, there will not be any future comments.)

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@rchen152

rchen152 commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Unfortunately, I think this has the same issue as #3798 - it works through converter_param, which also affects attribute assignment, when we should only modify __init__ parameter annotations. Some other notes:

  • The pydantic test stubs don't have functional_validators.py, so the tests don't exercise the same code paths that the real pydantic library would.
  • The first argument of every call in Annotated metadata now gets inferred a second time, which is unnecessary duplicate work.
  • AnnotatedMetadata::callee_name stores just the plain name, so any callee named BeforeValidator gets this pydantic-specific behavior.

IMO a more promising way forward would be to modify just __init__ parameter annotations. Note that direct Annotated[T, BeforeValidator(f)] fields already get an Any __init__ parameter (the reason this doesn't work properly in our tests is the lack of a functional_validators.py stub), so what's still missing is validators behind type aliases.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support Pydantic validators in Annotated fields

4 participants