Skip to content

Commit

Permalink
Prevent unused-import for six.with_metaclass
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls committed Jun 25, 2022
1 parent e1e5b39 commit 0bfa1df
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
19 changes: 17 additions & 2 deletions pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1137,9 +1137,24 @@ def visit_classdef(self, node: nodes.ClassDef) -> None:
"""Visit class: update consumption analysis variable."""
self._to_consume.append(NamesConsumer(node, "class"))

def leave_classdef(self, _: nodes.ClassDef) -> None:
def leave_classdef(self, node: nodes.ClassDef) -> None:
"""Leave class: update consumption analysis variable."""
# do not check for not used locals here (no sense)
# Check for hidden ancestor names
# e.g. "six" in: Class X(six.with_metaclass(ABCMeta, object)):
for name_node in node.nodes_of_class(nodes.Name):
if (
isinstance(name_node.parent, nodes.Call)
and isinstance(name_node.parent.func, nodes.Attribute)
and isinstance(name_node.parent.func.expr, nodes.Name)
):
hidden_name_node = name_node.parent.func.expr
for consumer in self._to_consume:
if hidden_name_node.name in consumer.to_consume:
consumer.mark_as_consumed(
hidden_name_node.name,
consumer.to_consume[hidden_name_node.name],
)
break
self._to_consume.pop()

def visit_lambda(self, node: nodes.Lambda) -> None:
Expand Down
5 changes: 5 additions & 0 deletions tests/functional/u/unused/unused_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import xml.etree # [unused-import]
import xml.sax # [unused-import]
import os.path as test # [unused-import]
from abc import ABCMeta
from sys import argv as test2 # [unused-import]
from sys import flags # [unused-import]
# +1:[unused-import,unused-import]
from collections import deque, OrderedDict, Counter
import re, html.parser # [unused-import]
DATA = Counter()
# pylint: disable=self-assigning-variable
import six
from fake import SomeName, SomeOtherName # [unused-import]
class SomeClass(object):
SomeName = SomeName # https://bitbucket.org/logilab/pylint/issue/475
Expand Down Expand Up @@ -87,3 +89,6 @@ def blop(self):
TYPE_CHECKING = False
if TYPE_CHECKING:
import zoneinfo

class WithMetaclass(six.with_metaclass(ABCMeta, object)):
pass
22 changes: 11 additions & 11 deletions tests/functional/u/unused/unused_import.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
unused-import:3:0:3:16::Unused import xml.etree:UNDEFINED
unused-import:4:0:4:14::Unused import xml.sax:UNDEFINED
unused-import:5:0:5:22::Unused os.path imported as test:UNDEFINED
unused-import:6:0:6:29::Unused argv imported from sys as test2:UNDEFINED
unused-import:7:0:7:21::Unused flags imported from sys:UNDEFINED
unused-import:9:0:9:51::Unused OrderedDict imported from collections:UNDEFINED
unused-import:9:0:9:51::Unused deque imported from collections:UNDEFINED
unused-import:10:0:10:22::Unused import re:UNDEFINED
unused-import:13:0:13:40::Unused SomeOtherName imported from fake:UNDEFINED
unused-import:48:0:48:9::Unused import os:UNDEFINED
unused-import:79:4:79:19::Unused import unittest:UNDEFINED
unused-import:81:4:81:15::Unused import uuid:UNDEFINED
unused-import:83:4:83:19::Unused import warnings:UNDEFINED
unused-import:85:4:85:21::Unused import compileall:UNDEFINED
unused-import:7:0:7:29::Unused argv imported from sys as test2:UNDEFINED
unused-import:8:0:8:21::Unused flags imported from sys:UNDEFINED
unused-import:10:0:10:51::Unused OrderedDict imported from collections:UNDEFINED
unused-import:10:0:10:51::Unused deque imported from collections:UNDEFINED
unused-import:11:0:11:22::Unused import re:UNDEFINED
unused-import:15:0:15:40::Unused SomeOtherName imported from fake:UNDEFINED
unused-import:50:0:50:9::Unused import os:UNDEFINED
unused-import:81:4:81:19::Unused import unittest:UNDEFINED
unused-import:83:4:83:15::Unused import uuid:UNDEFINED
unused-import:85:4:85:19::Unused import warnings:UNDEFINED
unused-import:87:4:87:21::Unused import compileall:UNDEFINED

0 comments on commit 0bfa1df

Please sign in to comment.