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-mutableNo compatibility data collected yet for this library.
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.
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.
Consider augmenting your linting setup with `flake8-bugbear` for broader bug detection, or using a fast linter like `ruff` that includes these checks.
Change the default argument to `None` and initialize the list inside the function: `def my_func(arg=None): if arg is None: arg = []`
Change the default argument to `None` and initialize the dictionary inside the function: `def my_func(config=None): if config is None: config = {}`