Comment 9 for bug 1328169

Revision history for this message
Kurtis Rader (krader) wrote :

It is a common idiom at my company to assign to the underscore var as a way to explicitly indicate a variable is unused. For example, you might have a method that overrides a base class method but which contains vars your implementation doesn't need:

    def MethodX(self, needed_var, unneeded_var):
        _ = unneeded_var
        ...

This makes it clear to the reader that the variable is being deliberately ignored and silences pylint. It would be nice if pep8 also recognized this convention. I'd like to propose a one line change that achieves this result:

diff --git a/pyflakes/checker.py b/pyflakes/checker.py
index 7055832..5bbc6b5 100644
--- a/pyflakes/checker.py
+++ b/pyflakes/checker.py
@@ -211,7 +211,7 @@ class FunctionScope(Scope):
     @ivar globals: Names declared 'global' in this function.
     """
     usesLocals = False
- alwaysUsed = set(['__tracebackhide__',
+ alwaysUsed = set(['_', '__tracebackhide__',
                       '__traceback_info__', '__traceback_supplement__'])

     def __init__(self):