Classify __slots__ as class variables - #21886
Open
XuehaiPan wants to merge 2 commits into
Open
Conversation
Mark ordinary and plugin-generated __slots__ symbols as class variables so inheritance does not produce spurious instance/class variable override errors. Add regression coverage for ordinary classes, dataclasses, and attrs classes.
This comment has been minimized.
This comment has been minimized.
Keep __slots__ classified as a class variable for type checking, but do not attach it to native classes at runtime. Native classes use their own layout, and exposing inherited slots prevents compiled subclasses from being created. Add a compiled inheritance regression that reproduces the CI failure.
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
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.
Python defines
__slots__on the class, but mypy currently classifies generated slots as instance variables. This produces an inconsistent override error when a subclass declares an explicitly typed empty__slots__:Before this change, the ignore is required to suppress:
The same inconsistency applies to ordinary
__slots__and attrs-generated slots.This PR classifies ordinary and plugin-generated
__slots__symbols as class variables while preserving the existing special__slots__type and layout handling. It adds inheritance regression coverage for ordinary classes, dataclasses, and attrs classes.Test plan:
__slots__regressions: 3 passedcheck-slots.test,check-dataclasses.test,check-plugin-attrs.test, andcheck-classvar.test: 364 passedpython3 -m pre_commit run --all-files: all hooks passed