Skip to content

Commit 10748b2

Browse files
authored
[flake8-pytest-style] Allow match and check keyword arguments without an expected exception type (PT010) (#21964)
## Summary <!-- What's the purpose of the change? What does it do, and why? --> Updates PT010(`pytest-raises-without-exception`) to recognize `match` and `check` keyword arguments as valid alternatives to specifying an exception class. As of pytest 8.4.0, `pytest.raises()` can be called with only `match` or `check` keyword arguments without an expected exception. Fixes #18653 ## Test Plan <!-- How was it tested? --> - Added test cases for `match`-only, `check`-only, and both arguments. - `cargo test -p ruff_linter -- "pytestraiseswithoutexception"` passes
1 parent 56539db commit 10748b2

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

  • crates/ruff_linter
    • resources/test/fixtures/flake8_pytest_style
    • src/rules/flake8_pytest_style/rules

crates/ruff_linter/resources/test/fixtures/flake8_pytest_style/PT010.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,15 @@ def test_ok():
99
def test_error():
1010
with pytest.raises(UnicodeError):
1111
pass
12+
13+
def test_match_only():
14+
with pytest.raises(match="some error message"):
15+
pass
16+
17+
def test_check_only():
18+
with pytest.raises(check=lambda e: True):
19+
pass
20+
21+
def test_match_and_check():
22+
with pytest.raises(match="some error message", check=lambda e: True):
23+
pass

crates/ruff_linter/src/rules/flake8_pytest_style/rules/raises.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ impl Violation for PytestRaisesTooBroad {
125125
/// ## Why is this bad?
126126
/// `pytest.raises` expects to receive an expected exception as its first
127127
/// argument. If omitted, the `pytest.raises` call will fail at runtime.
128+
/// The rule will also accept calls without an expected exception but with
129+
/// `match` and/or `check` keyword arguments, which are also valid after
130+
/// pytest version 8.4.0.
128131
///
129132
/// ## Example
130133
/// ```python
@@ -181,6 +184,8 @@ pub(crate) fn raises_call(checker: &Checker, call: &ast::ExprCall) {
181184
.arguments
182185
.find_argument("expected_exception", 0)
183186
.is_none()
187+
&& call.arguments.find_keyword("match").is_none()
188+
&& call.arguments.find_keyword("check").is_none()
184189
{
185190
checker.report_diagnostic(PytestRaisesWithoutException, call.func.range());
186191
}

0 commit comments

Comments
 (0)