flake8-mutable

1.2.0 · active · verified Thu Apr 16

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.

Common errors

Warnings

Install

Quickstart

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.

view raw JSON →