From 8142fa8ac2754dc6c5f74ec122fca39dd1b83f65 Mon Sep 17 00:00:00 2001 From: vicky-dx Date: Tue, 2 Dec 2025 23:01:37 +0530 Subject: [PATCH 1/2] Add test coverage for compiled regex patterns in pytest.raises() --- AUTHORS | 1 + testing/python/raises.py | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/AUTHORS b/AUTHORS index a089ca678f7..fd7c132a4a9 100644 --- a/AUTHORS +++ b/AUTHORS @@ -337,6 +337,7 @@ Niclas Olofsson Nicolas Delaby Nicolas Simonds Nico Vidal +Nikesh Chavhan Nikolay Kondratyev Nipunn Koorapati Oleg Pidsadnyi diff --git a/testing/python/raises.py b/testing/python/raises.py index c9d57918a83..6b2a765e7fb 100644 --- a/testing/python/raises.py +++ b/testing/python/raises.py @@ -407,3 +407,26 @@ def test_issue_11872(self) -> None: code=404, msg="Not Found", fp=io.BytesIO(), hdrs=Message(), url="" ) exc_info.value.close() # avoid a resource warning + + def test_raises_match_compiled_regex(self) -> None: + """Test that compiled regex patterns work with pytest.raises.""" + # Test with a compiled pattern that matches + pattern = re.compile(r"with base \d+") + with pytest.raises(ValueError, match=pattern): + int("asdf") + + # Test with a compiled pattern that doesn't match + pattern_nomatch = re.compile(r"with base 16") + expr = ( + "Regex pattern did not match.\n" + f" Expected regex: {pattern_nomatch.pattern!r}\n" + f" Actual message: \"invalid literal for int() with base 10: 'asdf'\"" + ) + with pytest.raises(AssertionError, match="^" + re.escape(expr) + "$"): + with pytest.raises(ValueError, match=pattern_nomatch): + int("asdf", base=10) + + # Test compiled pattern with flags + pattern_with_flags = re.compile(r"INVALID LITERAL", re.IGNORECASE) + with pytest.raises(ValueError, match=pattern_with_flags): + int("asdf") From 5d5db6062f95b19c1c3280095e98c5780412347e Mon Sep 17 00:00:00 2001 From: vicky-dx Date: Tue, 2 Dec 2025 23:06:37 +0530 Subject: [PATCH 2/2] Add changelog entry for #14026 --- changelog/14026.improvement.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/14026.improvement.rst diff --git a/changelog/14026.improvement.rst b/changelog/14026.improvement.rst new file mode 100644 index 00000000000..7025ba80481 --- /dev/null +++ b/changelog/14026.improvement.rst @@ -0,0 +1 @@ +Added test coverage for compiled regex patterns in :func:`pytest.raises` match parameter.