Skip to content

set-attr-with-constant

Pythonstyleno fixdefault: offpython/ruff

Reports a statement setattr(x, "name", v) with a literal name that is a valid identifier.

setattr is for names known only at run time. With a fixed name x.name = v does the same and is checked by type checkers and found by searches for the attribute.

To fix, write x.name = v.

Reported
...
getattr(x, "__private")
getattr(x, "class")
getattr(x, "name", None)
setattr(x, "name", 1)
y = setattr(x, "name", 1)
setattr(x, "class", 1)
...
python:
lint:
rules:
set-attr-with-constant: warn # off (default), warn, error

Enabled by the presets: python/ruff.

Suppress one line with sudsy-disable-next-line set-attr-with-constant 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 B010 full