Registry / testing / flake8-mutable

flake8-mutable

JSON →
library1.2.0pypypiunverified

flake8-mutable is a linter extension for Flake8 that identifies and flags the use of mutable objects as default arguments in Python function definitions. This helps prevent common and often unexpected bugs where mutations to the default argument persist across function calls. The current version is 1.2.0. The project appears to be stable with infrequent updates, having been last released in 2017.

pip install flake8-mutable
INSTALL
IMPORT
SIG · FLAKE8-MUTABLE
F
flake8-mutable
testingpythonv1.2.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Install flake8-mutable, then run flake8 from your command line on your Python files. The plugin automatically integrates with flake8 to report `M511` errors for mutable default arguments.

import os def my_function_with_footgun(a, b=[]): # M511: mutable default arg of type List b.append(a) return b def my_safe_function(a, b=None): if b is None: b = [] b.append(a) return b # To run flake8-mutable, you'd typically execute via the flake8 CLI. # Example: flake8 your_script.py # This code demonstrates the issue and the fix. # Running `flake8` on a file containing `my_function_with_footgun` # will produce an M511 error.
Debug
Known issues
gotchaPython's default arguments are evaluated only once, when the function is defined. If a mutable object (like a list, dictionary, or set) is used as a default argument, all subsequent calls to that function will share the *same instance* of that mutable object. Modifications to it will persist across calls, leading to unexpected behavior.
fix
Use `None` as the default value for mutable arguments and initialize the mutable object inside the function body if `None` is passed. Alternatively, use immutable data structures like `tuple` or `frozenset` if appropriate, or `types.MappingProxyType` for dictionaries.
affects: All Python versions, flake8-mutable 1.x
gotchaflake8-mutable specifically checks for mutable default arguments (M511). Other similar issues, such as mutable class attributes shared across instances, are not covered by this specific plugin but might be caught by other linters like `flake8-bugbear` (B006) or `ruff` (RUF012).
fix
Consider augmenting your linting setup with `flake8-bugbear` for broader bug detection, or using a fast linter like `ruff` that includes these checks.
affects: All versions
Errors
Common errors & fixes
M511 - mutable default arg of type List
A list (which is a mutable data type) was used as a default argument in a function definition. This causes all calls to the function to share the same list object, leading to unintended side effects when the list is modified.
fix
Change the default argument to `None` and initialize the list inside the function: `def my_func(arg=None): if arg is None: arg = []`
M511 - mutable default arg of type Dict
A dictionary (a mutable data type) was used as a default argument in a function definition, causing all function calls to share the same dictionary object.
fix
Change the default argument to `None` and initialize the dictionary inside the function: `def my_func(config=None): if config is None: config = {}`
Upgrade
Version history
1.2.0latest on PyPI · released Apr 24, 2017
Audit
Dependencies
flake8requiredflake8-mutable is a plugin for flake8 and requires flake8 to run.
Agent activity
6 hits · last 30 days
node
6
Resources
flake8-mutable — pip install flake8-mutable · libregistry