From 10748b2fdb021e4d8e548f09e8e182c69d7c1f5a Mon Sep 17 00:00:00 2001 From: mahiro <70263039+mahiro72@users.noreply.github.com> Date: Fri, 19 Dec 2025 00:42:06 +0900 Subject: [PATCH] [`flake8-pytest-style`] Allow `match` and `check` keyword arguments without an expected exception type (`PT010`) (#21964) ## Summary 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 - Added test cases for `match`-only, `check`-only, and both arguments. - `cargo test -p ruff_linter -- "pytestraiseswithoutexception"` passes --- .../test/fixtures/flake8_pytest_style/PT010.py | 12 ++++++++++++ .../src/rules/flake8_pytest_style/rules/raises.rs | 5 +++++ 2 files changed, 17 insertions(+) diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_pytest_style/PT010.py b/crates/ruff_linter/resources/test/fixtures/flake8_pytest_style/PT010.py index e6cc58cbc3..4191976043 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_pytest_style/PT010.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_pytest_style/PT010.py @@ -9,3 +9,15 @@ def test_ok(): def test_error(): with pytest.raises(UnicodeError): pass + +def test_match_only(): + with pytest.raises(match="some error message"): + pass + +def test_check_only(): + with pytest.raises(check=lambda e: True): + pass + +def test_match_and_check(): + with pytest.raises(match="some error message", check=lambda e: True): + pass diff --git a/crates/ruff_linter/src/rules/flake8_pytest_style/rules/raises.rs b/crates/ruff_linter/src/rules/flake8_pytest_style/rules/raises.rs index 04c9a8c372..c9e3e8e702 100644 --- a/crates/ruff_linter/src/rules/flake8_pytest_style/rules/raises.rs +++ b/crates/ruff_linter/src/rules/flake8_pytest_style/rules/raises.rs @@ -125,6 +125,9 @@ impl Violation for PytestRaisesTooBroad { /// ## Why is this bad? /// `pytest.raises` expects to receive an expected exception as its first /// argument. If omitted, the `pytest.raises` call will fail at runtime. +/// The rule will also accept calls without an expected exception but with +/// `match` and/or `check` keyword arguments, which are also valid after +/// pytest version 8.4.0. /// /// ## Example /// ```python @@ -181,6 +184,8 @@ pub(crate) fn raises_call(checker: &Checker, call: &ast::ExprCall) { .arguments .find_argument("expected_exception", 0) .is_none() + && call.arguments.find_keyword("match").is_none() + && call.arguments.find_keyword("check").is_none() { checker.report_diagnostic(PytestRaisesWithoutException, call.func.range()); }