Skip to content

Commit 3802a73

Browse files
Update docs
1 parent e8adef1 commit 3802a73

File tree

6 files changed

+14
-21
lines changed

6 files changed

+14
-21
lines changed

python/ql/src/Functions/NonCls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
class Entry(object):
22
@classmethod
3-
def make(klass):
3+
def make(self):
44
return Entry()

python/ql/src/Functions/NonCls.qhelp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@
55

66

77
<overview>
8-
<p> The first parameter of a class method, a new method or any metaclass method
9-
should be called <code>cls</code>. This makes the purpose of the parameter clear to other developers.
8+
<p> The first parameter of a class method (including certain special methods such as <code>__new__</code>), or a method of a metaclass,
9+
should be named <code>cls</code>.
1010
</p>
1111

1212
</overview>
1313
<recommendation>
1414

15-
<p>Change the name of the first parameter to <code>cls</code> as recommended by the style guidelines
15+
<p>Ensure that the first parameter of class methods is named <code>cls</code>, as recommended by the style guidelines
1616
in PEP 8.</p>
1717

1818
</recommendation>
1919
<example>
20-
<p>In the example, the first parameter to <code>make()</code> is <code>klass</code> which should be changed to <code>cls</code>
21-
for ease of comprehension.
20+
<p>In the following example, the first parameter of the class method <code>make</code> is named <code>self</code> instead of <code>cls</code>.
2221
</p>
2322

2423
<sample src="NonCls.py" />
@@ -29,6 +28,7 @@ for ease of comprehension.
2928

3029
<li>Python PEP 8: <a href="http://www.python.org/dev/peps/pep-0008/#function-and-method-arguments">Function and method arguments</a>.</li>
3130
<li>Python Tutorial: <a href="http://docs.python.org/2/tutorial/classes.html">Classes</a>.</li>
31+
<li>Python Docs: <a href="https://docs.python.org/3/library/functions.html#classmethod"><code>classmethod</code></a>.</li>
3232

3333

3434
</references>

python/ql/src/Functions/NonCls.ql

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/**
22
* @name First parameter of a class method is not named 'cls'
3-
* @description Using an alternative name for the first parameter of a class method makes code more
4-
* difficult to read; PEP8 states that the first parameter to class methods should be 'cls'.
3+
* @description By the PEP8 style guide, the first parameter of a class method should be named `cls`.
54
* @kind problem
65
* @tags maintainability
76
* readability

python/ql/src/Functions/NonSelf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
class Point:
2-
def __init__(val, x, y): # first parameter is mis-named 'val'
2+
def __init__(val, x, y): # BAD: first parameter is mis-named 'val'
33
val._x = x
44
val._y = y
55

66
class Point2:
7-
def __init__(self, x, y): # first parameter is correctly named 'self'
7+
def __init__(self, x, y): # GOOD: first parameter is correctly named 'self'
88
self._x = x
99
self._y = y

python/ql/src/Functions/NonSelf.qhelp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,18 @@
66

77
<overview>
88
<p> Normal methods should have at least one parameter and the first parameter should be called <code>self</code>.
9-
This makes the purpose of the parameter clear to other developers.
109
</p>
1110
</overview>
1211

1312
<recommendation>
14-
<p>If there is at least one parameter, then change the name of the first parameter to <code>self</code> as recommended by the style guidelines
13+
<p>Ensure that the first parameter of a normal method is named <code>self</code>, as recommended by the style guidelines
1514
in PEP 8.</p>
16-
<p>If there are no parameters, then it cannot be a normal method. It may need to be marked as a <code>staticmethod</code>
17-
or it could be moved out of the class as a normal function.
15+
<p>If a <code>self</code> parameter is unneeded, the method should be decorated with <code>staticmethod</code>, or moved out of the class as a regular function.
1816
</p>
1917
</recommendation>
2018

2119
<example>
22-
<p>The following methods can both be used to assign values to variables in a <code>point</code>
23-
object. The second method makes the association clearer because the <code>self</code> parameter is
24-
used.</p>
20+
<p>In the following cases, the first argument of <code>Point.__init__</code> is named <code>val</code> instead; whereas in <code>Point2.__init__</code> it is correctly named <code>self</code>.</p>
2521
<sample src="NonSelf.py" />
2622

2723

@@ -31,7 +27,7 @@ used.</p>
3127
<li>Python PEP 8: <a href="http://www.python.org/dev/peps/pep-0008/#function-and-method-arguments">Function and
3228
method arguments</a>.</li>
3329
<li>Python Tutorial: <a href="http://docs.python.org/2/tutorial/classes.html">Classes</a>.</li>
34-
30+
<li>Python Docs: <a href="https://docs.python.org/3/library/functions.html#staticmethod"><code>staticmethod</code></a>.</li>.
3531

3632

3733
</references>

python/ql/src/Functions/NonSelf.ql

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/**
22
* @name First parameter of a method is not named 'self'
3-
* @description Using an alternative name for the first parameter of an instance method makes
4-
* code more difficult to read; PEP8 states that the first parameter to instance
5-
* methods should be 'self'.
3+
* @description By the PEP8 style guide, the first parameter of a normal method should be named `self`.
64
* @kind problem
75
* @tags maintainability
86
* readability

0 commit comments

Comments
 (0)