-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix member access on generic classes #6418
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems good to me. One additional comment request
mypy/checkmember.py
Outdated
if is_classmethod: | ||
assert isuper is not None | ||
t = expand_type_by_instance(t, isuper) | ||
ids = {t.id for t in itype.args if isinstance(t, TypeVarType)} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add some comments about what is going on here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a detailed comment (and even found a little bug while writing it).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I double-checked that everything is OK with internal code bases.
This behaviour was intentionally chosen in python#6418 I agree with Ivan's comment there that it's similar to how mypy allows instantiation of `type[Abstract]` -- but that's a thing that I want to explore disallowing. Let's see primer
This behaviour was intentionally chosen in python#6418 I agree with Ivan's comment there that it's similar to how mypy allows instantiation of `type[Abstract]` -- but that's a thing that I want to explore disallowing. Let's see primer
Fixes #3645
Fixes #1337
Fixes #5664
The fix is straightforward, I just add/propagate the bound type variable values by mapping to supertype.
I didn't find any corner cases with class methods, and essentially follow the same logic as when we generate the callable from
__init__
for generic classes in calls likeC()
orC[int]()
.For class attributes there are two things I fixed. First we used to prohibit ambiguous access:
but the type variables were leaking after an error, now they are erased to
Any
. Second, I now make an exception and allow accessing attributes onType[C]
, this is very similar to how we allow instantiation ofType[C]
even if it is abstract (because we expect concrete subclasses there), plus this allows accessing variables oncls
(first argument in class methods), for example:(I also added a bunch of more detailed comments in this part of code.)