From 0281b3a89f9528970d1c0983b5d450988368a3fc Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Mon, 24 Jan 2022 00:39:29 +0300 Subject: [PATCH 1/6] Use `ParamSpec` in `unittest.case` --- stdlib/unittest/case.pyi | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/stdlib/unittest/case.pyi b/stdlib/unittest/case.pyi index 2ff01a32b90a..1be5d4158114 100644 --- a/stdlib/unittest/case.pyi +++ b/stdlib/unittest/case.pyi @@ -22,6 +22,7 @@ from typing import ( TypeVar, overload, ) +from typing_extensions import ParamSpec from warnings import WarningMessage if sys.version_info >= (3, 9): @@ -29,6 +30,7 @@ if sys.version_info >= (3, 9): _E = TypeVar("_E", bound=BaseException) _FT = TypeVar("_FT", bound=Callable[..., Any]) +_P = ParamSpec("_P") DIFF_OMITTED: str @@ -58,7 +60,7 @@ else: ) -> bool | None: ... if sys.version_info >= (3, 8): - def addModuleCleanup(__function: Callable[..., Any], *args: Any, **kwargs: Any) -> None: ... + def addModuleCleanup(__function: Callable[_P, Any], *args: _P.args, **kwargs: _P.kwargs) -> None: ... def doModuleCleanups() -> None: ... def expectedFailure(test_item: _FT) -> _FT: ... @@ -134,7 +136,7 @@ class TestCase: ) -> _AssertRaisesContext[_E]: ... @overload def assertWarns( # type: ignore[misc] - self, expected_warning: type[Warning] | tuple[type[Warning], ...], callable: Callable[..., Any], *args: Any, **kwargs: Any + self, expected_warning: type[Warning] | tuple[type[Warning], ...], callable: Callable[_P, Any], *args: _P.args, **kwargs: _P.kwargs, ) -> None: ... @overload def assertWarns(self, expected_warning: type[Warning] | tuple[type[Warning], ...], msg: Any = ...) -> _AssertWarnsContext: ... @@ -207,13 +209,13 @@ class TestCase: def id(self) -> str: ... def shortDescription(self) -> str | None: ... if sys.version_info >= (3, 8): - def addCleanup(self, __function: Callable[..., Any], *args: Any, **kwargs: Any) -> None: ... + def addCleanup(self, __function: Callable[_P, Any], *args: _P.args, **kwargs: _P.kwargs) -> None: ... else: - def addCleanup(self, function: Callable[..., Any], *args: Any, **kwargs: Any) -> None: ... + def addCleanup(self, function: Callable[_P, Any], *args: _P.args, **kwargs: _P.kwargs) -> None: ... def doCleanups(self) -> None: ... if sys.version_info >= (3, 8): @classmethod - def addClassCleanup(cls, __function: Callable[..., Any], *args: Any, **kwargs: Any) -> None: ... + def addClassCleanup(cls, __function: Callable[_P, Any], *args: _P.args, **kwargs: _P.kwargs) -> None: ... @classmethod def doClassCleanups(cls) -> None: ... def _formatMessage(self, msg: str | None, standardMsg: str) -> str: ... # undocumented From 7dba7c694f0da004ccdd898aea3df0c152c64658 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 23 Jan 2022 21:41:25 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/unittest/case.pyi | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/stdlib/unittest/case.pyi b/stdlib/unittest/case.pyi index 1be5d4158114..d99f1c42a987 100644 --- a/stdlib/unittest/case.pyi +++ b/stdlib/unittest/case.pyi @@ -136,7 +136,11 @@ class TestCase: ) -> _AssertRaisesContext[_E]: ... @overload def assertWarns( # type: ignore[misc] - self, expected_warning: type[Warning] | tuple[type[Warning], ...], callable: Callable[_P, Any], *args: _P.args, **kwargs: _P.kwargs, + self, + expected_warning: type[Warning] | tuple[type[Warning], ...], + callable: Callable[_P, Any], + *args: _P.args, + **kwargs: _P.kwargs, ) -> None: ... @overload def assertWarns(self, expected_warning: type[Warning] | tuple[type[Warning], ...], msg: Any = ...) -> _AssertWarnsContext: ... From 817c0e2601439b74f27bcdfd587bbfaef069a0a0 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Mon, 24 Jan 2022 00:47:39 +0300 Subject: [PATCH 3/6] Update case.pyi --- stdlib/unittest/case.pyi | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/stdlib/unittest/case.pyi b/stdlib/unittest/case.pyi index d99f1c42a987..0925bc2dd450 100644 --- a/stdlib/unittest/case.pyi +++ b/stdlib/unittest/case.pyi @@ -60,7 +60,7 @@ else: ) -> bool | None: ... if sys.version_info >= (3, 8): - def addModuleCleanup(__function: Callable[_P, Any], *args: _P.args, **kwargs: _P.kwargs) -> None: ... + def addModuleCleanup(__function: Callable[_P, object], *args: _P.args, **kwargs: _P.kwargs) -> None: ... def doModuleCleanups() -> None: ... def expectedFailure(test_item: _FT) -> _FT: ... @@ -112,9 +112,9 @@ class TestCase: def assertRaises( # type: ignore[misc] self, expected_exception: type[BaseException] | tuple[type[BaseException], ...], - callable: Callable[..., Any], - *args: Any, - **kwargs: Any, + callable: Callable[_P, object], + *args: _P.args, + **kwargs: _P.kwargs, ) -> None: ... @overload def assertRaises(self, expected_exception: type[_E] | tuple[type[_E], ...], msg: Any = ...) -> _AssertRaisesContext[_E]: ... @@ -123,9 +123,9 @@ class TestCase: self, expected_exception: type[BaseException] | tuple[type[BaseException], ...], expected_regex: str | bytes | Pattern[str] | Pattern[bytes], - callable: Callable[..., Any], - *args: Any, - **kwargs: Any, + callable: Callable[_P, object], + *args: _P.args, + **kwargs: _P.kwargs, ) -> None: ... @overload def assertRaisesRegex( @@ -138,7 +138,7 @@ class TestCase: def assertWarns( # type: ignore[misc] self, expected_warning: type[Warning] | tuple[type[Warning], ...], - callable: Callable[_P, Any], + callable: Callable[_P, object], *args: _P.args, **kwargs: _P.kwargs, ) -> None: ... @@ -149,9 +149,9 @@ class TestCase: self, expected_warning: type[Warning] | tuple[type[Warning], ...], expected_regex: str | bytes | Pattern[str] | Pattern[bytes], - callable: Callable[..., Any], - *args: Any, - **kwargs: Any, + callable: Callable[_P, object], + *args: _P.args, + **kwargs: _P.kwargs, ) -> None: ... @overload def assertWarnsRegex( @@ -213,13 +213,13 @@ class TestCase: def id(self) -> str: ... def shortDescription(self) -> str | None: ... if sys.version_info >= (3, 8): - def addCleanup(self, __function: Callable[_P, Any], *args: _P.args, **kwargs: _P.kwargs) -> None: ... + def addCleanup(self, __function: Callable[_P, object], *args: _P.args, **kwargs: _P.kwargs) -> None: ... else: - def addCleanup(self, function: Callable[_P, Any], *args: _P.args, **kwargs: _P.kwargs) -> None: ... + def addCleanup(self, function: Callable[_P, object], *args: _P.args, **kwargs: _P.kwargs) -> None: ... def doCleanups(self) -> None: ... if sys.version_info >= (3, 8): @classmethod - def addClassCleanup(cls, __function: Callable[_P, Any], *args: _P.args, **kwargs: _P.kwargs) -> None: ... + def addClassCleanup(cls, __function: Callable[_P, object], *args: _P.args, **kwargs: _P.kwargs) -> None: ... @classmethod def doClassCleanups(cls) -> None: ... def _formatMessage(self, msg: str | None, standardMsg: str) -> str: ... # undocumented @@ -236,9 +236,9 @@ class TestCase: def failUnlessRaises( # type: ignore[misc] self, exception: type[BaseException] | tuple[type[BaseException], ...], - callable: Callable[..., Any] = ..., - *args: Any, - **kwargs: Any, + callable: Callable[_P, object] = ..., + *args: _P.args, + **kwargs: _P.kwargs, ) -> None: ... @overload def failUnlessRaises(self, exception: type[_E] | tuple[type[_E], ...], msg: Any = ...) -> _AssertRaisesContext[_E]: ... @@ -257,9 +257,9 @@ class TestCase: self, exception: type[BaseException] | tuple[type[BaseException], ...], expected_regex: str | bytes | Pattern[str] | Pattern[bytes], - callable: Callable[..., Any], - *args: Any, - **kwargs: Any, + callable: Callable[_P, object], + *args: _P.args, + **kwargs: _P.kwargs, ) -> None: ... @overload def assertRaisesRegexp( From 2900fa3b59fcfed75a45f1a3a181507962f25a64 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Mon, 24 Jan 2022 01:09:46 +0300 Subject: [PATCH 4/6] Update case.pyi --- stdlib/unittest/case.pyi | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/stdlib/unittest/case.pyi b/stdlib/unittest/case.pyi index 0925bc2dd450..3bad16be4619 100644 --- a/stdlib/unittest/case.pyi +++ b/stdlib/unittest/case.pyi @@ -108,13 +108,16 @@ class TestCase: def assertGreaterEqual(self, a: Any, b: Any, msg: Any = ...) -> None: ... def assertLess(self, a: Any, b: Any, msg: Any = ...) -> None: ... def assertLessEqual(self, a: Any, b: Any, msg: Any = ...) -> None: ... + # `assertRaises`, `assertRaisesRegex`, and `assertRaisesRegexp` + # are not using `ParamSpec` intentionally, + # because they might be used with explicitly wrong arg types to raise some error in tests. @overload def assertRaises( # type: ignore[misc] self, expected_exception: type[BaseException] | tuple[type[BaseException], ...], - callable: Callable[_P, object], - *args: _P.args, - **kwargs: _P.kwargs, + callable: Callable[..., object], + *args: Any, + **kwargs: Any, ) -> None: ... @overload def assertRaises(self, expected_exception: type[_E] | tuple[type[_E], ...], msg: Any = ...) -> _AssertRaisesContext[_E]: ... @@ -123,9 +126,9 @@ class TestCase: self, expected_exception: type[BaseException] | tuple[type[BaseException], ...], expected_regex: str | bytes | Pattern[str] | Pattern[bytes], - callable: Callable[_P, object], - *args: _P.args, - **kwargs: _P.kwargs, + callable: Callable[..., object], + *args: Any, + **kwargs: Any, ) -> None: ... @overload def assertRaisesRegex( @@ -138,9 +141,9 @@ class TestCase: def assertWarns( # type: ignore[misc] self, expected_warning: type[Warning] | tuple[type[Warning], ...], - callable: Callable[_P, object], - *args: _P.args, - **kwargs: _P.kwargs, + callable: Callable[..., object], + *args: Any, + **kwargs: Any, ) -> None: ... @overload def assertWarns(self, expected_warning: type[Warning] | tuple[type[Warning], ...], msg: Any = ...) -> _AssertWarnsContext: ... @@ -257,9 +260,9 @@ class TestCase: self, exception: type[BaseException] | tuple[type[BaseException], ...], expected_regex: str | bytes | Pattern[str] | Pattern[bytes], - callable: Callable[_P, object], - *args: _P.args, - **kwargs: _P.kwargs, + callable: Callable[..., object], + *args: Any, + **kwargs: Any, ) -> None: ... @overload def assertRaisesRegexp( From 568cd484ac785e842faa5a4f3384be6c6921ab1f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 23 Jan 2022 22:10:57 +0000 Subject: [PATCH 5/6] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/unittest/case.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/unittest/case.pyi b/stdlib/unittest/case.pyi index 3bad16be4619..010e1ede2669 100644 --- a/stdlib/unittest/case.pyi +++ b/stdlib/unittest/case.pyi @@ -108,7 +108,7 @@ class TestCase: def assertGreaterEqual(self, a: Any, b: Any, msg: Any = ...) -> None: ... def assertLess(self, a: Any, b: Any, msg: Any = ...) -> None: ... def assertLessEqual(self, a: Any, b: Any, msg: Any = ...) -> None: ... - # `assertRaises`, `assertRaisesRegex`, and `assertRaisesRegexp` + # `assertRaises`, `assertRaisesRegex`, and `assertRaisesRegexp` # are not using `ParamSpec` intentionally, # because they might be used with explicitly wrong arg types to raise some error in tests. @overload From 3667f1ad740c29099998ef341e5dd05bf1b18761 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Mon, 24 Jan 2022 01:11:48 +0300 Subject: [PATCH 6/6] Update case.pyi --- stdlib/unittest/case.pyi | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stdlib/unittest/case.pyi b/stdlib/unittest/case.pyi index 010e1ede2669..dabe63039bbc 100644 --- a/stdlib/unittest/case.pyi +++ b/stdlib/unittest/case.pyi @@ -141,9 +141,9 @@ class TestCase: def assertWarns( # type: ignore[misc] self, expected_warning: type[Warning] | tuple[type[Warning], ...], - callable: Callable[..., object], - *args: Any, - **kwargs: Any, + callable: Callable[_P, object], + *args: _P.args, + **kwargs: _P.kwargs, ) -> None: ... @overload def assertWarns(self, expected_warning: type[Warning] | tuple[type[Warning], ...], msg: Any = ...) -> _AssertWarnsContext: ...