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

Don't raise PT017 if exception is used in assertion message #11873

Closed
wants to merge 1 commit into from

Conversation

MichaReiser
Copy link
Member

Summary

Fixes #11869

Test Plan

Added test

@MichaReiser MichaReiser added the bug Something isn't working label Jun 14, 2024
Copy link
Contributor

github-actions bot commented Jun 14, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Comment on lines +15 to +20
def allow_exception_in_message():
x = 0
try:
1 / x
except ZeroDivisionError as e:
assert x == 0, e
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me this feels like exactly the kind of thing the rule is trying to catch, rather than something we should avoid emitting a diagnostic for: I commented at #11869 (comment). If you want to use the value of the exception in the assertion message, you can do that using pytest.raises():

def allow_exception_in_message():
    x = 0
    with pytest.raises(ZeroDivisionError) as exc_info:
        1 / x
    assert x == 0, exc_info.value

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm leaning towards agreeing with Alex here

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 this pull request may close these issues.

PT017 (pytest-assert-in-except) differs from flake8
3 participants