Skip to content

mutable-class-default

Pythonsuspiciousno fixdefault: offpython/ruff

Reports a class attribute assigned a list, dict, set or comprehension, or a call that builds one, with no ClassVar or Final annotation. Data classes, Pydantic models and named tuples are left alone.

A class attribute belongs to the class and is shared by every instance. self.items.append(x) on one instance changes all of them, which is rarely meant.

To fix, create the value in __init__, or annotate the attribute ClassVar if sharing is the intent.

Reported
...
return a
class A:
x = []
...
...
python:
lint:
rules:
mutable-class-default: warn # off (default), warn, error

Enabled by the presets: python/ruff.

Suppress one line with sudsy-disable-next-line mutable-class-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 RUF012 full