-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[raise-missing-from] Clearer message and example in the documentation #6576
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
[raise-missing-from] Clearer message and example in the documentation #6576
Conversation
Pull Request Test Coverage Report for Build 2308633827
💛 - Coveralls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree with the original issue that the message description can be a lot better. Then we can probably also remove the details.rst
.
pylint/checkers/exceptions.py
Outdated
@@ -131,7 +131,7 @@ def _is_raising(body: list) -> bool: | |||
"try-except-raise block!", | |||
), | |||
"W0707": ( | |||
"Consider explicitly re-raising using the 'from' keyword", | |||
"Consider explicitly re-raising using 'raise NewError(...) from old_error'", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would probably be a lot more user-friendly if we passed old_error
as an argument and showed the actual error that was caught. I think we have that information when we add_message
, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I started doing that but then we can have issues when there is no except ZeroDivisionError as e:
but except ZeroDivisionError:
instead, what should we use for old_error
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
diff --git a/pylint/checkers/exceptions.py b/pylint/checkers/exceptions.py
index 7e182c3dc..26024f8aa 100644
--- a/pylint/checkers/exceptions.py
+++ b/pylint/checkers/exceptions.py
@@ -336,6 +336,12 @@ class ExceptionsChecker(checkers.BaseChecker):
if containing_except_node.name is None:
# The `except` doesn't have an `as exception:` part, meaning there's no way that
# the `raise` is raising the same exception.
+ if isinstance(containing_except_node.type, nodes.Name):
+ name_of_old_error = containing_except_node.type.name
+ elif isinstance(containing_except_node.type, nodes.Tuple):
+ name_of_old_error = ", ".join(
+ n.name for n in containing_except_node.type.elts
+ )
self.add_message("raise-missing-from", node=node)
elif isinstance(node.exc, nodes.Call) and isinstance(node.exc.func, nodes.Name):
# We have a `raise SomeException(whatever)`.
We have access to the wrapping except handler and can determine the node name from it.
There are three other places at which the message is raised but I think we should be able to get the names there as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Say we have this:
try:
1 / 0
except ZeroDivisionError:
# +1: [raise-missing-from]
raise KeyError
name_of_old_error
would be ZeroDivisionError
when we would want it to be e
or something so the end result is this:
try:
1 / 0
except ZeroDivisionError as e:
raise KeyError from e
So the message should be something like "Consider explicitly re-raising using 'raise KeyError from old_error'",
and if we have except ZeroDivisionError as exc:
in the original code it should be Consider explicitly re-raising using 'raise KeyError from exc"
, right ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah. I think we should potentially rephrase the message. What about:
Consider explicitly re-raising using 'except %s as exc' and 'raise from exc'
That should work for both situations right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 I don't know if we can do Consider explicitly re-raising using 'except %s as exc' and 'raise%s from exc'
with the second args being KeyError
or ValueError("my message")
(depending on what the code is originally), but Consider explicitly re-raising using 'except %s as exc' and 'raise ... from exc'
is probably good enough ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I think the ...
works. Messages shouldn't be too long I think so that seems like a nice middle ground.
Is exc
the most common name to do this? Docs seems to use err
and exc
: https://docs.python.org/3/tutorial/errors.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A small refactor permitted to have a nice little message. At this point we might as well autofix files 😄 Something to consider when we have less than 10 issues opened
40f6e2a
to
c8cce4e
Compare
44f5b51
to
b9f9006
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Final nitpicks
Sorry for pushing something else after the review, I completed the coverage and fixed the suggestion for multiple exceptions caught. |
I don't know if the failure was discussed above, but I found it in the primer logs:
|
Co-authored-by: cool-RR <[email protected]> Co-authored-by: Daniël van Noord <[email protected]> Refs pylint-dev#5953 Closes pylint-dev#3707
452ceb8
to
642b18d
Compare
I fixed-up what was previously reviewed, the new code is in 642b18d |
Type of Changes
Description
Co-authored-by: @cool-RR [email protected]
Refs #5953
Closes #3707