2023-06-30 09:37:42 +02:00
This rule raises an issue when an instance or a class method does not have at least one positional parameter.
2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-06-30 09:37:42 +02:00
Every instance method is expected to have at least one positional parameter. This parameter will reference the object instance on which the method is called. Calling an instance method which doesn't have at least one parameter will raise a `TypeError`. By convention, this first parameter is usually named `self`.
Class methods, i.e. methods annotated with `@classmethod`, also require at least one parameter. The only differences is that they will receive the class itself instead of a class instance. By convention, this first parameter is usually named `cls`.
2021-04-28 16:49:39 +02:00
2023-06-30 09:37:42 +02:00
=== Exceptions
2021-04-28 16:49:39 +02:00
2023-06-30 09:37:42 +02:00
Static methods (methods annotated with the `@staticmethod` decorator) do not require any positional parameter. This rule will not raise an issue on them.
2021-04-28 16:49:39 +02:00
2023-06-30 09:37:42 +02:00
== How to fix it
2021-04-28 16:49:39 +02:00
2023-06-30 09:37:42 +02:00
Adding `self` as the first parameter of an instance method or `cls` as the first parameter of a class method will resolve the issue.
2021-04-28 16:49:39 +02:00
2023-06-30 09:37:42 +02:00
=== Code examples
2021-04-28 18:08:03 +02:00
2023-06-30 09:37:42 +02:00
==== Noncompliant code example
2021-04-28 16:49:39 +02:00
2023-06-30 09:37:42 +02:00
[source,python,diff-id=1,diff-type=noncompliant]
2021-04-28 16:49:39 +02:00
----
class MyClass:
2023-06-30 09:37:42 +02:00
def instance_method(): # Noncompliant: "self" parameter is missing.
2021-04-28 16:49:39 +02:00
print("instance_method")
@classmethod
2023-06-30 09:37:42 +02:00
def class_method(): # Noncompliant: "cls" parameter is missing.
2021-04-28 16:49:39 +02:00
print("class_method")
----
2021-04-28 18:08:03 +02:00
2023-06-30 09:37:42 +02:00
==== Compliant solution
2021-04-28 16:49:39 +02:00
2023-06-30 09:37:42 +02:00
[source,python,diff-id=1,diff-type=compliant]
2021-04-28 16:49:39 +02:00
----
class MyClass:
def instance_method(self):
print("instance_method")
@classmethod
def class_method(cls):
print("class_method")
@staticmethod
def static_method():
print("static_method")
----
2023-06-30 09:37:42 +02:00
=== Pitfalls
Note that ``++__new__++`` and ``++__init_subclass__++`` take a class as first argument even thought they are not decorated with `@classmethod`.
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
== Resources
2021-04-28 16:49:39 +02:00
2023-06-30 09:37:42 +02:00
=== Documentation
* https://docs.python.org/3.11/tutorial/classes.html#method-objects[Method Objects] - Python Method Objects
* https://docs.python.org/3.11/library/functions.html?highlight=classmethod#classmethod[Class Method] - Python Class Method
2021-04-28 18:08:03 +02:00
2021-09-20 15:38:42 +02:00
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Method has no @classmethod or @staticmethod annotation
* Add a "self" or class parameter
Method has a @classmethod annotation, or method is __new__ or __init_subclass__
* Add a class parameter
=== Highlighting
The method signature ``++def name()++``
2021-09-20 15:38:42 +02:00
endif::env-github,rspecator-view[]