-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Prevent usage of return
, yield
in try/finally
#8260
Comments
Sounds like a good idea to me. I did a quick search of a variety of repos and I couldn't find a single instance, so it seems to be a rare pattern. |
I would like to solve this issue. |
I assigned you to it @deepgohil :) |
Can you tell me where I have to change the code? I want to know folder name |
You can create or modify a checker in Design by ChatGPT would indicate that it could be in the basic checker:
But I think that ChatGPT is basic and might be giving bad design advise here. I don't know where it should really go though. Or if it should really be called 'return-superseded-by-finally'. |
What should be in the finally block instead of return? |
The finally block should be removed for example like this: def add(a: int, b: int) -> int:
try:
return a + b
except:
return 0 @deepgohil do you have an opinion regarding the name and the checker it should go in ? (maybe a new one ?) |
I'd say in general: I'm also wondering where to put this new rules 🙂 |
Regarding the name it could also be |
|
Since nobody is working on this, I'd try to come up with something. Let's see how this goes. |
Current problem
Code in
finally
blocks will always be executed. If there's areturn
inside thetry
, it's possible to overwrite this return value with areturn
in thefinally
.Desired solution
There should not be any
return
oryield
statements in a finally block.Additional context
https://docs.python.org/3/reference/compound_stmts.html#finally-clause
The text was updated successfully, but these errors were encountered: