org.sonar.l10n.py.rules.python.S2710.html Maven / Gradle / Ivy
This rule raises an issue when the first parameter of a class method is not an accepted name.
Why is this an issue?
By convention, the first argument in a class method, i.e. methods decorated with @classmethod
, is named cls
as a
representation and a reminder that the argument is the class itself. If you were to name the argument something else, you would 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
.
How to fix it
Follow the naming convention for the first parameter name of a class method.
Code examples
Noncompliant code example
class Rectangle(object):
@classmethod
def area(bob, height, width): #Noncompliant
return height * width
Compliant solution
class Rectangle(object):
@classmethod
def area(cls, height, width):
return height * width
Resources
Documentation