Skip to main content

SKY-L001 · Logic and resilience

Mutable default argument

High

The catalog gives no severity for this rule; the detector always reports HIGH.

Python builds a default value such as [] or {} once, when the function is defined, and every call that uses the default gets that same object. If the function changes it, the change is still there on the next call.

Languages
Python
Quality gate
counted toward the gate limits (full and diff scans)
Finding type
defect
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

Data leaks from one call into the next. In the example below, every call returns the same dictionary: requests running at the same time can go out with another user's Authorization header, and any header a caller adds is sent with every later request.

What Skylos flags

api_client.py · PythonReported on line 8
"""Small helper around our internal billing API.""" import requests BASE_URL = "https://billing.internal.example.com"  def build_headers(token, headers={}):    headers["Authorization"] = f"Bearer {token}"    headers.setdefault("Accept", "application/json")    return headers  def fetch_invoice(token, invoice_id):    response = requests.get(        f"{BASE_URL}/invoices/{invoice_id}",        headers=build_headers(token),        timeout=10,    )    response.raise_for_status()    return response.json()

What Skylos leaves alone

api_client.py · Python
"""Small helper around our internal billing API.""" import requests BASE_URL = "https://billing.internal.example.com"  def build_headers(token, extra={}):    headers = dict(extra)    headers["Authorization"] = f"Bearer {token}"    headers.setdefault("Accept", "application/json")    return headers  def fetch_invoice(token, invoice_id):    response = requests.get(        f"{BASE_URL}/invoices/{invoice_id}",        headers=build_headers(token),        timeout=10,    )    response.raise_for_status()    return response.json()

Why Skylos leaves it alone

The default {} is only ever copied: dict(extra) builds a new dictionary on every call, and the shared default is never changed. Skylos proves this and stays quiet. The None default in the fix is still the clearer style.

How Skylos decides

  1. Runs with --quality. Looks at every def and async def (not lambda) and checks each default value, keyword-only defaults included.
  2. Counts a default as mutable when it is a list, dict or set literal, a list, dict or set comprehension, or a call to list(), dict(), set(), defaultdict(), OrderedDict(), Counter(), deque() or array() written as a bare name.
  3. Skips an empty [] or {} default whose parameter is only ever passed, alone, to dict(), list() or set() (or, for {}, to CaseInsensitiveDict() imported from requests.structures), so the shared object is copied and never changed.
  4. That proof is dropped when the module could change what those names mean: it uses reflection anywhere (getattr, setattr, vars, globals, eval, exec, inspect.signature, __dict__ and similar, even without calling it), assigns attributes on an imported module, rebinds dict, list or set, patches them, or uses from … import *.
  5. Reports HIGH on the line and column of the default value itself, which matters for signatures that span several lines.

How to fix it

Use None as the default and create a new object inside the function.

api_client.py · Python · the fix, against the flagged file
"""Small helper around our internal billing API.""" import requests BASE_URL = "https://billing.internal.example.com"  Removed: def build_headers(token, headers={}):Added: def build_headers(token, headers=None):Added:     if headers is None:Added:         headers = {}    headers["Authorization"] = f"Bearer {token}"    headers.setdefault("Accept", "application/json")    return headers  def fetch_invoice(token, invoice_id):    response = requests.get(        f"{BASE_URL}/invoices/{invoice_id}",        headers=build_headers(token),        timeout=10,    )    response.raise_for_status()    return response.json()

When it can be wrong

  • A deliberate shared cache such as def lookup(key, _cache={}) is reported; there is no exemption for it.
  • A copy-only default is reported again when the module also uses getattr() or similar reflection anywhere.
  • It misses collections.OrderedDict() written with its module prefix, bytearray() and lambdas.

Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.

How to silence it

  • Put the marker on the line that holds the default value, not on the def line of a multi-line signature. # noqa: B006 (flake8-bugbear's code for the same problem) is not read.

    Python
    def lookup(key, _cache={}):  # skylos: ignore[SKY-L001] intentional memo cache
  • [tool.skylos] ignore = ["SKY-L001"] turns the rule off for the project. A baseline accepts existing findings, but it records the absolute file path and the line, so an entry stops matching when the checkout moves or the line shifts.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.