2023-08-08 11:03:54 +02:00
This rule raises an issue when the first parameter of an instance method is not called "self".
2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-04-28 16:49:39 +02:00
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".
2023-08-08 11:03:54 +02:00
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.
2021-04-28 16:49:39 +02:00
2023-08-08 11:03:54 +02:00
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++``.
2021-04-28 16:49:39 +02:00
2023-08-08 11:03:54 +02:00
=== Exceptions
2021-04-28 16:49:39 +02:00
2023-08-08 11:03:54 +02:00
This rule will accept "cls" or "mcs" as first parameter's name for class and metaclasses methods.
2021-04-28 16:49:39 +02:00
2023-08-08 11:03:54 +02:00
No issue will be raised for the following methods: ``++__init_subclass__++``, ``++__class_getitem__++`` and ``++__new__++``, as these methods' first parameter is a class.
2021-04-28 16:49:39 +02:00
2023-08-08 11:03:54 +02:00
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.
2021-04-28 18:08:03 +02:00
2023-08-08 11:03:54 +02:00
For example, with `ignoredDecorators` set to "myDecorator".
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,python]
2021-04-28 16:49:39 +02:00
----
2023-08-08 11:03:54 +02:00
class MyClass(ABC):
@myDecorator
def method(arg): # No issue will be raised.
pass
----
== How to fix it
Make sure to have a "self" parameter on instance methods and annotate static methods with the `@staticmethod` decorator.
=== Code examples
==== Noncompliant code example
[source,python,diff-id=1,diff-type=noncompliant]
----
2021-04-28 16:49:39 +02:00
class MyClass:
2023-08-08 11:03:54 +02:00
def send_request(request): # Noncompliant: the "self" parameter is missing.
2021-04-28 16:49:39 +02:00
print("send_request")
class ClassWithStaticMethod:
2023-08-08 11:03:54 +02:00
def static_method(param): # Noncompliant: the "@staticmethod" decorator is missing.
2021-04-28 16:49:39 +02:00
print(param)
2023-08-08 11:03:54 +02:00
ClassWithStaticMethod().static_method(42) # The method is available on the instance but calling it will raise a TypeError.
2021-04-28 16:49:39 +02:00
----
2021-04-28 18:08:03 +02:00
2023-08-08 11:03:54 +02:00
==== Compliant solution
2021-04-28 16:49:39 +02:00
2023-08-08 11:03:54 +02:00
[source,python,diff-id=1,diff-type=compliant]
2021-04-28 16:49:39 +02:00
----
class MyClass:
def send_request(self, request):
print("send_request")
class ClassWithStaticMethod:
@staticmethod
def static_method(param):
print(param)
2023-08-08 11:03:54 +02:00
2021-04-28 16:49:39 +02:00
ClassWithStaticMethod().static_method(42)
----
2021-04-28 18:08:03 +02:00
2023-08-08 11:03:54 +02:00
== Resources
2021-04-28 16:49:39 +02:00
2023-08-08 11:03:54 +02:00
=== Documentation
2021-04-28 16:49:39 +02:00
2023-08-08 11:03:54 +02:00
* Python Documentation - https://docs.python.org/3.8/tutorial/classes.html#method-objects[Method Objects]
2021-04-28 18:08:03 +02:00
2023-08-08 11:03:54 +02:00
=== Standards
2021-04-28 16:49:39 +02:00
* PEP8 - https://www.python.org/dev/peps/pep-0008/#function-and-method-arguments[Function and Method Arguments]
2021-04-28 18:08:03 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Rename XXX to "self" or add the missing "self" parameter.
=== Parameters
.ignoredDecorators
****
----
abstractmethod
----
Comma-separated list of decorators which will disable this rule.
****
2021-09-20 15:38:42 +02:00
2023-05-25 14:18:12 +02:00
=== Highlighting
Instance method's first parameter
2021-09-20 15:38:42 +02:00
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== on 10 Mar 2020, 17:51:21 Nicolas Harraudeau wrote:
Exceptions to this rule which are not worth mentioning in the RSPEC:
* No issue will be raised on zope.Interface methods
* No issue will be raised on methods in classes nested in other methods. It is common to name "self" otherwise to avoid the confusion
----
class A:
def meth(self):
class B:
def nested(this): # Ok
pass
----
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]