Skip to content

Commit

Permalink
VariableChecker now accounts for attribute lookups in type comments
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
superbobry committed Jun 23, 2021
1 parent 99589b0 commit ae82678
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CONTRIBUTORS.txt
Expand Up @@ -503,3 +503,5 @@ contributors:
* Markus Siebenhaar: contributor

* Lorena Buciu (lorena-b): contributor

* Sergei Lebedev (superbobry): contributor
5 changes: 5 additions & 0 deletions ChangeLog
Expand Up @@ -171,6 +171,11 @@ Closes #4555

Closes #4023

* Fix ``unused-import`` false positive for imported modules referenced in
attribute lookups in type comments.

Closes #4603


What's New in Pylint 2.8.3?
===========================
Expand Down
4 changes: 4 additions & 0 deletions pylint/checkers/variables.py
Expand Up @@ -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)
return

if not isinstance(type_annotation, astroid.Subscript):
return

Expand Down
13 changes: 13 additions & 0 deletions tests/checkers/unittest_variables.py
Expand Up @@ -191,6 +191,19 @@ def my_method(self) -> MyType:
with self.assertNoMessages():
self.walk(module)

def test_attribute_in_type_comment(self):
"""Ensure attribute lookups in type comments are accounted for.
https://github.com/PyCQA/pylint/issues/4603
"""
module = astroid.parse(
"""
import abc
x = ... # type: abc.ABC
""")
with self.assertNoMessages():
self.walk(module)


class TestVariablesCheckerWithTearDown(CheckerTestCase):

Expand Down

0 comments on commit ae82678

Please sign in to comment.