Skip to content

mutable-argument-default

Pythonsuspiciousno fixdefault: warnpython/ruff

Reports a parameter whose default is a list, dict or set: a literal, a comprehension, or a call of list(), dict(), set(), collections.deque() and the like. A parameter annotated with an immutable type is left alone.

The default is made once, when the function is defined, and every call that does not pass the argument shares it. A call that appends to items=[] changes the default for all later calls.

To fix, default to None and create the value in the body.

Reported
...
x = a = b = 1
def f(a=[], b={}, c=set(), d=list(), e=collections.deque(), g=[x for x in a]):
pass
...
python:
lint:
rules:
mutable-argument-default: warn # off, warn (default), error

Enabled by the presets: python/ruff.

Suppress one line with sudsy-disable-next-line mutable-argument-default in a comment of the file’s language.

The same check in other tools; Sudsy reports what each upstream rule reports.

Tool Rule Match
ruff B006 full