rspec/rules/S2710/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

83 lines
2.2 KiB
Plaintext

== Why is this an issue?
By convention, the first argument to class methods, i.e. methods decorated with ``++@classmethod++``, is named ``++cls++`` as a representation and a reminder that the argument is the class itself. Name the argument something else, and you stand a good chance of confusing both users and maintainers of the code. It might also indicate that the ``++cls++`` parameter was forgotten, in which case calling the method will most probably fail. This rule also applies to methods ``++__init_subclass__++``, ``++__class_getitem__++`` and ``++__new__++`` as their first argument is always the class instead of "self".
By default this rule accepts ``++cls++`` and ``++mcs++``, which is sometime used in metaclasses, as valid names for class parameters. You can set your own list of accepted names via the parameter ``++classParameterNames++``.
This rule raises an issue when the first parameter of a class method is not an accepted name.
=== Noncompliant code example
[source,python]
----
class Rectangle(object):
@classmethod
def area(bob, height, width): #Noncompliant
return height * width
----
=== Compliant solution
[source,python]
----
class Rectangle(object):
@classmethod
def area(cls, height, width):
return height * width
----
== Resources
* PEP8 - https://www.python.org/dev/peps/pep-0008/#function-and-method-arguments[Function and Method Arguments]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Rename XXX to a valid class parameter name or add the missing class parameter.
=== Parameters
.classParameterNames
****
_string_
----
cls,mcs
----
Comma separated list of accepted values for a class parameter.
****
=== Highlighting
The first parameter of the class method.
'''
== Comments And Links
(visible only on this page)
=== on 17 Mar 2015, 08:49:57 Elena Vilchik wrote:
\[~ann.campbell.2] I would suggest label "convention" here
=== on 18 Mar 2015, 16:24:14 Ann Campbell wrote:
There is some question about class vs metaclass method vs instance method. Postponing this rule until that's settled. Updates may be needed at that time.
endif::env-github,rspecator-view[]