stubtest: distinguish metaclass attributes from class attributes#18314
Merged
JelleZijlstra merged 3 commits intopython:masterfrom Dec 20, 2024
Merged
stubtest: distinguish metaclass attributes from class attributes#18314JelleZijlstra merged 3 commits intopython:masterfrom
JelleZijlstra merged 3 commits intopython:masterfrom
Conversation
If the runtime attribute of a class is actually from the metaclass, consider it to be MISSING at runtime.
mypy/stubtest.py
Outdated
|
|
||
| # If it came from the metaclass, consider the runtime_attr to be MISSING | ||
| # for a more accurate message | ||
| if runtime_attr is not MISSING and type(runtime) != runtime: |
Member
There was a problem hiding this comment.
Should != be is not?
And can you add a test that changes behavior with this patch?
Contributor
Author
There was a problem hiding this comment.
I think you're right. I'll have a test ready in a little bit.
JelleZijlstra
approved these changes
Dec 20, 2024
Contributor
Author
|
Here's a test. In order to get it to where that test would not generate the error without this change, I had to make a different change so that method-wrapper types can also be considered a function by |
hauntsaninja
pushed a commit
that referenced
this pull request
Mar 2, 2025
`__setattr__` and `__delattr__` from object are special cased by type checkers, so defining them on an inheriting class, even with the same signature, has a different meaning. This one is very similar to #18314
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.
If the runtime attribute of a class is actually from the metaclass, consider it to be MISSING at runtime.
This only occurs a couple times in the stdlib: it shows up when a descriptor is present on the metaclass but not the class, and we want to lie in the stub and say it's a thing on the class anyway.
I found this after noticing that
enum.auto.__or__had a comment that said itdidn't exist at runtime, but stubtest thought that it actually did. The issue is that on 3.10+,
type.__or__is defined for the purpose of Union types, and stubtest doesn't know the difference betweentype.__or__and__or__on the actual class.Currently this matches on these things in typeshed's stdlib:
This MR doesn't resolve any allowlist entries for typeshed, and it doesn't create any new ones either, but should generate more accurate error messages in this particular edge case.