A special method takes a different number of arguments than Python passes it
What it finds
def __exit__(self):, def __eq__(self):: Python calls special methods with a fixed number of arguments, __exit__ with the exception's type, value and traceback, and __eq__ with the other operand. A method that cannot take that many, or needs more, raises TypeError when Python calls it, often far from the definition.
The counts include self and follow the data model of the language reference. Methods with decorators, methods that take *args, and special methods whose arguments vary, such as __init__, __call__, __pow__ and __round__, are left alone, and so are stubs, whose overloads describe each way of calling a method.
From the rule's source, special_method_arity.rs.
class Resource: def __enter__(self): return self def __exit__(self):tx0 reports: Python calls __exit__ with 4 arguments counting self, but it takes 1 return False def __eq__(self, other, strict):tx0 reports: Python calls __eq__ with 2 arguments counting self, but it takes 3 return strict def __len__(self): return 0 def __getitem__(self, key, default=None): return default def __get__(self, instance, owner=None): return self def __setitem__(self, *args): pass def __len__(self, extra): return extra Turn it on, off or suppress it
The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:
[rules]
"tx0/python:special-method-arity" = "off"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:special-method-arity] reason="..."tx0 explain tx0/python:special-method-arity shows the same in the terminal. Found a false positive? Report it.