First parameter of a class method is not named ‘cls’¶
ID: py/not-named-cls
Kind: problem
Security severity:
Severity: recommendation
Precision: high
Tags:
- maintainability
- readability
- convention
Query suites:
- python-security-and-quality.qls
Click to see the query in the CodeQL repository
The first parameter of a class method, a new method or any metaclass method should be called cls
. This makes the purpose of the parameter clear to other developers.
Recommendation¶
Change the name of the first parameter to cls
as recommended by the style guidelines in PEP 8.
Example¶
In the example, the first parameter to make()
is klass
which should be changed to cls
for ease of comprehension.
class Entry(object):
@classmethod
def make(klass):
return Entry()
References¶
Python PEP 8: Function and method arguments.
Python Tutorial: Classes.