summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharlie Liu <CLiu13@users.noreply.github.com>2018-11-02 20:10:26 -0400
committerCharlie Liu <CLiu13@users.noreply.github.com>2018-11-03 11:55:03 -0400
commit7d4476e1fde8876c3bc145d066188ecdc14c6526 (patch)
tree177269725552be7db6a0e7b9fa017c8d8bc810f0
parent18414e1f9ce0efc29be16ac2213cdf6ce7090675 (diff)
downloadpyflakes-7d4476e1fde8876c3bc145d066188ecdc14c6526.tar.gz
checker.py: Report undefined names in __all__
This allows pyflakes to report undefined names in __all__ when import * is used, without having to use pyflakes 2 times to detect the errors. Fixes https://github.com/PyCQA/pyflakes/issues/280
-rw-r--r--pyflakes/checker.py7
-rw-r--r--pyflakes/test/test_imports.py9
2 files changed, 12 insertions, 4 deletions
diff --git a/pyflakes/checker.py b/pyflakes/checker.py
index 756b77e..9937685 100644
--- a/pyflakes/checker.py
+++ b/pyflakes/checker.py
@@ -665,9 +665,16 @@ class Checker(object):
# mark all import '*' as used by the undefined in __all__
if scope.importStarred:
+ from_list = []
for binding in scope.values():
if isinstance(binding, StarImportation):
binding.used = all_binding
+ from_list.append(binding.fullName)
+ # report * usage, with a list of possible sources
+ from_list = ', '.join(sorted(from_list))
+ for name in undefined:
+ self.report(messages.ImportStarUsage,
+ scope['__all__'].source, name, from_list)
# Look for imported names that aren't used.
for value in scope.values():
diff --git a/pyflakes/test/test_imports.py b/pyflakes/test/test_imports.py
index 4272186..193f9d3 100644
--- a/pyflakes/test/test_imports.py
+++ b/pyflakes/test/test_imports.py
@@ -1110,12 +1110,13 @@ class TestSpecialAll(TestCase):
def test_importStarExported(self):
"""
- Do not report undefined if import * is used
+ Report undefined if import * is used
"""
self.flakes('''
- from foolib import *
- __all__ = ["foo"]
- ''', m.ImportStarUsed)
+ from math import *
+ __all__ = ['sin', 'cos']
+ csc(1)
+ ''', m.ImportStarUsed, m.ImportStarUsage, m.ImportStarUsage, m.ImportStarUsage)
def test_importStarNotExported(self):
"""Report unused import when not needed to satisfy __all__."""