This rule raises an issue when the first parameter of an instance method is not called "self".
Why is this an issue?
Instance methods, i.e. methods not annotated with
@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
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:
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
For example, with
ignoredDecorators set to "myDecorator".
def method(arg): # No issue will be raised.
How to fix it
Make sure to have a "self" parameter on instance methods and annotate static methods with the
Noncompliant code example
def send_request(request): # Noncompliant: the "self" parameter is missing.
def static_method(param): # Noncompliant: the "@staticmethod" decorator is missing.
ClassWithStaticMethod().static_method(42) # The method is available on the instance but calling it will raise a TypeError.
def send_request(self, request):