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
VariableChecker now accounts for attribute lookups in type comments #4604
VariableChecker now accounts for attribute lookups in type comments #4604
Conversation
@@ -1826,6 +1826,10 @@ def _store_type_annotation_node(self, type_annotation): | |||
self._type_annotation_names.append(type_annotation.name) | |||
return | |||
|
|||
if isinstance(type_annotation, astroid.Attribute): | |||
self._store_type_annotation_node(type_annotation.expr) |
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 could change this to a more restrictive form
if isinstance(type_annotation, astroid.Attribute):
if not isinstance(type_anntoation.expr, astroid.Name):
return
self._type_annotation_names.append(type_annotation.expr.name)
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.
Hello, could you maybe add a use case so it's easier to understand what this would prevent ?
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 current check covers all of
# type: foo.Bar
# type: foo.bar.Boo
# type: Foo[T].Bar
# type: Foo[S, T].Bar
The restricted check only allows # type: foo.Bar
. I personally prefer the current check to the restricted one, but I don't feel strongly.
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.
Thank you for the explanation :) I also prefer handling all the use cases. Would it be possible to add those 4 examples in the functional tests in tests/functional
?
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've added them to variables_test.py
. Would you prefer me also add them to tests/functional
?
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 like functional because there's no tests code around it, only the code and the expected output, but as long as there is a test it's ok :)
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.
Thank you for the fix !
@@ -1826,6 +1826,10 @@ def _store_type_annotation_node(self, type_annotation): | |||
self._type_annotation_names.append(type_annotation.name) | |||
return | |||
|
|||
if isinstance(type_annotation, astroid.Attribute): | |||
self._store_type_annotation_node(type_annotation.expr) |
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.
Hello, could you maybe add a use case so it's easier to understand what this would prevent ?
Is it common that tests fail on PyPy but pass on CPython? I had a quick look locally, and apparently the |
Ha, we never encountered this before. but by your description of the problem, it might be due to the latest astroid, we added a LOT of typing recently. Do you think adding the proper typing for |
aacaaa8
to
ae82678
Compare
Sorry if my earlier comment about PyPy was misleading. I was not referring to type annotations in of the |
It might be a problem in astroid that do not understand the code properly when the interpreter is pypi. I think we can handle the two case and add a |
27ce7cc
to
37dac96
Compare
I have a theory about what's going on
I have disabled the new test on PyPy. |
11a965c
to
5f5a0f5
Compare
Prior to this commit VariableChecker did not recurse into attribute lookups in type comments. This lead to false positive unused-import messages in e.g. import collections d = ... # type: collections.OrderedDict Fixes pylint-dev#4603.
ac61c78
to
7766e61
Compare
for more information, see https://pre-commit.ci
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.
Thank you for this fix, much appreciated :) !
Steps
doc/whatsnew/<current release.rst>
.Description
Prior to this commit
VariableChecker
did not recurse into attribute lookups in type comments. This lead to false positiveunused-import messages
in e.g.Type of Changes
Related Issue
Fixes #4603.