Skip to content

Commit

Permalink
Ignore underscore redefinition (#73)
Browse files Browse the repository at this point in the history
* Ignore underscore redefinition

* Add test: ignore underscore redefinition

* Redefining an importation should always raise a warning
  • Loading branch information
eight04 authored and myint committed Dec 8, 2017
1 parent 8aece72 commit 8a1feac
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pyflakes/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,8 +679,9 @@ def addBinding(self, node, value):
self.report(messages.RedefinedInListComp,
node, value.name, existing.source)
elif not existing.used and value.redefines(existing):
self.report(messages.RedefinedWhileUnused,
node, value.name, existing.source)
if value.name != '_' or isinstance(existing, Importation):
self.report(messages.RedefinedWhileUnused,
node, value.name, existing.source)

elif isinstance(existing, Importation) and value.redefines(existing):
existing.redefined.append(node)
Expand Down
19 changes: 19 additions & 0 deletions pyflakes/test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,25 @@ def a(): pass
def a(): pass
''', m.RedefinedWhileUnused)

def test_redefinedUnderscoreFunction(self):
"""
Test that shadowing a function definition named with underscore doesn't
raise anything.
"""
self.flakes('''
def _(): pass
def _(): pass
''')

def test_redefinedUnderscoreImportation(self):
"""
Test that shadowing an underscore importation raises a warning.
"""
self.flakes('''
from .i18n import _
def _(): pass
''', m.RedefinedWhileUnused)

def test_redefinedClassFunction(self):
"""
Test that shadowing a function definition in a class suite with another
Expand Down

0 comments on commit 8a1feac

Please sign in to comment.