Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added try-except-raise message example #6540

Merged
merged 4 commits into from
May 9, 2022
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
4 changes: 4 additions & 0 deletions doc/data/messages/t/try-except-raise/bad.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
try:
1 / 0
except ZeroDivisionError as e: # [try-except-raise]
raise
18 changes: 18 additions & 0 deletions doc/data/messages/t/try-except-raise/details.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
There is a legitimate use case for re-raising immediately. E.g. with the following inheritance tree::

+-- ArithmeticError
+-- FloatingPointError
+-- OverflowError
+-- ZeroDivisionError

The following code shows valid case for re-raising exception immediately::

def execute_calculation(a, b):
try:
return some_calculation(a, b)
except ZeroDivisionError:
raise
except ArithmeticError:
return float('nan')

The pylint is able to detect this case and does not produce error.
8 changes: 8 additions & 0 deletions doc/data/messages/t/try-except-raise/good.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# The try except might be removed entirely:
1 / 0

# Or another more detailed exception can be raised:
try:
1 / 0
except ZeroDivisionError as e:
raise ValueError("The area of the rectangle cannot be zero") from e