stubgen: emit synthesized __init__ for dataclass / Pydantic models#3306
Draft
tobyh-canva wants to merge 1 commit intofacebook:mainfacebook/pyrefly:mainfrom
tobyh-canva:stubgen/synthesized-class-inittobyh-canva/pyrefly:stubgen/synthesized-class-initCopy head branch name to clipboard
Draft
stubgen: emit synthesized __init__ for dataclass / Pydantic models#3306tobyh-canva wants to merge 1 commit intofacebook:mainfacebook/pyrefly:mainfrom tobyh-canva:stubgen/synthesized-class-inittobyh-canva/pyrefly:stubgen/synthesized-class-initCopy head branch name to clipboard
__init__ for dataclass / Pydantic models#3306tobyh-canva wants to merge 1 commit intofacebook:mainfacebook/pyrefly:mainfrom
tobyh-canva:stubgen/synthesized-class-inittobyh-canva/pyrefly:stubgen/synthesized-class-initCopy head branch name to clipboard
Conversation
Pydantic models default to lax init parameter types and extra **kwargs, which made .pyi stubs noisy and unlike the source annotations. Stub extraction now reuses the checker's synthesized __init__, overlays class-body annotation text on matching parameters (including validation aliases), omits **kwargs from the stub, and forces alias-only init parameters to be keyword-only. Exposes ClassField::dataclass_flags_of to stubgen and treats re-exported BaseModel as a Pydantic model for metadata. Alternative to trimming field()/Field() on the class body (facebook#3223). Co-authored-by: Cursor <cursoragent@cursor.com>
a48a83e to
69dd83e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
As in #3221 / #3223, stub output for dataclass and Pydantic models needs to reflect how instances are constructed (defaults,
kw_only, validation aliases, etc.) without turning.pyifiles into a dump offield()/Field()kwargs.Intent
Alternative to #3223. Rather than primarily re-emitting a reduced
field()/Field()on the class line, this approach injects a synthesized__init__derived from the checker’s existing dataclass / Pydantic init plumbing, then aligns annotations with the authored class body where parameter names match (including alias-only init).Changes
extract.rs: Resolve synthesized__init__fromClassSynthesizedFields, mapParam→StubParam, merge AST annotation text for init params (skippingInitVar), drop**kwargsfrom emitted synthetic inits, preserve / improve class-body ordering andfield/Fieldelision behavior, import pruning for unused Pydantic helpers.emit.rs: Multilinedef __init__when the stub has many parameters.dataclass.rs: Validation-alias-only init parameters are keyword-only (*, id: ...), matching runtime and stub tests.pydantic.rs/class_field.rs: Treatpydantic.BaseModelre-exports like other BaseModel bases; exposeClassField::dataclass_flags_ofto stubgen.mod.rstests: Minimal on-diskpydanticshim + glob onlyinput.pyso temp package imports do not overwrite stubgen output.Testing
cargo test -p pyrefly stubgen::tests::Made with Cursor