function-call-in-default
What it does
Section titled “What it does”Reports a call in the default value of a parameter. Calls in a lambda, and calls of immutable builtins such as tuple, are not reported.
Why it matters
Section titled “Why it matters”A default is evaluated once, when the function is defined, and every call shares the value. def f(when=datetime.now()) keeps the time of the import for ever, and a default that builds an object shares that object between calls.
To fix, default to None and make the value in the body.
Example
Section titled “Example”...x = a = b = 1
def f(a=[], b={}, c=set(), d=list(), e=collections.deque(), g=[x for x in a]): pass
def immutable(a: Sequence[int] = [], b: tuple = (), c=(), d=frozenset(), e=bytearray()): pass
...Configuration
Section titled “Configuration”python: lint: rules: function-call-in-default: warn # off, warn (default), errorEnabled by the presets: python/ruff.
Suppress one line with sudsy-disable-next-line function-call-in-default in a comment of the file’s language.
Upstream equivalents
Section titled “Upstream equivalents”The same check in other tools; Sudsy reports what each upstream rule reports.
| Tool | Rule | Match |
|---|---|---|
| ruff | B008 |
full |