Skip to content

Commit 15a35c1

Browse files
committed
Don't warn about unused _ magic var
Resolves #202
1 parent 4b2d720 commit 15a35c1

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

pyflakes/checker.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,9 @@ def unusedAssignments(self):
425425
Return a generator for the assignments which have not been used.
426426
"""
427427
for name, binding in self.items():
428-
if (not binding.used and name not in self.globals
428+
if (not binding.used
429+
and name != '_' # see issue #202
430+
and name not in self.globals
429431
and not self.usesLocals
430432
and isinstance(binding, Assignment)):
431433
yield name, binding

pyflakes/test/test_imports.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ def test_importfrom_future(self):
9494
assert binding.source_statement == 'from __future__ import print_function'
9595
assert str(binding) == '__future__.print_function'
9696

97+
def test_unusedImport_underscore(self):
98+
"""
99+
The magic underscore var should be reported as unused when used as an
100+
import alias.
101+
"""
102+
self.flakes('import fu as _', m.UnusedImport)
103+
97104

98105
class Test(TestCase):
99106

pyflakes/test/test_other.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,16 @@ def a():
11751175
b = 1
11761176
''', m.UnusedVariable)
11771177

1178+
def test_unusedUnderscoreVariable(self):
1179+
"""
1180+
Don't warn when the magic "_" (underscore) variable is unused.
1181+
See issue #202.
1182+
"""
1183+
self.flakes('''
1184+
def a(unused_param):
1185+
_ = unused_param
1186+
''')
1187+
11781188
def test_unusedVariableAsLocals(self):
11791189
"""
11801190
Using locals() it is perfectly valid to have unused variables

0 commit comments

Comments
 (0)