Skip to content

get-attr-with-constant

Pythonstyleno fixdefault: offpython/ruff

Reports getattr(x, "name") with a literal name that is a valid identifier and two arguments.

getattr is for names that are known only at run time. With a fixed name x.name does the same, is checked by type checkers and found by searches for the attribute.

To fix, write x.name.

Reported
...
assert 0
getattr(x, "name")
getattr(x, "__private")
getattr(x, "class")
getattr(x, "name", None)
...
python:
lint:
rules:
get-attr-with-constant: warn # off (default), warn, error

Enabled by the presets: python/ruff.

Suppress one line with sudsy-disable-next-line get-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 B009 full