A class body assigns the same attribute twice

tx0/python:class-attribute-redefinedrecommendedwarningbug

What it finds

timeout = 10 and later timeout = 30 in the same class body: the second replaces the first before the class is even finished, so the first value is never used. Usually one of them was meant to have another name.

Only plain assignments of one name directly in the class body are compared; assignments inside if and try blocks, which pick a value, are left alone, and so is an annotation without a value followed by an assignment.

From the rule's source, class_attribute_redefined.rs.

Example from the rule's tests
class Settings:    timeout = 10    retries = 3    timeout = 30tx0 reports: timeout was already assigned in this class body, and that value is never used    name: str = "a"    name: str = "b"tx0 reports: name was already assigned    __slots__ = ()    if retries:        retries = 5
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/python:class-attribute-redefined" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

# tx0: ignore[tx0/python:class-attribute-redefined] reason="..."

tx0 explain tx0/python:class-attribute-redefined shows the same in the terminal. Found a false positive? Report it.