[stubgen] Always generate ellipsis for class attr with initial value#20985
Open
michaelm-openai wants to merge 1 commit intopython:masterfrom
Open
Conversation
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.
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.