Skip to content

Commit dcf31a1

Browse files
committed
Import in Class is a public member
An import in a class is a member of the class, and may be used outside the class scope, so it can not be marked as unused.
1 parent d6de471 commit dcf31a1

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

pyflakes/checker.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,10 @@ def checkDeadScopes(self):
348348
which were imported but unused.
349349
"""
350350
for scope in self.deadScopes:
351+
# imports in classes are public members
352+
if isinstance(scope, ClassScope):
353+
continue
354+
351355
if isinstance(scope.get('__all__'), ExportBinding):
352356
all_names = set(scope['__all__'].names)
353357
if not scope.importStarred and \

pyflakes/test/test_imports.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,22 @@ class bar:
236236
print(fu)
237237
''')
238238

239+
def test_importInClass(self):
240+
"""
241+
Test that import within class is a locally scoped attribute.
242+
"""
243+
self.flakes('''
244+
class bar:
245+
import fu
246+
''')
247+
248+
self.flakes('''
249+
class bar:
250+
import fu
251+
252+
fu
253+
''', m.UndefinedName)
254+
239255
def test_usedInFunction(self):
240256
self.flakes('''
241257
import fu
@@ -570,7 +586,7 @@ class bar:
570586
import fu
571587
def fun(self):
572588
fu
573-
''', m.UnusedImport, m.UndefinedName)
589+
''', m.UndefinedName)
574590

575591
def test_nestedFunctionsNestScope(self):
576592
self.flakes('''
@@ -689,7 +705,6 @@ def test_importingForImportError(self):
689705
pass
690706
''')
691707

692-
@skip("todo: requires evaluating attribute access")
693708
def test_importedInClass(self):
694709
"""Imports in class scope can be used through self."""
695710
self.flakes('''
@@ -760,12 +775,11 @@ def foo():
760775

761776
def test_ignoredInClass(self):
762777
"""
763-
An C{__all__} definition does not suppress unused import warnings in a
764-
class scope.
778+
An C{__all__} definition in a class does not suppress unused import warnings.
765779
"""
766780
self.flakes('''
781+
import bar
767782
class foo:
768-
import bar
769783
__all__ = ["bar"]
770784
''', m.UnusedImport)
771785

0 commit comments

Comments
 (0)