Skip to content

SCR103: unused-variable

Local variable is assigned but never read

Code SCR103
Name unused-variable
Category Scripts (SCR)
Default severity warning
Enabled by default yes
Fix available sometimes
Fix safety unsafe
ign-lint equivalent none

Reports local variables of functions (including Perspective scripts, which are function bodies) whose name is assigned but never read, neither in the function nor in a nested function or lambda. Assignments, with ... as x and except E, x count; parameters, loop variables, comprehension variables and imports (see SCR102) do not.

Not reported:

  • names that are part of a tuple or list target (a, b = f(), where only one may be needed);
  • names matching the dummy pattern: _, starting with _, dummy, ignored_..., unused_... (option dummy-pattern);
  • functions that read their locals dynamically (locals(), vars(), eval, exec), for example "%(name)s" % locals(), and every function of a module with code that reads or writes a frame's or a module's names by itself (sys._getframe(), .f_locals, .tb_frame, e = eval, import __builtin__; the table is in docs/internal/fix-safety.md, "Frame routes: findings and fixes"). Reflection or introspection that is only a first hop (a Java member that leads to reflection such as .type or getColumnType(i), getattr with a computed name, sys.exc_info(), line numbers) does not silence the rule;
  • the exception class in except ValueError, TypeError:, which SCR115 reports.

An unused variable is dead code at best; often it is a typo (the value was meant to be used under that name) or a forgotten step (result = system.db.runPrepQuery(...) whose result is never written anywhere).

def runAction(self, event):
rows = system.db.runNamedQuery("Orders/Open")
count = len(rows)
self.props.text = "%d open orders" % len(rows)

Use instead:

def runAction(self, event):
rows = system.db.runNamedQuery("Orders/Open")
self.props.text = "%d open orders" % len(rows)
  • dummy-pattern (regex, default ^(_.*|dummy.*|ignored_.*|unused_.*)$): names that may be unused.

For x = value with a single target: removes the statement when value calls nothing and binds nothing (literals, names, attribute and subscript reads, operators, comparisons), otherwise removes only x = and keeps the expression, so that its calls still run and the variables of a list comprehension stay bound (Python 2 leaks them: x = [i for i in y] followed by return i). Backticks call repr. What is kept is never a pointless statement for SCR114. Unsafe: the value may still be wanted under another name, and a removed read or operator can have effects in Python (value["pages"] raises KeyError when the key is missing; a property, __getattr__, a Java getter, __eq__, __contains__ or the __str__ that % formatting calls runs code).

Corresponds to W0612 unused-variable. pylint also reports unused names of tuple targets (a, b = f()); iglint does not.

Set in [rules.SCR103] of iglint.toml (or [tool.iglint.rules.SCR103] in pyproject.toml).

Option Type Default Description
dummy-pattern string "^(_.*|dummy.*|ignored_.*|unused_.*)$" Names matching this regex may be unused.

Generated from the rule metadata by iglint generate-docs; iglint rule SCR103 prints the same page in the terminal. Source: docs/rules/SCR103.md.