SKY-L001 · Logic and resilience
Mutable default argument
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
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
"""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
"""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
- Runs with
--quality. Looks at everydefandasync def(notlambda) and checks each default value, keyword-only defaults included. - 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()orarray()written as a bare name. - Skips an empty
[]or{}default whose parameter is only ever passed, alone, todict(),list()orset()(or, for{}, toCaseInsensitiveDict()imported fromrequests.structures), so the shared object is copied and never changed. - 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, rebindsdict,listorset, patches them, or usesfrom … import *. - 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.
"""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
defline 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.