From 118a9221e0ebc248c22be8c3fcd1834168f4c5ee Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sat, 5 Nov 2022 07:53:53 -0700 Subject: [PATCH 1/2] unittest: cannot use bytes regexes >>> from unittest.case import TestCase >>> c = TestCase() >>> with c.assertRaisesRegex(Exception, b"x"): 1/0 ... ZeroDivisionError: division by zero During handling of the above exception, another exception occurred: Traceback (most recent call last): File "", line 1, in File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/unittest/case.py", line 274, in __exit__ if not expected_regex.search(str(exc_value)): ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: cannot use a bytes pattern on a string-like object --- stdlib/unittest/case.pyi | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/stdlib/unittest/case.pyi b/stdlib/unittest/case.pyi index 200f8dbaea23..d7a874e32e18 100644 --- a/stdlib/unittest/case.pyi +++ b/stdlib/unittest/case.pyi @@ -157,7 +157,7 @@ class TestCase: def assertRaisesRegex( # type: ignore[misc] self, expected_exception: type[BaseException] | tuple[type[BaseException], ...], - expected_regex: str | bytes | Pattern[str] | Pattern[bytes], + expected_regex: str | Pattern[str], callable: Callable[..., Any], *args: Any, **kwargs: Any, @@ -166,7 +166,7 @@ class TestCase: def assertRaisesRegex( self, expected_exception: type[_E] | tuple[type[_E], ...], - expected_regex: str | bytes | Pattern[str] | Pattern[bytes], + expected_regex: str | Pattern[str], *, msg: Any = ..., ) -> _AssertRaisesContext[_E]: ... @@ -186,7 +186,7 @@ class TestCase: def assertWarnsRegex( # type: ignore[misc] self, expected_warning: type[Warning] | tuple[type[Warning], ...], - expected_regex: str | bytes | Pattern[str] | Pattern[bytes], + expected_regex: str | Pattern[str], callable: Callable[_P, Any], *args: _P.args, **kwargs: _P.kwargs, @@ -195,7 +195,7 @@ class TestCase: def assertWarnsRegex( self, expected_warning: type[Warning] | tuple[type[Warning], ...], - expected_regex: str | bytes | Pattern[str] | Pattern[bytes], + expected_regex: str | Pattern[str], *, msg: Any = ..., ) -> _AssertWarnsContext: ... From 43b43a4043e1bb53ca0833191eb3616b246eb6a4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 5 Nov 2022 14:56:11 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/unittest/case.pyi | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/stdlib/unittest/case.pyi b/stdlib/unittest/case.pyi index d7a874e32e18..c75539a97368 100644 --- a/stdlib/unittest/case.pyi +++ b/stdlib/unittest/case.pyi @@ -164,11 +164,7 @@ class TestCase: ) -> None: ... @overload def assertRaisesRegex( - self, - expected_exception: type[_E] | tuple[type[_E], ...], - expected_regex: str | Pattern[str], - *, - msg: Any = ..., + self, expected_exception: type[_E] | tuple[type[_E], ...], expected_regex: str | Pattern[str], *, msg: Any = ... ) -> _AssertRaisesContext[_E]: ... @overload def assertWarns( # type: ignore[misc] @@ -193,11 +189,7 @@ class TestCase: ) -> None: ... @overload def assertWarnsRegex( - self, - expected_warning: type[Warning] | tuple[type[Warning], ...], - expected_regex: str | Pattern[str], - *, - msg: Any = ..., + self, expected_warning: type[Warning] | tuple[type[Warning], ...], expected_regex: str | Pattern[str], *, msg: Any = ... ) -> _AssertWarnsContext: ... def assertLogs( self, logger: str | logging.Logger | None = ..., level: int | str | None = ...