This rule raises an issue when the first parameter of an instance method is not called "self".
Instance methods, i.e. methods not annotated with @classmethod or @staticmethod, are expected to have at least one
parameter. This parameter will reference the object instance on which the method is called. By convention, this first parameter is named "self".
Naming the first parameter something different from "self" is not recommended as it could lead to confusion. It might indicate that the "self" parameter was forgotten, in which case calling the method will most probably fail.
Note also that creating methods which are used as static methods without the @staticmethod decorator is a bad practice. Calling these
methods on an instance will raise a TypeError. Either move the method out of the class or decorate it with
@staticmethod.
This rule will accept "cls" or "mcs" as first parameter’s name for class and metaclasses methods.
No issue will be raised for the following methods: __init_subclass__, __class_getitem__ and __new__, as
these methods' first parameter is a class.
This rule may be parameterized to prevent raising issues on methods decorated with specific decorators. These decorators can be added to this
rule’s ignoredDecorators parameter.
For example, with ignoredDecorators set to "myDecorator".
class MyClass(ABC):
@myDecorator
def method(arg): # No issue will be raised.
pass
Make sure to have a "self" parameter on instance methods and annotate static methods with the @staticmethod decorator.
class MyClass:
def send_request(request): # Noncompliant: the "self" parameter is missing.
print("send_request")
class ClassWithStaticMethod:
def static_method(param): # Noncompliant: the "@staticmethod" decorator is missing.
print(param)
ClassWithStaticMethod().static_method(42) # The method is available on the instance but calling it will raise a TypeError.
class MyClass:
def send_request(self, request):
print("send_request")
class ClassWithStaticMethod:
@staticmethod
def static_method(param):
print(param)
ClassWithStaticMethod().static_method(42)