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

TRY301 disregards whether the raised exeption type is actually caught or not #5246

Closed
runfalk opened this issue Jun 21, 2023 · 1 comment · Fixed by #5455
Closed

TRY301 disregards whether the raised exeption type is actually caught or not #5246

runfalk opened this issue Jun 21, 2023 · 1 comment · Fixed by #5455
Assignees
Labels
bug Something isn't working

Comments

@runfalk
Copy link

runfalk commented Jun 21, 2023

I believe the following is a false positive:

# example.py
try:
    raise ValueError("foo")
except TypeError:
    print("We don't catch ValueError")
$ poetry run ruff example.py 
example.py:3:5: TRY301 Abstract `raise` to an inner function
Found 1 errors.

I would expect TRY301 only if we actually caught the ValueError.

Relevant metadata

$ poetry run python --version
Python 3.11.3
$ poetry run ruff --version
ruff 0.0.274
[tool.ruff]
target-version = "py311"

select = [
    # ...
    "TRY",  # tryceratops
    # ...
]
ignore = [
    # ...

    # tryceratops
    # (these options are probably irrelevant)
    # Allow long inline exception messages
    "TRY003",
    # Allow return statements in try blocks
    "TRY300",
]
@charliermarsh charliermarsh added the bug Something isn't working label Jun 21, 2023
@evanrittenhouse
Copy link
Contributor

You can assign this to me

charliermarsh pushed a commit that referenced this issue Jul 10, 2023
## Summary

Fixes #5246. We generate a hash set of all exception IDs caught by the
`try` statement, then check that the inner `raise` actually raises a
caught exception.

## Test Plan

Added a new test, `cargo t`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants