A parameter's default is a list, dict or set shared by all calls
What it finds
def add(item, items=[]): a default value is made once, when the function is defined, and every call that leaves the parameter out gets the same object. Changes one call makes to a list, dict or set default are seen by all later calls.
Reported are list, dict and set displays and comprehensions, and calls of the builtins list, dict, set and bytearray. This rule is in the recommended tier because a shared default is sometimes used on purpose, as a cache. Stubs are not checked, since their defaults only document those of the code they describe.
From the rule's source, mutable_default_argument.rs.
from collections import OrderedDict as dict def add(item, items=[]):tx0 reports: the default [] is made once and shared by every call that leaves out items items.append(item) return items def configure( options={"debug": False},tx0 reports: leaves out options *, seen=set(),tx0 reports: leaves out seen flags=list(),tx0 reports: leaves out flags squares={n * n for n in range(3)},tx0 reports: leaves out squares): return options, seen, flags, squares def count(word, counts: list[int] = []):tx0 reports: leaves out counts return counts handler = lambda event, log=[]: log.append(event)tx0 reports: leaves out log def fine(item, items=None, size=(), name="", limit=frozenset(), table=dict()): return item, items, size, name, limit, table Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/python:mutable-default-argument" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
# tx0: ignore[tx0/python:mutable-default-argument] reason="..."tx0 explain tx0/python:mutable-default-argument shows the same in the terminal. Found a false positive? Report it.