Skip to content

Commit 5600d1e

Browse files
Update Rule Metadata to prepare v2.8 (#657)
1 parent 7211615 commit 5600d1e

File tree

15 files changed

+79
-38
lines changed

15 files changed

+79
-38
lines changed

python-checks/src/main/resources/org/sonar/l10n/py/rules/python/S1144.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ <h2>Compliant Solution</h2>
5252
<h2>See</h2>
5353
<ul>
5454
<li> <a href="https://docs.python.org/3.8/tutorial/classes.html#private-variables">Python documentation <del></del> Private Variables</a> </li>
55-
<li> <a href="https://www.python.org/dev/peps/pep-0008/#designing-for-inheritance">PEP 8 <del></del> Style Guide for Python Code</a> </li>
55+
<li> <a href="https://www.python.org/dev/peps/pep-0008/#designing-for-inheritance">PEP8 <del></del> Designing for Inheritance</a> </li>
5656
</ul>
5757

python-checks/src/main/resources/org/sonar/l10n/py/rules/python/S2638.html

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
<li> Removing parameters, even when they have default values. </li>
1818
<li> Adding mandatory parameters, i.e. without a default value. </li>
1919
<li> Removing the default value of a parameter. </li>
20-
<li> Renaming parameters, except when they are positional-only parameters. </li>
2120
<li> Reordering parameters, except when they are keyword-only parameters. </li>
2221
<li> Removing some ways of providing a parameter. If a parameter could be passed as keyword it should still be possible to pass it as keyword, and
2322
the same is true for positional parameters. </li>
@@ -42,13 +41,8 @@ <h2>Noncompliant Code Example:</h2>
4241
def mymethod(self): # Noncompliant. Add missing parameter "param1".
4342
pass
4443

45-
class ChildClassRenamed(ParentClass):
46-
def mymethod(self, renamed): # Noncompliant. Rename this parameter as "param1".
47-
pass
48-
4944
class ChildClassReordered(ParentClass):
50-
def mymethod(self, inserted, param1): # Noncompliant * 2.
51-
# Move param1 in first position
45+
def mymethod(self, inserted, param1): # Noncompliant
5246
# Remove parameters "inserted" or provide a default value.
5347
pass
5448
</pre>
@@ -66,16 +60,29 @@ <h2>Compliant Solution:</h2>
6660
def mymethod(self, param1=None):
6761
pass
6862

69-
class ChildClassRenamed(ParentClass):
63+
class ChildClassReordered(ParentClass):
64+
def mymethod(self, param1, inserted=None):
65+
pass
66+
</pre>
67+
<h2>Exceptions</h2>
68+
<p>In theory renaming parameters also breaks Liskov Substitution Principle. Arguments can't be passed via keyword arguments anymore. However, <a
69+
href="https://www.python.org/dev/peps/pep-0570/#consistency-in-subclasses">as PEP-570 says</a>, it is common to rename parameters when it improves
70+
code readability and when arguments are always passed by position.</p>
71+
<p>"Positional-Only Parameters" were introduced in python 3.8 to solve this problem. As most programs will need to support older versions of python,
72+
this rule won't raise an issue on renamed parameters.</p>
73+
<pre>
74+
class ParentClass(object):
7075
def mymethod(self, param1):
7176
pass
7277

73-
class ChildClassReordered(ParentClass):
74-
def mymethod(self, param1, inserted=None):
78+
class ChildClassRenamed(ParentClass):
79+
def mymethod(self, renamed): # No issue but this is suspicious. Rename this parameter as "param1" or use positional only arguments if possible.
7580
pass
7681
</pre>
7782
<h2>See</h2>
7883
<ul>
7984
<li> <a href="https://en.wikipedia.org/wiki/Liskov_substitution_principle">Wikipedia - Liskov substitution principle</a> </li>
85+
<li> Python Enhancement Proposal (PEP) 3102 - <a href="https://www.python.org/dev/peps/pep-3102/">Keyword-Only Arguments</a> </li>
86+
<li> Python Enhancement Proposal (PEP) 570 - <a href="https://www.python.org/dev/peps/pep-0570/">Python Positional-Only Parameters</a> </li>
8087
</ul>
8188

python-checks/src/main/resources/org/sonar/l10n/py/rules/python/S2710.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
<p>By convention, the first argument to class methods, i.e. methods decorated with @classmethod, is named <code>cls</code> as a representation and a
2-
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
3-
of the code. It might also indicate that the <code>cls</code> parameter was forgotten, in which case calling the method will most probably fail. This
4-
rule also applies to methods <code>__init_subclass__</code>, <code>__class_getitem__</code> and <code>__new__</code> as their first argument is always
5-
the class instead of "self".</p>
1+
<p>By convention, the first argument to class methods, i.e. methods decorated with <code>@classmethod</code>, is named <code>cls</code> as a
2+
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
3+
users and maintainers of the code. It might also indicate that the <code>cls</code> parameter was forgotten, in which case calling the method will
4+
most probably fail. This rule also applies to methods <code>__init_subclass__</code>, <code>__class_getitem__</code> and <code>__new__</code> as their
5+
first argument is always the class instead of "self".</p>
66
<p>By default this rule accepts <code>cls</code> and <code>mcs</code>, which is sometime used in metaclasses, as valid names for class parameters. You
77
can set your own list of accepted names via the parameter <code>classParameterNames</code>.</p>
88
<p>This rule raises an issue when the first parameter of a class method is not an accepted name.</p>

python-checks/src/main/resources/org/sonar/l10n/py/rules/python/S3985.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<h2>Noncompliant Code Example</h2>
1717
<pre>
1818
class Noncompliant:
19-
class __MyClas1s(): # Noncompliant
19+
class __MyClass1(): # Noncompliant
2020
pass
2121

2222
class _MyClass2(): # Noncompliant

python-checks/src/main/resources/org/sonar/l10n/py/rules/python/S5685.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<p>The <a href="https://www.python.org/dev/peps/pep-0572">walrus operator</a> <code>:=</code> (also known as "assignment expression") should be used
22
with caution as it can easily make code more difficult to understand and thus maintain. In such case it is advised to refactor the code and use an
33
assignment statement (i.e. <code>=</code>) instead.</p>
4-
<p>This rule raises an issue raises an issue when the walrus operator is used in a way which makes the code confusing.</p>
4+
<p>This rule raises an issue raises an issue when the walrus operator is used in a way which makes the code confusing, as described in <a
5+
href="https://www.python.org/dev/peps/pep-0572/#exceptional-cases">PEP 572</a>.</p>
56
<h2>Noncompliant Code Example</h2>
67
<pre>
78
# using an assignment expression (:=) as an assignment statement (=) is more explicit

python-checks/src/main/resources/org/sonar/l10n/py/rules/python/S5706.html

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
<p> Raising this exception will make the stack trace difficult to understand.</p>
55
<p>The <code>__exit__</code> method can filter passed-in exceptions by simply returning True or False.</p>
66
<p>This rule raises an issue when:</p>
7-
<p> * an <code>__exit__</code> method has a bare <code>raise</code> outside of an <code>except</code> block.</p>
8-
<p> * an <code>__exit__</code> method raises the exception provided as parameter.</p>
7+
<ul>
8+
<li> an <code>__exit__</code> method has a bare <code>raise</code> outside of an <code>except</code> block. </li>
9+
<li> an <code>__exit__</code> method raises the exception provided as parameter. </li>
10+
</ul>
911
<h2>Noncompliant Code Example</h2>
1012
<pre>
1113
class MyContextManager:
@@ -42,7 +44,9 @@ <h2>Compliant Solution</h2>
4244
raise MemoryError("No more memory") # This is ok too.
4345
</pre>
4446
<h2>See</h2>
45-
<p> * Python documentation – <a href="https://docs.python.org/3/reference/datamodel.html?highlight=__exit__%20special#object.__exit__">The
46-
<code>__exit__</code> special method</a></p>
47-
<p> * PEP 343 – <a href="https://www.python.org/dev/peps/pep-0343/">The "with" Statement</a></p>
47+
<ul>
48+
<li> Python documentation – <a href="https://docs.python.org/3/reference/datamodel.html?highlight=__exit__%20special#object.__exit__">The
49+
<code>__exit__</code> special method</a> </li>
50+
<li> PEP 343 – <a href="https://www.python.org/dev/peps/pep-0343/">The "with" Statement</a> </li>
51+
</ul>
4852

python-checks/src/main/resources/org/sonar/l10n/py/rules/python/S5714.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<p>The only two possible types for an <code>except</code>'s expression are a class deriving from <code>BaseException</code>, or a tuple composed of
2-
such classes.</p>
2+
such classes (or an old style class if you are using python 2, but this has been removed in python 3).</p>
33
<p>This rule raises an issue when the expression used in an <code>except</code> block is a boolean expression of exceptions. The result of such
44
expression is a single exception class, which is valid but not what the developer intended.</p>
55
<h2>Noncompliant Code Example</h2>

python-checks/src/main/resources/org/sonar/l10n/py/rules/python/S5717.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ <h2>Exceptions</h2>
7373
<h2>See</h2>
7474
<ul>
7575
<li> <a href="https://docs.python-guide.org/writing/gotchas/#mutable-default-arguments">The Hitchhiker's Guide to Python - Common Gotchas</a> </li>
76-
<li> [effbot.org - Default Parameter Values in Python | http://effbot.org/zone/default-values.htm] </li>
76+
<li> <a href="http://effbot.org/zone/default-values.htm">effbot.org - Default Parameter Values in Python</a> </li>
7777
<li> <a href="https://docs.python.org/3/reference/compound_stmts.html#function-definitions">Python documentation - Function definitions</a> </li>
7878
</ul>
7979

python-checks/src/main/resources/org/sonar/l10n/py/rules/python/S5719.html

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@
55
receive the class itself instead of a class instance. By convention, this first parameter is usually named "cls". Note that <code>__new__</code> and
66
<code>__init_subclass__</code> take a class as first argument even thought they are not decorated with <code>@classmethod</code>.</p>
77
<p>This rule raises an issue when an instance of class method does not have at least one positional parameter.</p>
8+
<h2>Noncompliant Code Example</h2>
9+
<pre>
10+
class MyClass:
11+
def instance_method(): # Noncompliant. "self" parameter is missing.
12+
print("instance_method")
13+
14+
@classmethod
15+
def class_method(): # Noncompliant. "cls" parameter is missing.
16+
print("class_method")
17+
</pre>
818
<h2>Compliant Solution</h2>
919
<pre>
1020
class MyClass:
@@ -21,5 +31,7 @@ <h2>Compliant Solution</h2>
2131
print("static_method")
2232
</pre>
2333
<h2>See</h2>
24-
<p> * Python documentation - <a href="https://docs.python.org/3.8/tutorial/classes.html#method-objects">Method Objects</a></p>
34+
<ul>
35+
<li> Python documentation - <a href="https://docs.python.org/3.8/tutorial/classes.html#method-objects">Method Objects</a> </li>
36+
</ul>
2537

python-checks/src/main/resources/org/sonar/l10n/py/rules/python/S5720.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,46 @@
22
parameter. This parameter will reference the object instance on which the method is called. By convention, this first parameter is named "self".</p>
33
<p>Naming the "self" parameter differently is confusing. It might also indicate that the "self" parameter was forgotten, in which case calling the
44
method will most probably fail.</p>
5+
<p>Note also that creating methods which are used as static methods without the <code>@staticmethod</code> decorator is a bad practice because calling
6+
these methods on an instance will raise a `TypeError`. Either move the method out of the class or decorate it with <code>@staticmethod</code>.</p>
57
<p>This rule raises an issue when the first parameter of an instance method is not called "self".</p>
68
<h2>Noncompliant Code Example</h2>
79
<pre>
810
class MyClass:
911
def send_request(request): # Noncompliant. "self" was probably forgotten
1012
print("send_request")
13+
14+
class ClassWithStaticMethod:
15+
def static_method(param): # Noncompliant
16+
print(param)
17+
ClassWithStaticMethod().static_method(42) # Method is available on the instance but calling it will raise a TypeError
1118
</pre>
1219
<h2>Compliant Solution</h2>
1320
<pre>
1421
class MyClass:
1522
def send_request(self, request):
1623
print("send_request")
24+
25+
class ClassWithStaticMethod:
26+
@staticmethod
27+
def static_method(param):
28+
print(param)
29+
ClassWithStaticMethod().static_method(42)
1730
</pre>
1831
<h2>Exceptions</h2>
32+
<p>This rule will also accept "cls" or "mcs" as first parameter's name for metaclasses' methods.</p>
1933
<p>No issue will be raised for methods called <code>__init_subclass__</code>, <code>__class_getitem__</code> or <code>__new__</code> as these methods'
2034
first parameter is a class.</p>
35+
<p>You can also disable issues on methods decorated with a specific decorator. Add these decorators to this rule's "ignoreDecorators" parameter.</p>
36+
<p>With "ignoredDecorators" set to "abstractmethod"</p>
37+
<pre>
38+
from abc import abstractmethod, ABC
39+
40+
class MyClass(ABC):
41+
@abstractmethod
42+
def method(): # No issue, even if it is better in this case to also decorate with @staticmethod
43+
pass
44+
</pre>
2145
<h2>See</h2>
2246
<ul>
2347
<li> Python documentation - <a href="https://docs.python.org/3.8/tutorial/classes.html#method-objects">Method Objects</a> </li>

0 commit comments

Comments
 (0)