-
Notifications
You must be signed in to change notification settings - Fork 921
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
PT017 (pytest-assert-in-except) differs from flake8 #11869
Comments
Yeah I think this is a bug. |
Hmm, I think the snippet here could be written more idiomatically and more correctly as: def test_foo():
x = 0
with pytest.raises(ZeroDivisionError) as exc_info:
1 / x
assert x == 0, exc_info.value
assert exc_info.args I'm not sure I fully understand why we should only emit one error for this snippet rather than two. Both of the assertions here are assertions inside |
To me the difference is that only the |
But I think you still have the footgun where if no exception is actually raised in the |
I'm confused by the rule documentation vs the message. In the documentation, we say we're catching all assertions in except blocks but in the message we say we're catching an assertion on the exception. The latter also matches the rule name. I think I need more context on why we only flag assertions that include the exception before I can say who I agree with :) |
Yeah, I considered just updating the message but the problem is that the lint rule only catches That's where I think the rule title and documentation says one thing, but the implementation actually does something entirely else. In the context of the implementation, I would not expect the rule to raise a violation. |
The only rationale given for this rule in the docs for the original
That feels like it applies equally well to the two assertions in the example snippet.
I see, but I'm not really sure why a user would want the current behaviour. I don't feel like assertions on exceptions themselves in In my opinion, the better fix would be to revise the rule so that it highlights more |
I'm not speaking in favor of the current implementation. I just tried to explain that the documentation and implementation aren't matching. Or at least, I would understand a different behavior from the rule documentation than is implemented today. |
cc @harupy who implemented the rule. |
I agree. The documentation doesn't match the implementation. I would expect the following code to violate PT017, but it doesn't. def test_abc():
try:
foo()
except Exception:
bar() |
Ruff flags the
assert x == 0, e
line asPT017(pytest-assert-in-except)
even though the exception is not part of the predicate.Flake8 only flags the
assert e.args
line.This looks like a false-positive in ruff rather than a bug in flake8?
Ruff:
Flake8:
The text was updated successfully, but these errors were encountered: