summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjoshua1b <joshua1b2823@gmail.com>2018-11-01 18:49:09 +0900
committerjoshua1b <joshua1b2823@gmail.com>2018-11-02 21:21:51 +0900
commit18414e1f9ce0efc29be16ac2213cdf6ce7090675 (patch)
tree1e4216c040df7a0ab415005c733582e619ecbb87
parent91c9989de63ff7053a6c09313e2c5e09a98d46c0 (diff)
downloadpyflakes-18414e1f9ce0efc29be16ac2213cdf6ce7090675.tar.gz
Mark SubmoduleImportation imported with Importation with alias as used
If the name of SubmoduleImportation is same with the alias of other Importation and it is used, the SubmoduleImportation should be marked as used, too. Fixes https://github.com/PyCQA/pyflakes/issues/298
-rw-r--r--pyflakes/checker.py10
-rw-r--r--pyflakes/test/test_imports.py16
2 files changed, 26 insertions, 0 deletions
diff --git a/pyflakes/checker.py b/pyflakes/checker.py
index ad25b5e..756b77e 100644
--- a/pyflakes/checker.py
+++ b/pyflakes/checker.py
@@ -818,6 +818,16 @@ class Checker(object):
try:
scope[name].used = (self.scope, node)
+
+ # if the name of SubImportation is same as
+ # alias of other Importation and the alias
+ # is used, SubImportation also should be marked as used.
+ n = scope[name]
+ if isinstance(n, Importation) and n._has_alias():
+ try:
+ scope[n.fullName].used = (self.scope, node)
+ except KeyError:
+ pass
except KeyError:
pass
else:
diff --git a/pyflakes/test/test_imports.py b/pyflakes/test/test_imports.py
index f83e77e..4272186 100644
--- a/pyflakes/test/test_imports.py
+++ b/pyflakes/test/test_imports.py
@@ -887,6 +887,22 @@ class Test(TestCase):
fu.x
''')
+ def test_used_package_with_submodule_import_of_alias(self):
+ """
+ Usage of package by alias marks submodule imports as used.
+ """
+ self.flakes('''
+ import foo as f
+ import foo.bar
+ f.bar.do_something()
+ ''')
+
+ self.flakes('''
+ import foo as f
+ import foo.bar.blah
+ f.bar.blah.do_something()
+ ''')
+
def test_unused_package_with_submodule_import(self):
"""
When a package and its submodule are imported, only report once.