Skip to content

Commit

Permalink
Update rules metadata (#2069)
Browse files Browse the repository at this point in the history
  • Loading branch information
joke1196 authored Oct 14, 2024
1 parent cbef8fd commit 2aebe38
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<p>This rule is deprecated; use {rule:python:S5905} instead.</p>
<h2>Why is this an issue?</h2>
<p>Parentheses are not required after the <code>assert</code>, <code>del</code>, <code>elif</code>, <code>except</code>, <code>for</code>,
<code>if</code>, <code>in</code>, <code>not</code>, <code>raise</code>, <code>return</code>, <code>while</code>, and <code>yield</code> keywords, and
using them unnecessarily impairs readability. They should therefore be omitted.</p>
<code>if</code>, <code>not</code>, <code>raise</code>, <code>return</code>, <code>while</code>, and <code>yield</code> keywords. Similarly,
parentheses are not required after <code>in</code> in a <code>for</code> loop. Using parentheses unnecessarily impairs readability, and therefore,
they should be omitted.</p>
<h3>Noncompliant code example</h3>
<pre>
x = 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"title": "Hard-coded credentials are security-sensitive",
"title": "Hard-coded passwords are security-sensitive",
"type": "SECURITY_HOTSPOT",
"code": {
"impacts": {
Expand Down Expand Up @@ -41,5 +41,6 @@
"3.5.2",
"6.4.1"
]
}
},
"quickfix": "unknown"
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ <h2>Why is this an issue?</h2>
<code>__new__</code> as their first argument is always the class instead of "self".</p>
<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
can set your own list of accepted names via the parameter <code>classParameterNames</code>.</p>
<h3>How to fix it</h3>
<h2>How to fix it</h2>
<p>Follow the naming convention for the first parameter name of a class method.</p>
<h3>Code examples</h3>
<h4>Noncompliant code example</h4>
<pre data-diff-id="1" data-diff-type="noncompliant">
class Rectangle(object):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ <h2>Why is this an issue?</h2>
<li> <strong>Compatibility:</strong> If you are working on projects that need to be compatible with older versions of Python (before 3.8), you
should avoid using the walrus operator, as it won’t be available in those versions. </li>
</ul>
<h3>How to fix it</h3>
<h2>How to fix it</h2>
<p>Avoid using the walrus operator for the cases when it is not mandatory.</p>
<h3>Code examples</h3>
<h4>Noncompliant code example</h4>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,5 +204,8 @@ <h3>Documentation</h3>
<h3>Standards</h3>
<ul>
<li> CWE - <a href="https://cwe.mitre.org/data/definitions/284">CWE-284 - Improper Access Control</a> </li>
<li> OWASP - <a href="https://owasp.org/Top10/A01_2021-Broken_Access_Control/">Top 10 2021 Category A1 - Broken Access Control</a> </li>
<li> OWASP - <a href="https://owasp.org/www-project-top-ten/2017/A3_2017-Sensitive_Data_Exposure">Top 10 2017 Category A3 - Sensitive Data
Exposure</a> </li>
</ul>

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
"CWE": [
284
],
"OWASP": [
"A3"
],
"OWASP Top 10 2021": [
"A1"
],
"PCI DSS 3.2": [
"6.5.8"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,16 +400,14 @@ <h3>Code examples</h3>
<h4>Noncompliant code example</h4>
<pre data-diff-id="1" data-diff-type="noncompliant">
import numpy as np
def foo():
np.random.seed(42)
x = np.random.randn() # Noncompliant: this relies on numpy.random.RandomState, which is deprecated
np.random.seed(42)
x = np.random.randn() # Noncompliant: this relies on numpy.random.RandomState, which is deprecated
</pre>
<h4>Compliant solution</h4>
<pre data-diff-id="1" data-diff-type="compliant">
import numpy as np
def foo():
generator = np.random.default_rng(42)
x = generator.standard_normal()
generator = np.random.default_rng(42)
x = generator.standard_normal()
</pre>
<h2>Resources</h2>
<h3>Documentation</h3>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<p>This rule raises an issue when the axis argument is not provided to TensorFlow’s reduction operations.</p>
<p>This rule raises an issue when the <code>axis</code>/<code>dim`</code> argument is not provided to reduction operations.</p>
<h2>Why is this an issue?</h2>
<p>The result of TensorFlow’s reduction operations (i.e. <code>tf.math.reduce_sum</code>, <code>tf.math.reduce_std</code>), highly depends on the
shape of the Tensor provided.</p>
<h3>TensorFlow</h3>
<p>The result of reduction operations (i.e. <code>tf.math.reduce_sum</code>, <code>tf.math.reduce_std</code>, <code>torch.sum</code>,
<code>torch.mean</code>, etc…​), highly depends on the shape of the Tensor provided.</p>
<pre>
import tensorflow as tf

Expand Down Expand Up @@ -42,7 +43,9 @@ <h2>Why is this an issue?</h2>
<p>In the example above, specifying the axis clarifies the intent, as the result now is <code>[5, 7]</code>. If the intent was to effectively reduce
across all dimensions the user should provide the list of axis <code>axis=[0,1]</code> or clearly state the default behavior should be applied with
<code>axis=None</code>.</p>
<h2>How to fix it</h2>
<h3>The PyTorch equivalent</h3>
<p>The same behavior occurs in PyTorch, but the argument is called <code>dim</code> instead of <code>axis</code>.</p>
<h2>How to fix it in TensorFlow</h2>
<p>To fix this issue provide the axis argument when using a TensorFlow reduction operation such as <code>tf.math.reduce_sum</code>,
<code>tf.math.reduce_prod</code>, <code>tf.math.reduce_mean</code>, etc…​</p>
<h3>Code examples</h3>
Expand All @@ -60,6 +63,24 @@ <h4>Compliant solution</h4>
x = tf.constant([[1, 1, 1], [1, 1, 1]])
tf.math.reduce_sum(x, axis=0) # Compliant: the reduction will happen only on the axis 0, resulting in `[2,2,2]`
</pre>
<h2>How to fix it in PyTorch</h2>
<p>To fix this issue provide the dim argument when using a PyTorch reduction operation such as <code>torch.sum</code>, <code>torch.prod</code>,
<code>torch.mean</code>, etc…​</p>
<h3>Code examples</h3>
<h4>Noncompliant code example</h4>
<pre data-diff-id="2" data-diff-type="noncompliant">
import torch

x = torch.tensor([[1, 1, 1], [1, 1, 1]])
torch.sum(x) # Noncompliant: the dim argument defaults to None
</pre>
<h4>Compliant solution</h4>
<pre data-diff-id="2" data-diff-type="compliant">
import torch

x = torch.tensor([[1, 1, 1], [1, 1, 1]])
torch.sum(x, dim=None) # Compliant: all dimensions will be reduced
</pre>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
Expand All @@ -71,6 +92,7 @@ <h3>Documentation</h3>
<li> TensorFlow Documentation - <a href="https://www.tensorflow.org/api_docs/python/tf/math/reduce_sum">tf.math.reduce_sum reference</a> </li>
<li> TensorFlow Documentation - <a href="https://www.tensorflow.org/api_docs/python/tf/math/reduce_variance">tf.math.reduce_variance reference</a>
</li>
<li> PyTorch Documentation - <a href="https://pytorch.org/docs/stable/torch.html#reduction-ops">Reduction operations</a> </li>
</ul>
<h3>Articles &amp; blog posts</h3>
<ul>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
{
"title": "The axis argument should be specified when using TensorFlow\u0027s reduction operations",
"title": "The reduction axis\/dimension should be specified when using reduction operations",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [],
"tags": [
"tensorflow",
"pytorch",
"machine-learning",
"scientific-computing"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-6929",
"sqKey": "S6929",
"scope": "All",
"quickfix": "unknown",
"quickfix": "targeted",
"code": {
"impacts": {
"MAINTAINABILITY": "MEDIUM",
Expand Down
2 changes: 1 addition & 1 deletion sonarpedia.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"languages": [
"PY"
],
"latest-update": "2024-09-24T09:07:11.168038831Z",
"latest-update": "2024-10-14T08:24:54.620505158Z",
"options": {
"no-language-in-filenames": true,
"preserve-filenames": true
Expand Down

0 comments on commit 2aebe38

Please sign in to comment.