Skip to content

add warning for unused global / nonlocal names #825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions pyflakes/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,8 @@ def __init__(self):
super().__init__()
# Simplify: manage the special locals as globals
self.globals = self.alwaysUsed.copy()
# {name: node}
self.indirect_assignments = {}

def unused_assignments(self):
"""
Expand All @@ -564,6 +566,9 @@ def unused_annotations(self):
if not binding.used and isinstance(binding, Annotation):
yield name, binding

def unused_indirect_assignments(self):
return self.indirect_assignments.items()


class TypeScope(Scope):
pass
Expand Down Expand Up @@ -839,6 +844,8 @@ def checkDeadScopes(self):
self.report(messages.UnusedVariable, binding.source, name)
for name, binding in scope.unused_annotations():
self.report(messages.UnusedAnnotation, binding.source, name)
for name, node in scope.unused_indirect_assignments():
self.report(messages.UnusedIndirectAssignment, node, name)

all_binding = scope.get('__all__')
if all_binding and not isinstance(all_binding, ExportBinding):
Expand Down Expand Up @@ -981,6 +988,9 @@ def addBinding(self, node, value):
self.report(messages.RedefinedWhileUnused,
node, value.name, existing.source)

if isinstance(scope, FunctionScope):
scope.indirect_assignments.pop(value.name, None)

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

Expand Down Expand Up @@ -1177,6 +1187,9 @@ def on_conditional_branch():
# be executed.
return

if isinstance(self.scope, FunctionScope):
self.scope.indirect_assignments.pop(name, None)

if isinstance(self.scope, FunctionScope) and name in self.scope.globals:
self.scope.globals.remove(name)
else:
Expand Down Expand Up @@ -1835,6 +1848,8 @@ def GLOBAL(self, node):
for scope in self.scopeStack[global_scope_index + 1:]:
scope[node_name] = node_value

self.scope.indirect_assignments[node_name] = node

NONLOCAL = GLOBAL

def GENERATOREXP(self, node):
Expand Down
9 changes: 9 additions & 0 deletions pyflakes/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ def __init__(self, filename, loc, names):
self.message_args = (names,)


class UnusedIndirectAssignment(Message):
"""A `global` or `nonlocal` statement where the name is never reassigned"""
message = '`%s %s` is unused: name is never assigned in scope'

def __init__(self, filename, loc, name):
Message.__init__(self, filename, loc)
self.message_args = (type(loc).__name__.lower(), name)


class ReturnOutsideFunction(Message):
"""
Indicates a return statement outside of a function/method.
Expand Down
4 changes: 2 additions & 2 deletions pyflakes/test/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ def test_usedInGlobal(self):
self.flakes('''
import fu
def f(): global fu
''', m.UnusedImport)
''', m.UnusedImport, m.UnusedIndirectAssignment)

def test_usedAndGlobal(self):
"""
Expand All @@ -665,7 +665,7 @@ def test_usedAndGlobal(self):
import foo
def f(): global foo
def g(): foo.is_used()
''')
''', m.UnusedIndirectAssignment)

def test_assignedToGlobal(self):
"""
Expand Down
36 changes: 35 additions & 1 deletion pyflakes/test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,41 @@ def test_globalDeclaredInDifferentScope(self):
self.flakes('''
def f(): global foo
def g(): foo = 'anything'; foo.is_used()
''')
''', m.UnusedIndirectAssignment)

def test_unused_global_statement(self):
self.flakes('''
g = 0
def f1():
global g
g = 1
def f2():
global g # this is unused!
return g
''', m.UnusedIndirectAssignment)

def test_unused_nonlocal_statement(self):
self.flakes('''
def f():
x = 1
def set_x():
nonlocal x
x = 2
def get_x():
nonlocal x
return x
set_x()
return get_x()
''', m.UnusedIndirectAssignment)

def test_unused_global_statement_not_marked_as_used_by_nested_scope(self):
self.flakes('''
g = 0
def f():
global g
def f2():
g = 2
''', m.UnusedIndirectAssignment, m.UnusedVariable)

def test_function_arguments(self):
"""
Expand Down
4 changes: 2 additions & 2 deletions pyflakes/test/test_undefined_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ def f1():

def f2():
global m
''', m.UndefinedName)
''', m.UndefinedName, m.UnusedIndirectAssignment)

@skip("todo")
def test_unused_global(self):
Expand Down Expand Up @@ -462,7 +462,7 @@ def fun2():
a
a = 2
return a
''', m.UndefinedLocal)
''', m.UndefinedLocal, m.UnusedIndirectAssignment)

def test_intermediateClassScopeIgnored(self):
"""
Expand Down