Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ <h2>Noncompliant Code Example</h2>
<pre>
def foo():
try:
raise FileExistsError()
except (OSError, RuntimeError) as e:
raise FloatingPointError()
except (ArithmeticError, RuntimeError) as e:
print(e)
except FileExistsError as e: # Noncompliant. FileExistsError is a subclass of OSError
except FloatingPointError as e: # Noncompliant. FloatingPointError is a subclass of ArithmeticError
print("Never executed")
except FileNotFoundError as e: # Noncompliant. FileNotFoundError is a subclass of OSError
except OverflowError as e: # Noncompliant. OverflowError is a subclass of ArithmeticError
print("Never executed")

try:
Expand All @@ -39,12 +39,12 @@ <h2>Compliant Solution</h2>
<pre>
def foo():
try:
raise FileExistsError()
except FileExistsError as e:
print("Never executed")
except FileNotFoundError as e:
print("Never executed")
except (OSError, RuntimeError) as e:
raise FloatingPointError()
except FloatingPointError as e:
print("Executed")
except OverflowError as e:
print("Executed")
except (ArithmeticError, RuntimeError) as e:
print(e)

try:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<p>Using <code>return</code>, <code>break</code> or <code>continue</code> in a <code>finally</code> block suppresses the propagation of any unhandled
exception which was raised in the <code>try</code>, <code>else</code> or <code>except</code> blocks.</p>
exception which was raised in the <code>try</code>, <code>else</code> or <code>except</code> blocks. It will also ignore their return statements.</p>
<p><code><a href="https://docs.python.org/3/library/exceptions.html#SystemExit">SystemExit</a></code> is raised when <code>sys.exit()</code> is
called. <code><a href="https://docs.python.org/3/library/exceptions.html#KeyboardInterrupt">KeyboardInterrupt</a></code> is raised when the user asks
the program to stop by pressing interrupt keys. Both exceptions are expected to propagate up until the application stops. It is ok to catch them when
a clean-up is necessary but they should be raised again immediately. They should never be ignored.</p>
<p>If you need to ignore every other exception you can simply catch the <code>Exception</code> class. However you should be very careful when you do
this as it will ignore other important exceptions such as <code><a
href="https://docs.python.org/3/library/exceptions.html#MemoryError">MemoryError</a></code></p>
<p>In python 2 it is possible to raise old style classes. You can use a bare <code>except:</code> statement to catch every exception. Remember to
still reraise <code>SystemExit</code> and <code>KeyboardInterrupt</code>.</p>
<p>This rule raises an issue when a jump statement (<code>break</code>, <code>continue</code>, <code>return</code>) would force the control flow to
leave a finally block.</p>
<h2>Noncompliant Code Example</h2>
Expand Down Expand Up @@ -39,6 +48,14 @@ <h2>Noncompliant Code Example</h2>
raise
finally:
break # This will prevent SystemExit from raising

def continue_whatever_happens_noncompliant():
for i in range(10):
try:
raise ValueError()
finally:
continue # Noncompliant

</pre>
<h2>Compliant Solution</h2>
<pre>
Expand Down Expand Up @@ -75,6 +92,15 @@ <h2>Compliant Solution</h2>
except (SystemExit) as e:
print("Exiting")
raise # SystemExit is re-raised

import logging

def continue_whatever_happens_compliant():
for i in range(10):
try:
raise ValueError()
except Exception:
logging.exception("Failed") # Ignore all "Exception" subclasses yet allow SystemExit and other important exceptions to pass
</pre>
<h2>See</h2>
<ul>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
<p>In some cases a comparison with operators <code>==</code>, or <code>!=</code> will always return True or always return False. When this happens,
the comparison and all its dependent code can simply be removed. This includes:</p>
<ul>
<li> comparing <code>None</code> with a value which will always be <code>None</code>. </li>
<li> comparing unrelated builtin types such as string and integer. </li>
<li> comparing class instances which do not implement <code>__eq__</code>, or <code>__ne__</code> if <code>!=</code> is used, to an object of a
different type (builtin or from an unrelated class which also doesn't implement <code>__eq__</code> or <code>__ne__</code>). </li>
<li> comparing class instances which do not implement <code>__eq__</code> or <code>__ne__</code> to an object of a different type (builtin or from
an unrelated class which also doesn't implement <code>__eq__</code> or <code>__ne__</code>). </li>
</ul>
<h2>Noncompliant Code Example</h2>
<pre>
mynone = None

foo = mynone == None # Noncompliant. Always True.

foo = 1 == "1" # Noncompliant. Always False.

foo = 1 != "1" # Noncompliant. Always True.
Expand All @@ -22,14 +17,6 @@ <h2>Noncompliant Code Example</h2>
myvar = A() == 1 # Noncompliant. Always False.
myvar = A() != 1 # Noncompliant. Always True.

class Ne:
def __ne__(self, other):
return True

myvar = Ne() == 1 # Noncompliant. Always False. "__eq__" does not call "__ne__" by default
myvar = 1 == Ne() # Noncompliant. Always False.
myvar = Ne() != 1 # Ok
myvar = 1 != Ne() # Ok
</pre>
<h2>Compliant Solution</h2>
<pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"tags": [
"unused"
],
"defaultSeverity": "Major",
"defaultSeverity": "Blocker",
"ruleSpecification": "RSPEC-2159",
"sqKey": "S2159",
"scope": "Main"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,5 @@ <h2>Noncompliant Code Example</h2>
mystring = "1"
value = myint is mystring # Noncompliant. Always False
value = myint is not mystring # Noncompliant. Always True

if (myint is None): # Noncompliant. Always False
pass
</pre>

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"tags": [

],
"defaultSeverity": "Major",
"defaultSeverity": "Blocker",
"ruleSpecification": "RSPEC-3403",
"sqKey": "S3403",
"scope": "Main"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ <h2>Noncompliant Code Example</h2>
result = 0
try:
print("foo")
except ValueError as e:
pass
else:
if param:
raise ValueError()
Expand All @@ -26,6 +28,8 @@ <h2>Compliant Solution</h2>
result = 0
try:
print("foo")
except ValueError as e:
pass
else:
if param:
raise ValueError()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"type": "CODE_SMELL",
"status": "ready",
"tags": [

"error-handling",
"unpredictable",
"confusing"
],
"defaultSeverity": "Critical",
"ruleSpecification": "RSPEC-5704",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<p>The special method <a
href="https://docs.python.org/3/reference/datamodel.html?highlight=__exit__%20special#object.__exit__"><code>__exit__</code></a> should only raise an
exception when it fails. I should never raise the provided exception, it is the caller's responsibility.</p>
<p>Raising this exception will make the stack trace difficult to understand.</p>
exception when it fails. It should never raise the provided exception, it is the caller's responsibility.</p>
<p> Raising this exception will make the stack trace difficult to understand.</p>
<p>The <code>__exit__</code> method can filter passed-in exceptions by simply returning True or False.</p>
<p>This rule raises an issue when:</p>
<ul>
<li> an <code>__exit__</code> method has a bare <code>raise</code> outside of an <code>except</code> block. </li>
<li> an <code>__exit__</code> method raises the exception provided as parameter. </li>
</ul>
<p> * an <code>__exit__</code> method has a bare <code>raise</code> outside of an <code>except</code> block.</p>
<p> * an <code>__exit__</code> method raises the exception provided as parameter.</p>
<h2>Noncompliant Code Example</h2>
<pre>
class MyContextManager:
Expand Down Expand Up @@ -44,9 +42,7 @@ <h2>Compliant Solution</h2>
raise MemoryError("No more memory") # This is ok too.
</pre>
<h2>See</h2>
<ul>
<li> Python documentation <del></del> <a href="https://docs.python.org/3/reference/datamodel.html?highlight=__exit__%20special#object.__exit__">The
<code>__exit__</code> special method</a> </li>
<li> PEP 343 <del></del> <a href="https://www.python.org/dev/peps/pep-0343/">The "with" Statement</a> </li>
</ul>
<p> * Python documentation – <a href="https://docs.python.org/3/reference/datamodel.html?highlight=__exit__%20special#object.__exit__">The
<code>__exit__</code> special method</a></p>
<p> * PEP 343 – <a href="https://www.python.org/dev/peps/pep-0343/">The "with" Statement</a></p>

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"type": "CODE_SMELL",
"status": "ready",
"tags": [

"error-handling",
"bad-practice"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-5706",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<code>B.__radd__(A)</code>. This enables adding new operations by changing only one class instead of two.</p>
<p>This rule raises an issue when one of the following methods raises <code>NotImplementedError</code> instead of returning
<code>NotImplemented</code>:</p>
<p> * __lt__(self, other)</p>
<ul>
<li> __lt__(self, other) </li>
<li> __le__(self, other) </li>
<li> __eq__(self, other) </li>
<li> __ne__(self, other) </li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"tags": [
"unused"
],
"defaultSeverity": "Major",
"defaultSeverity": "Minor",
"ruleSpecification": "RSPEC-5713",
"sqKey": "S5713",
"scope": "All"
Expand Down
2 changes: 1 addition & 1 deletion 2 sonarpedia.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"languages": [
"PY"
],
"latest-update": "2020-02-28T12:47:20.661522Z",
"latest-update": "2020-03-09T13:24:26.631386Z",
"options": {
"no-language-in-filenames": true,
"preserve-filenames": true
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.