Skip to content

function-call-in-default

Pythonsuspiciousno fixdefault: warnpython/ruff

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.

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.

Reported
...
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
...
python:
lint:
rules:
function-call-in-default: warn # off, warn (default), error

Enabled 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.

The same check in other tools; Sudsy reports what each upstream rule reports.

Tool Rule Match
ruff B008 full