astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
33.13k stars 1.11k forks source link

Extend `pytest-raises-with-multiple-statements (PT012)` to cover `pytest.warns` #14239

Open harupy opened 2 weeks ago

harupy commented 2 weeks ago

pytest-raises-with-multiple-statements only covers pytest.raises. It'd be nice if it covers pytest.warns as well.

import pytest

def test_raises():
    with pytest.raises(Exception):  # PT012
        f3()
        f4()

def test_warns():
    with pytest.warns(UserWarning):
        f1()  # does this warn?
        f2()  # or perhaps this one warns?
        # -> Not obvious which line warns.

Playground to ensure pytest.warns is not covered: https://play.ruff.rs/271e2f8e-751a-4368-971c-322eeeb10b39

References:

harupy commented 2 weeks ago

Adding a new rule is another option.

MichaReiser commented 2 weeks ago

I think that would have to be its own rule unless we rename the existing rule

harupy commented 2 weeks ago

I think that would have to be its own rule unless we rename the existing rule

Makes sense.

harupy commented 2 weeks ago

@MichaReiser do we need to add the same rule in https://github.com/m-burst/flake8-pytest-style to add it in ruff?

MichaReiser commented 2 weeks ago

I think it should be fine. We just need to make sure that we use a rule code that's unlikely to collide with any new rule added to the upstream plugin

tjkuson commented 1 week ago

Created an issue in the upstream repo: https://github.com/m-burst/flake8-pytest-style/issues/317

MichaReiser commented 1 week ago

Thanks @tjkuson

snowdrop4 commented 4 days ago

This lint makes a lot of sense to me.

I'll make a draft branch.

I'll just use a placeholder rule code until the flake8 plugin decides on one.

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

snowdrop4 commented 4 days ago

I have a finished branch: https://github.com/snowdrop4/ruff/tree/AVK/PytestWarnsWithMultipleStatements

But yeah, I'll wait and see what the flake8 plugin people say before PRing.