[raise-missing-from] Clearer message and example in the documentation#6576
Conversation
Pull Request Test Coverage Report for Build 2308633827
💛 - Coveralls |
DanielNoord
left a comment
There was a problem hiding this comment.
Agree with the original issue that the message description can be a lot better. Then we can probably also remove the details.rst.
| ), | ||
| "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.
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.
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.
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.
Say we have this:
try:
1 / 0
except ZeroDivisionError:
# +1: [raise-missing-from]
raise KeyErrorname_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 eSo 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.
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.
👍 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.
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.
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
|
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 <ram@rachum.com> Co-authored-by: Daniël van Noord <13665637+DanielNoord@users.noreply.github.com> 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 ram@rachum.com
Refs #5953
Closes #3707