Repository navigation
Fix member access on generic classes - #6418
Merged
Merged
Conversation
msullivan
approved these changes
Feb 22, 2019
msullivan
left a comment
Collaborator
There was a problem hiding this comment.
This seems good to me. One additional comment request
| 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)} |
Collaborator
There was a problem hiding this comment.
Could you add some comments about what is going on here?
Member
Author
There was a problem hiding this comment.
I added a detailed comment (and even found a little bug while writing it).
Member
Author
There was a problem hiding this comment.
I double-checked that everything is OK with internal code bases.
hauntsaninja
added a commit
to hauntsaninja/mypy
that referenced
this pull request
Nov 3, 2024
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
hauntsaninja
added a commit
to hauntsaninja/mypy
that referenced
this pull request
Nov 3, 2024
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 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 #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.)