Skip to content

Commit 7b5e0e7

Browse files
jrobertson98atxPCManticore
authored andcommitted
Fix misattribution of unused import (#2672)
Fixed issue where import statements with multiple import arguments wouldn't flag the correct missing package.
1 parent 095928c commit 7b5e0e7

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

pylint/checkers/variables.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,16 +213,24 @@ def _fix_dot_imports(not_consumed):
213213
continue
214214
for imports in stmt.names:
215215
second_name = None
216-
if imports[0] == "*":
216+
import_module_name = imports[0]
217+
if import_module_name == "*":
217218
# In case of wildcard imports,
218219
# pick the name from inside the imported module.
219220
second_name = name
220221
else:
221-
if imports[0].find(".") > -1 or name in imports:
222+
name_matches_dotted_import = False
223+
if (
224+
import_module_name.startswith(name)
225+
and import_module_name.find(".") > -1
226+
):
227+
name_matches_dotted_import = True
228+
229+
if name_matches_dotted_import or name in imports:
222230
# Most likely something like 'xml.etree',
223231
# which will appear in the .locals as 'xml'.
224232
# Only pick the name if it wasn't consumed.
225-
second_name = imports[0]
233+
second_name = import_module_name
226234
if second_name and second_name not in names:
227235
names[second_name] = stmt
228236
return sorted(names.items(), key=lambda a: a[1].fromlineno)

pylint/test/functional/unused_import.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
"""unused import"""
2-
# pylint: disable=undefined-all-variable, import-error, no-absolute-import, too-few-public-methods, missing-docstring,wrong-import-position, useless-object-inheritance
2+
# pylint: disable=undefined-all-variable, import-error, no-absolute-import, too-few-public-methods, missing-docstring,wrong-import-position, useless-object-inheritance, multiple-imports
33
import xml.etree # [unused-import]
44
import xml.sax # [unused-import]
55
import os.path as test # [unused-import]
66
from sys import argv as test2 # [unused-import]
77
from sys import flags # [unused-import]
88
# +1:[unused-import,unused-import]
99
from collections import deque, OrderedDict, Counter
10+
import re, html.parser # [unused-import]
1011
DATA = Counter()
1112

1213
from fake import SomeName, SomeOtherName # [unused-import]
@@ -33,3 +34,6 @@ def get_ordered_dict() -> 'collections.OrderedDict':
3334

3435
def get_itertools_obj() -> 'itertools.count':
3536
return []
37+
38+
def use_html_parser() -> 'html.parser.HTMLParser':
39+
return html.parser.HTMLParser

pylint/test/functional/unused_import.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ unused-import:6::Unused argv imported from sys as test2
55
unused-import:7::Unused flags imported from sys
66
unused-import:9::Unused OrderedDict imported from collections
77
unused-import:9::Unused deque imported from collections
8-
unused-import:12::Unused SomeOtherName imported from fake
8+
unused-import:10::Unused import re
9+
unused-import:13::Unused SomeOtherName imported from fake

0 commit comments

Comments
 (0)