Skip to content

[stubgen] Always generate ellipsis for class attr with initial value#20985

Open
michaelm-openai wants to merge 1 commit intopython:masterfrom
michaelm-openai:michaelm/stubgen-always-respect-class-attr-initializer
Open

[stubgen] Always generate ellipsis for class attr with initial value#20985
michaelm-openai wants to merge 1 commit intopython:masterfrom
michaelm-openai:michaelm/stubgen-always-respect-class-attr-initializer

Conversation

@michaelm-openai
Copy link
Contributor

Fixes #16968

Currently semantic analysis is needed to detect whether a class is affected by dataclass transformation, which is the only case where where = ... is emitted for class attributes with default values besides named tuples.

This means that even if one adds explicit types everywhere they can in a file, they cannot rely on purely parse-based stub generation, because there is no way to explicitly specify a class attribute as needing an initializer in type stubs.

One approach could be to add a way to explicitly make that specification, but a much simpler approach is to always emit an ellipsis initializer if a class attribute is defined with an initial value. During typecheck, the ellipsis initializer would just have no effect if on a class not affected by dataclass transformation.

This idea is validated by Pyright, which already has this behavior using it's stub generation feature, i.e. pyright --createstub.

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.

Incorrect stub generation for subclasses of pydantic.BaseModel

1 participant