Derived from the flake8-bugbear linter.
Fix is sometimes available.
What it does#
Checks for uses of mutable objects as function argument defaults.
Why is this bad?#
Function defaults are evaluated once, when the function is defined.
The same mutable object is then shared across all calls to the function. If the object is modified, those modifications will persist across calls, which can lead to unexpected behavior.
Instead, prefer to use immutable data structures, or take
None as a
default, and initialize a new mutable object inside the function body
for each call.
Arguments with immutable type annotations will be ignored by this rule.
Types outside of the standard library can be marked as immutable with the
flake8-bugbear.extend-immutable-calls configuration option.
Mutable argument defaults can be used intentionally to cache computation
results. Replacing the default with
None or an immutable data structure
does not work for such usages. Instead, prefer the
decorator from the standard library.