mirror of
https://github.com/astral-sh/ruff
synced 2026-01-23 14:30:53 -05:00
<!-- Thank you for contributing to Ruff! To help us out with reviewing, please consider the following: - Does this pull request include a summary of the change? (See below.) - Does this pull request include a descriptive title? - Does this pull request include references to any relevant issues? --> This PR aims to close #16605. ## Summary This PR introduces a new rule (`RUF061`) that detects non-contextmanager usage of `pytest.raises`, `pytest.warns`, and `pytest.deprecated_call`. This pattern is discouraged and [was proposed in flake8-pytest-style](https://github.com/m-burst/flake8-pytest-style/pull/332), but the corresponding PR has been open for over a month without activity. Additionally, this PR provides an unsafe fix for simple cases where the non-contextmanager form can be transformed into the context manager form. Examples of supported patterns are listed in `RUF061_raises.py`, `RUF061_warns.py`, and `RUF061_deprecated_call.py` test files. The more complex case from the original issue (involving two separate statements): ```python excinfo = pytest.raises(ValueError, int, "hello") assert excinfo.match("^invalid literal") ``` is getting fixed like this: ```python with pytest.raises(ValueError) as excinfo: int("hello") assert excinfo.match("^invalid literal") ``` Putting match in the raises call requires multi-statement transformation, which I am not sure how to implement. ## Test Plan <!-- How was it tested? --> New test files were added to cover various usages of the non-contextmanager form of pytest.raises, warns, and deprecated_call.