{"id":8993,"library":"flake8-mutable","title":"flake8-mutable","description":"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.","status":"active","version":"1.2.0","language":"en","source_language":"en","source_url":"https://github.com/ebeweber/flake8-mutable","tags":["flake8","linter","code quality","mutable defaults","static analysis"],"install":[{"cmd":"pip install flake8-mutable","lang":"bash","label":"Install stable release"}],"dependencies":[{"reason":"flake8-mutable is a plugin for flake8 and requires flake8 to run.","package":"flake8","optional":false}],"imports":[],"quickstart":{"code":"import os\n\ndef my_function_with_footgun(a, b=[]):\n    # M511: mutable default arg of type List\n    b.append(a)\n    return b\n\ndef my_safe_function(a, b=None):\n    if b is None:\n        b = []\n    b.append(a)\n    return b\n\n# To run flake8-mutable, you'd typically execute via the flake8 CLI.\n# Example: flake8 your_script.py\n# This code demonstrates the issue and the fix.\n# Running `flake8` on a file containing `my_function_with_footgun`\n# will produce an M511 error.","lang":"python","description":"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."},"warnings":[{"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.","message":"Python'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.","severity":"gotcha","affected_versions":"All Python versions, flake8-mutable 1.x"},{"fix":"Consider augmenting your linting setup with `flake8-bugbear` for broader bug detection, or using a fast linter like `ruff` that includes these checks.","message":"flake8-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).","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Change the default argument to `None` and initialize the list inside the function: `def my_func(arg=None): if arg is None: arg = []`","cause":"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.","error":"M511 - mutable default arg of type List"},{"fix":"Change the default argument to `None` and initialize the dictionary inside the function: `def my_func(config=None): if config is None: config = {}`","cause":"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.","error":"M511 - mutable default arg of type Dict"}]}