summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJim Robertson <jrobertson98atx@gmail.com>2019-03-19 04:11:59 -0500
committerClaudiu Popa <pcmanticore@gmail.com>2019-03-19 10:11:59 +0100
commit7b5e0e797709afcacd440cab4b326973d3397f3f (patch)
tree35c36564ff788c8d0e8989e41349a96b378911c1
parent095928cceaef476f2e76a64a63cf215ffa782f5c (diff)
downloadpylint-git-7b5e0e797709afcacd440cab4b326973d3397f3f.tar.gz
Fix misattribution of `unused import` (#2672)
Fixed issue where import statements with multiple import arguments wouldn't flag the correct missing package.
-rw-r--r--pylint/checkers/variables.py14
-rw-r--r--pylint/test/functional/unused_import.py6
-rw-r--r--pylint/test/functional/unused_import.txt3
3 files changed, 18 insertions, 5 deletions
diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py
index 694be8e5e..85de92aaa 100644
--- a/pylint/checkers/variables.py
+++ b/pylint/checkers/variables.py
@@ -213,16 +213,24 @@ def _fix_dot_imports(not_consumed):
continue
for imports in stmt.names:
second_name = None
- if imports[0] == "*":
+ import_module_name = imports[0]
+ if import_module_name == "*":
# In case of wildcard imports,
# pick the name from inside the imported module.
second_name = name
else:
- if imports[0].find(".") > -1 or name in imports:
+ name_matches_dotted_import = False
+ if (
+ import_module_name.startswith(name)
+ and import_module_name.find(".") > -1
+ ):
+ name_matches_dotted_import = True
+
+ if name_matches_dotted_import or name in imports:
# Most likely something like 'xml.etree',
# which will appear in the .locals as 'xml'.
# Only pick the name if it wasn't consumed.
- second_name = imports[0]
+ second_name = import_module_name
if second_name and second_name not in names:
names[second_name] = stmt
return sorted(names.items(), key=lambda a: a[1].fromlineno)
diff --git a/pylint/test/functional/unused_import.py b/pylint/test/functional/unused_import.py
index e551db5d4..4f265277a 100644
--- a/pylint/test/functional/unused_import.py
+++ b/pylint/test/functional/unused_import.py
@@ -1,5 +1,5 @@
"""unused import"""
-# pylint: disable=undefined-all-variable, import-error, no-absolute-import, too-few-public-methods, missing-docstring,wrong-import-position, useless-object-inheritance
+# pylint: disable=undefined-all-variable, import-error, no-absolute-import, too-few-public-methods, missing-docstring,wrong-import-position, useless-object-inheritance, multiple-imports
import xml.etree # [unused-import]
import xml.sax # [unused-import]
import os.path as test # [unused-import]
@@ -7,6 +7,7 @@ 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()
from fake import SomeName, SomeOtherName # [unused-import]
@@ -33,3 +34,6 @@ def get_ordered_dict() -> 'collections.OrderedDict':
def get_itertools_obj() -> 'itertools.count':
return []
+
+def use_html_parser() -> 'html.parser.HTMLParser':
+ return html.parser.HTMLParser
diff --git a/pylint/test/functional/unused_import.txt b/pylint/test/functional/unused_import.txt
index 9d6497f96..aa3c07726 100644
--- a/pylint/test/functional/unused_import.txt
+++ b/pylint/test/functional/unused_import.txt
@@ -5,4 +5,5 @@ unused-import:6::Unused argv imported from sys as test2
unused-import:7::Unused flags imported from sys
unused-import:9::Unused OrderedDict imported from collections
unused-import:9::Unused deque imported from collections
-unused-import:12::Unused SomeOtherName imported from fake
+unused-import:10::Unused import re
+unused-import:13::Unused SomeOtherName imported from fake