Skip to content
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

Merged

Conversation

superbobry
Copy link
Contributor

Steps

  • Add yourself to CONTRIBUTORS if you are a new contributor.
  • Add a ChangeLog entry describing what your PR does.
  • If it's a new feature or an important bug fix, add a What's New entry in
    doc/whatsnew/<current release.rst>.
  • Write a good description on what the PR does.

Description

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

Type of Changes

Type
🐛 Bug fix

Related Issue

Fixes #4603.

@@ -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)
Copy link
Contributor Author

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)

Copy link
Member

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 ?

Copy link
Contributor Author

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.

Copy link
Member

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 ?

Copy link
Contributor Author

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?

Copy link
Member

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 :)

@coveralls
Copy link

coveralls commented Jun 22, 2021

Coverage Status

Coverage increased (+0.003%) to 92.039% when pulling 751cf4c on superbobry:fix-attribute-in-type-comment into 1e55ae6 on PyCQA:master.

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a 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)
Copy link
Member

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 ?

@superbobry
Copy link
Contributor Author

Is it common that tests fail on PyPy but pass on CPython? I had a quick look locally, and apparently the Assign node in the test lacks a type annotation on PyPy. Have you encountered anything similar before?

@Pierre-Sassoulas
Copy link
Member

Pierre-Sassoulas commented Jun 22, 2021

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 Assign would fix the problem with pypy ?

@superbobry superbobry force-pushed the fix-attribute-in-type-comment branch from aacaaa8 to ae82678 Compare June 23, 2021 22:10
@superbobry
Copy link
Contributor Author

superbobry commented Jun 23, 2021

Sorry if my earlier comment about PyPy was misleading. I was not referring to type annotations in of the Assign node, but rather the type_annotation attribute of Assign, which is non-None on CPython and None on PyPy for the same input.

@Pierre-Sassoulas
Copy link
Member

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 @skipif for pypi. Because the fix seems non trivial.

@superbobry superbobry force-pushed the fix-attribute-in-type-comment branch from 27ce7cc to 37dac96 Compare June 26, 2021 18:53
@superbobry
Copy link
Contributor Author

I have a theory about what's going on

  • the ast module in PyPy does not yet support type comment parsing (added in CPython 3.8);
  • typed_ast does not build on recent PyPy versions;
  • astroid fails to import typed_ast on PyPy and falls back to ast.

I have disabled the new test on PyPy.

@Pierre-Sassoulas Pierre-Sassoulas added Enhancement ✨ Improvement to a component False Positive 🦟 A message is emitted but nothing is wrong with the code labels Jun 26, 2021
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.9.0 milestone Jun 26, 2021
@superbobry superbobry force-pushed the fix-attribute-in-type-comment branch from 11a965c to 5f5a0f5 Compare June 26, 2021 19:02
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.
@superbobry superbobry force-pushed the fix-attribute-in-type-comment branch from ac61c78 to 7766e61 Compare June 27, 2021 10:48
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a 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 :) !

@Pierre-Sassoulas Pierre-Sassoulas merged commit 3d6389b into pylint-dev:master Jun 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component False Positive 🦟 A message is emitted but nothing is wrong with the code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

unused-import false positive for a module used in a type comment
3 participants