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

I want a rule that will check for pytest.warns contexts with multiple statements #317

Open
tjkuson opened this issue Nov 17, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@tjkuson
Copy link

tjkuson commented Nov 17, 2024

Rule request

This would be analogous to PT012 which checks for pytest.raises contexts with multiple statements. If accepted, I volunteer to implement the check.

Description (adapted from the PT012 documentation)

Bad code:

import pytest

def test_foo():
    with pytest.warns(UserWarning):
        obj = get_object()
        obj.do_something()

    with pytest.warns(UserWarning):
        if some_condition:
            do_something()

Good code:

import pytest

def test_foo():
    obj = get_object()
    with pytest.warns(UserWarning):
        obj.do_something()

An empty with-statement (containing only a pass inside) is allowed to allow testing of exceptions raised by context manager enter/exit methods.

import pytest

def test_my_context_manager():
    context_manager = get_context_manager()
    with pytest.warns(UserWarning):
        with context_manager:
            pass

Rationale

  • to help ensure that only the expected warning from code under test is caught in a pytest.warns block
@snowdrop4
Copy link

snowdrop4 commented Nov 22, 2024

This lint makes a lot of sense.

There could also be pytest.warns versions of PT010 and PT011 (not just PT012), since they are all analagous.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants