Modify Rule S5719: LaYC format (#2257)

This commit is contained in:
David Kunzmann 2023-06-30 09:37:42 +02:00 committed by GitHub
parent b545853971
commit fe0265f995
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,31 +1,38 @@
This rule raises an issue when an instance or a class method does not have at least one positional parameter.
== 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".
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`.
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++``.
=== Exceptions
Static methods (methods annotated with the `@staticmethod` decorator) do not require any positional parameter. This rule will not raise an issue on them.
This rule raises an issue when an instance of class method does not have at least one positional parameter.
== How to fix it
Adding `self` as the first parameter of an instance method or `cls` as the first parameter of a class method will resolve the issue.
=== Noncompliant code example
=== Code examples
[source,python]
==== Noncompliant code example
[source,python,diff-id=1,diff-type=noncompliant]
----
class MyClass:
def instance_method(): # Noncompliant. "self" parameter is missing.
def instance_method(): # Noncompliant: "self" parameter is missing.
print("instance_method")
@classmethod
def class_method(): # Noncompliant. "cls" parameter is missing.
def class_method(): # Noncompliant: "cls" parameter is missing.
print("class_method")
----
=== Compliant solution
==== Compliant solution
[source,python]
[source,python,diff-id=1,diff-type=compliant]
----
class MyClass:
def instance_method(self):
@ -35,16 +42,21 @@ class MyClass:
def class_method(cls):
print("class_method")
@staticmethod
def static_method():
print("static_method")
----
=== Pitfalls
Note that ``++__new__++`` and ``++__init_subclass__++`` take a class as first argument even thought they are not decorated with `@classmethod`.
== Resources
* Python documentation - https://docs.python.org/3.8/tutorial/classes.html#method-objects[Method Objects]
=== 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
ifdef::env-github,rspecator-view[]