Skip to content
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

PERF203 should only trigger for exception handler that unconditionally raises #5858

Closed
NeilGirdhar opened this issue Jul 18, 2023 · 2 comments · Fixed by #6145
Closed

PERF203 should only trigger for exception handler that unconditionally raises #5858

NeilGirdhar opened this issue Jul 18, 2023 · 2 comments · Fixed by #6145
Labels
needs-info More information is needed from the issue author

Comments

@NeilGirdhar
Copy link

NeilGirdhar commented Jul 18, 2023

I think that PERF203 should only trigger when the exception handler unconditionally raises.

for i in range(10):
    try:
        print(1 / 0)
    except Exception:  # a.py:4:5: PERF203 `try`-`except` within a loop incurs performance overhead
        print("oops")
    print("2")

Although, this is not a priority for me since I decided to disable it. It never caught anything useful for me.

@charliermarsh charliermarsh added the needs-info More information is needed from the issue author label Jul 19, 2023
@charliermarsh
Copy link
Member

For completeness -- could you give an example of a case that should and shouldn't be caught, based on this criteria?

@NeilGirdhar
Copy link
Author

Where it should not be caught is, in my opinion constructs like the one displayed above. The problem is that this error can't be acted on for such an example. You can't move the try/except outside the loop since that would make an exception skip everything that follows the try/except.

As for where it should be caught, I think it's tricky because there's a balance. While it is more efficient to have try/except around the loop, it means that more code is protected by the try/except, which is a downside. Ideally, you want the minimum amount of code in a try/except.

I'm not sure what others like, but personally I don't find this error useful in the first place since it is trying to add more code to the try/except.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-info More information is needed from the issue author
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants