rspec/rules/S5719/python/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

72 lines
1.9 KiB
Plaintext

== Why is this an issue?
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 it will receive the class itself instead of a class instance. By convention, this first parameter is usually named "cls". Note that ``++__new__++`` and ``++__init_subclass__++`` take a class as first argument even thought they are not decorated with ``++@classmethod++``.
This rule raises an issue when an instance of class method does not have at least one positional parameter.
=== Noncompliant code example
[source,python]
----
class MyClass:
def instance_method(): # Noncompliant. "self" parameter is missing.
print("instance_method")
@classmethod
def class_method(): # Noncompliant. "cls" parameter is missing.
print("class_method")
----
=== Compliant solution
[source,python]
----
class MyClass:
def instance_method(self):
print("instance_method")
@classmethod
def class_method(cls):
print("class_method")
@staticmethod
def static_method():
print("static_method")
----
== Resources
* Python documentation - https://docs.python.org/3.8/tutorial/classes.html#method-objects[Method Objects]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== 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()++``
endif::env-github,rspecator-view[]