-
-
Notifications
You must be signed in to change notification settings - Fork 276
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
Modify infernce tip for typing.Generic and typing.Annotated with __class_getitem__
#931
Conversation
__class_getitem
__class_getitem__
841e387
to
dedf3b9
Compare
dedf3b9
to
3ff3222
Compare
3ff3222
to
f5333ba
Compare
During testing, I noticed two related issues with
from typing import Generic, TypeVar
T = TypeVar('T')
class A:
pass
class B(Generic[T]):
pass
class C(A, B[T], Generic[T]):
pass
print(C.__mro__)
# The result will be
(<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'typing.Generic'>, <class 'object'>)
# Previously, astroid would have resolved this to
['.C', '.A', '.B', '.Generic', '.Generic', 'builtins.object']
# Now
['.C', '.A', '.B', 'typing.Generic', 'builtins.object'] |
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.
Great job @cdce8p . I just left a couple of comments. The most important IMHO is the one dealing with clean_typing_generic_mro
.
astroid/scoped_nodes.py
Outdated
"""typing.Generic is allowed to appear multiple times in the initial mro. | ||
The final one however, MUST only contain ONE. |
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.
The terms initial and final are a bit confusing. What are their meanings?
part of bases_mro. If true, remove it from inferred_bases | ||
as well as its entry the bases_mro. | ||
|
||
Format sequences: [[self]] + bases_mro + [inferred_bases] |
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 wonder if it could not be clearer to pass 3 arguments instead of one (i.e self, bases_mro and inferred_bases).
What do you think about it?
astroid/scoped_nodes.py
Outdated
""" | ||
pos_generic_in_main_bases = -1 | ||
# Check if part of inferred_bases | ||
for i, base in enumerate(sequences[-1]): |
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 think it would be definitely clearer to manipulate self (or an alias), bases_mro and inferred bases instead of dealing with only one sequence and multiples indexes.
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 would like to leave list(clean_duplicates_mro(unmerged_mro, self, context))
in _compute_mro
untouched. This should be run before the MRO is cleaned from Generics. However, that also means we only have access to the whole sequence. Creating references inside clean_typing_generic_mro
might work though.
1b14dd5
to
39cfad4
Compare
39cfad4
to
b235283
Compare
@hippo91 I've pushed a new commit to address your comments. clean_typing_generic_mro I've also redone the comment. As you said, |
@hippo91 Do you had time to take it a short look at the changes yet? I was thinking that we could release a new patch version of /CC: @Pierre-Sassoulas What is your opinion? |
Yes, I think we should release as soon as possible because the packaging changes in pylint could cause problems we did not anticipate and the less other bugs we get handle for the next release the better. |
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.
Thanks @cdce8p ! It is clearer! 👍
Steps
Description
With Python 3.7,
typing.Generic
is now subscriptable through__class_getitem__
. Same fortyping.Annotated
which was added in 3.9. As with the__class_getitem__
methods for thecollections.abc
classes, astroid needs a bit of help inferring them correctly.Type of Changes
Related Issue
This will address: pylint-dev/pylint#2822
I would like to add a test case to
pylint
before closing it, though.