mutable-argument-default
What it does
Section titled “What it does”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.
Why it matters
Section titled “Why it matters”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.
Example
Section titled “Example”...x = a = b = 1
def f(a=[], b={}, c=set(), d=list(), e=collections.deque(), g=[x for x in a]): pass
...Configuration
Section titled “Configuration”python: lint: rules: mutable-argument-default: warn # off, warn (default), errorEnabled by the presets: python/ruff.
Suppress one line with sudsy-disable-next-line mutable-argument-default in a comment of the file’s language.
Upstream equivalents
Section titled “Upstream equivalents”The same check in other tools; Sudsy reports what each upstream rule reports.
| Tool | Rule | Match |
|---|---|---|
| ruff | B006 |
full |