-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add rule raise-missing-from #3695
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
Add rule raise-missing-from #3695
Conversation
95c5adf
to
a59fd6e
Compare
@Pierre-Sassoulas @PCManticore Here's my attempt to implement this feature. This is my first PyLint rule, so I'm likely to miss important things. Also I didn't write the documentation yet, because I need to know whether I'm even on the right track with this implementation. Let me know. |
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.
The codes looks fine to me, good work ! I just think there should be two or three more functional tests (using "from" explicitly to reraise). I made some suggestions.
45151fa
to
7998fa0
Compare
@Pierre-Sassoulas See the |
7998fa0
to
5b561ac
Compare
1ea39ff
to
d363377
Compare
d363377
to
4cb43b8
Compare
@Pierre-Sassoulas Any idea why Travis doesn't seem to be running? |
It can be long to launch some times. If it's still not running running you can force push with a new date for the commit to trigger the pipeline. |
I did the force push, it didn't help. |
4cb43b8
to
f5e1485
Compare
https://travis-ci.org/github/PyCQA/pylint/builds/699060729 here's the build |
@Pierre-Sassoulas That doesn't make sense. I'm getting this error in the build:
Even though I already fixed it in my code. Could it be running on an old version? |
If nothing in |
f5e1485
to
b88e989
Compare
Cool, fixed. I thought of something. This check should only be applied in Python 3 codebases. Should I mark it in some way as a Python 3-only check? |
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.
Super nice PR @cool-RR ! Thanks a lot. No worries regarding marking this as a Python 3 checker, as pylint already runs only on Python 3.
pylint/checkers/exceptions.py
Outdated
@@ -76,6 +76,26 @@ def _is_raising(body: typing.List) -> bool: | |||
return False | |||
|
|||
|
|||
def iterate_ancestors(node: NodeNG) -> typing.Iterator[NodeNG]: |
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.
These two functions can be replaced with pylint.checkers.utils.find_try_except_wrapper_node
:
except_handler = find_try_except_wrapper_node(node)
if isinstance(except_handler, astroid.ExceptHandler):
# it's found
On a quick look that function will need some adjustments to make it work for this use case, but it's better than having another except handler searching routine in the code base.
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'll need 2 adjustments. One is adding an argument stop_on_function: bool = False
and logic for it, that's pretty straightforward. Also consider that it's possible that the fact we haven't had this logic until now causes bugs in other checks, as code that's in a function defined in try-except doesn't have the same behavior as code that's actually in try-except. I won't dwell on that, you can open an issue on that if you'd like.
The second would be for it to find just the except
clause, and not the try
clause. Now, I don't know what kind of API you want for that. I can add an argument only_except: bool = False
but that's not a nice API. Let me know which API you'd like.
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 fixed the merge conflict. Besides this issue that I'm waiting on a response on from you, is there anything else that needs to be done before this PR can be merged?
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.
Nudge.
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.
To answer your question:
- The function should only look in the current scope so it should stop if it encounters a
FunctionDef
or aClassDef
. - We can split this function into two, one for finding
TryExcept
nodes, another for finding the parentExceptHandler
, or we can rely onisinstance
after the function call returned to deduce the same thing.
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.
We can split this function into two, one for finding
TryExcept
nodes, another for finding the parentExceptHandler
.
Okay, I pushed that now.
b88e989
to
4e08206
Compare
1b04ea7
to
6b5bfa0
Compare
6b5bfa0
to
fb7d5cf
Compare
Woohoo! |
* Format all Python files with latest version of black (20.8b1). * Add `from` keyword when re-raising exceptions. * pylint W0707: raise-missing-from * https://pylint.readthedocs.io/en/latest/technical_reference/features.html * pylint-dev/pylint#3695
Steps
doc/whatsnew/<current release.rst>
.Description
Implement #3687
Type of Changes