A class assigns a private attribute self.__name that nothing reads

tx0/python:write-only-private-fieldrecommendedwarningsmell

What it finds

An attribute whose name starts with two underscores is renamed by Python to belong to its class, so only code in that class can read it. When the class assigns it but never reads it, the value is computed and stored for nothing, or the read that was meant uses another name. Checked are self.__name = ... assignments in the methods of a class whose body never mentions __name in any other way; names ending in two underscores are special and not private.

From the rule's source, write_only_private_field.rs.

Example from the rule's tests
class Cache:    def __init__(self, size):        self.__size = sizetx0 reports: the private attribute __size is assigned but never read        self.__items = {}        self.__name__ = "cache"     def get(self, key):        return self.__items.get(key)  class Lookup:    def __init__(self):        self.__table = {}     def find(self, name):        return getattr(self, "_Lookup__table")
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:write-only-private-field" = "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:write-only-private-field] reason="..."

tx0 explain tx0/python:write-only-private-field shows the same in the terminal. Found a false positive? Report it.