Skip to content

Commit 8a1feac

Browse files
eight04myint
authored andcommitted
Ignore underscore redefinition (#73)
* Ignore underscore redefinition * Add test: ignore underscore redefinition * Redefining an importation should always raise a warning
1 parent 8aece72 commit 8a1feac

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

pyflakes/checker.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -679,8 +679,9 @@ def addBinding(self, node, value):
679679
self.report(messages.RedefinedInListComp,
680680
node, value.name, existing.source)
681681
elif not existing.used and value.redefines(existing):
682-
self.report(messages.RedefinedWhileUnused,
683-
node, value.name, existing.source)
682+
if value.name != '_' or isinstance(existing, Importation):
683+
self.report(messages.RedefinedWhileUnused,
684+
node, value.name, existing.source)
684685

685686
elif isinstance(existing, Importation) and value.redefines(existing):
686687
existing.redefined.append(node)

pyflakes/test/test_other.py

+19
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,25 @@ def a(): pass
151151
def a(): pass
152152
''', m.RedefinedWhileUnused)
153153

154+
def test_redefinedUnderscoreFunction(self):
155+
"""
156+
Test that shadowing a function definition named with underscore doesn't
157+
raise anything.
158+
"""
159+
self.flakes('''
160+
def _(): pass
161+
def _(): pass
162+
''')
163+
164+
def test_redefinedUnderscoreImportation(self):
165+
"""
166+
Test that shadowing an underscore importation raises a warning.
167+
"""
168+
self.flakes('''
169+
from .i18n import _
170+
def _(): pass
171+
''', m.RedefinedWhileUnused)
172+
154173
def test_redefinedClassFunction(self):
155174
"""
156175
Test that shadowing a function definition in a class suite with another

0 commit comments

Comments
 (0)