rspec/rules/S5747/python/rule.adoc

79 lines
2.1 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
A bare ``++raise++`` statement, i.e. a ``++raise++`` with no exception provided, will re-raise the last active exception in the current scope. If the "raise" statement is not in an ``++except++`` or ``++finally++`` block, no exception is active and a ``++RuntimeError++`` is raised instead.
If the bare ``++raise++`` statement is in a function called in an ``++except++`` statement, the exception caught by the ``++except++`` will be raised. This works but is hard to understand and maintain. Nothing indicates in the parent ``++except++`` that the exception will be reraised, and nothing prevents a developer from calling the function in another context.
Note also that using a bare ``++raise++`` in a ``++finally++`` block only works when an exception is active, i.e. when an exception from the ``++try++`` block is not caught or when an exception is raised by an ``++except++`` block. It will fail in any other case and should not be relied upon. This code smell is handled by rule S5704.
This rule raises an exception when a bare ``++raise++`` statement is not in an ``++except++`` or ``++finally++`` block.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,python]
2021-04-28 16:49:39 +02:00
----
raise # Noncompliant
def foo():
raise # Noncompliant
try:
raise # Noncompliant
except ValueError as e:
handle_error()
except:
raise
else:
raise # Noncompliant
finally:
raise
def handle_error():
raise # Noncompliant. This works but is hard to understand.
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,python]
2021-04-28 16:49:39 +02:00
----
raise ValueError()
def foo():
raise ValueError()
try:
raise ValueError()
except:
raise
else:
raise ValueError()
finally:
raise
----
== Resources
2021-04-28 16:49:39 +02:00
* Python Documentation - https://docs.python.org/3/reference/simple_stmts.html#raise[The ``++raise++`` statement]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
include::highlighting.adoc[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]