-
Notifications
You must be signed in to change notification settings - Fork 179
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
Check bare raise occurs in except clause #57
base: main
Are you sure you want to change the base?
Conversation
Any thoughts on the below case? I haven't seen/written code like this before, but it seems like valid code that would result in a false positive from pyflakes with this pull request. def foo():
raise
try:
raise Exception()
except:
foo() |
On one hand, I can't find anything in the 2.7 language reference that says
On the other hand, in the example above I'm not sure we could say the exception that is re-raised by def foo():
raise
def bar():
try:
raise Exception()
except:
foo() Certainly in this case, Probably it's a reasonable assumption that the behavior of a
In this interpretation, the calling stack frame(s) can determine whether a bare |
"""Check if node has any parent of type cls.""" | ||
while node: | ||
try: | ||
node = self.getParent(node) |
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.
What's the difference between node.parent and self.getParent(node)?
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 ask because CONTINUE
below uses node.parent
. I can't remember if I didn't use getParent
on purpose or not.
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.
getParent
also skips some parents when if not hasattr(node, 'elts') and not hasattr(node, 'ctx')
is not True.
However, not isnt really needed in this case, and inline-ing getParent
will like be faster, or at least more clear anyway.
Also, this routine is probably better named has_ancestor_type
.
What about Python 3. I seem to remember this giving a RuntimeError. |
The only difference in Python 3 I noticed was it raises RuntimeError instead of TypeError in the cases where there's no active exception. Otherwise it's the same: the bare |
Yeah it looks like it even works at the module scope rse.py: print("raising")
raise test.py: try:
raise ValueError
except ValueError:
import rse
What an obscure Python feature. |
regarding re- I would expect anyone using that 'feature' would happily adjust their code to def fu():
exc = sys.exc_info()
raise exc[0], exc[1], exc[2] I obviously think pyflakes should report insane code, even if it could be legally used. flake8 users can always add a That said, of course it is possible to reduce when this new error occurs.
If pyflakes isnt interested in preventing bare raise in |
-1 because this would give people the incorrect impression that this sort of thing isn't allowed, when it actually is. |
As a matter of design principle I can't accept a change that emits an error on legitimate code, even if that code is obscure or odd. Thanks for the PR though, I did learn something new about Python. |
I agree that it doesn't belong in
try:
raise
except:
pass
|
I've checked ~200 projects I have a local copy of. 73 hits in 19 projects:
There are zero cases of re-raise at module level not in The only case of bare raise in a try, finally or else blocks of a try except clause is: |
OK, I concede that's a pretty good argument. I'm reopening the PR for consideration. But I also have to get ready for holiday parties so I can't say much more right now. I will say one thing that would make me feel better is if this issue were raised on the Python mailing list. I don't have time to do that myself right now. It does very much seem like a thing which works by mistake, which is a bad idea almost always. The language reference is unclear, so I think either the language reference needs clarification, or we've uncovered a bug in Python (or at least something that can be acknowledged as unspecified behavior). |
Thanks. Bub is awake, so I am also out of time for now at least, and also wont get much time over the next few days. When I get time, I'll first revise the PR to handle those two specific cases only, so we can resume the discussion about those specific cases (including widening that discussion to include the Python mailing list) and can test those specific cases against more repos. |
Fixes lp:1528539