Why this matters
Magic methods (like `__eq__`, `__len__`, etc.) must have the right number of parameters. Otherwise, they won’t work as expected and may cause runtime errors.
Verify that special methods (e.g., `__eq__`, `__len__`) have the correct number of parameters. Incorrect parameters can cause runtime errors or unexpected behavior.
Magic methods (like `__eq__`, `__len__`, etc.) must have the right number of parameters. Otherwise, they won’t work as expected and may cause runtime errors.
Side-by-side examples engineers can pattern-match during review.
class A:
def __mul__(self, other, unexpected): # Noncompliant: too many parameters
return 42
def __add__(self): # Noncompliant: missing one parameter
return 42
A() * 3 # TypeError: __mul__() missing 1 required positional argument: 'unexpected'
A() + 3 # TypeError: __add__() takes 1 positional argument but 2 were givenclass A:
def __mul__(self, other):
return 42
def __add__(self, other):
return 42
A() * 3
A() + 3class A:
def __mul__(self, other, unexpected): # Noncompliant: too many parameters
return 42
def __add__(self): # Noncompliant: missing one parameter
return 42
A() * 3 # TypeError: __mul__() missing 1 required positional argument: 'unexpected'
A() + 3 # TypeError: __add__() takes 1 positional argument but 2 were givenclass A:
def __mul__(self, other):
return 42
def __add__(self, other):
return 42
A() * 3
A() + 3From the same buckets as this rule.
Check if loops use equality operators (== or !=) in termination conditions. These can lead to infinite loops if the condition is never met exactly. Instead, use relational operators like < or > for safer loop termination.