Skip to content

Commit 20508d0

Browse files
authored
Use ParamSpec in unittest.case (#7012)
1 parent 03a7ac5 commit 20508d0

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

stdlib/unittest/case.pyi

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ from typing import (
2222
TypeVar,
2323
overload,
2424
)
25+
from typing_extensions import ParamSpec
2526
from warnings import WarningMessage
2627

2728
if sys.version_info >= (3, 9):
2829
from types import GenericAlias
2930

3031
_E = TypeVar("_E", bound=BaseException)
3132
_FT = TypeVar("_FT", bound=Callable[..., Any])
33+
_P = ParamSpec("_P")
3234

3335
DIFF_OMITTED: str
3436

@@ -58,7 +60,7 @@ else:
5860
) -> bool | None: ...
5961

6062
if sys.version_info >= (3, 8):
61-
def addModuleCleanup(__function: Callable[..., Any], *args: Any, **kwargs: Any) -> None: ...
63+
def addModuleCleanup(__function: Callable[_P, object], *args: _P.args, **kwargs: _P.kwargs) -> None: ...
6264
def doModuleCleanups() -> None: ...
6365

6466
def expectedFailure(test_item: _FT) -> _FT: ...
@@ -106,11 +108,14 @@ class TestCase:
106108
def assertGreaterEqual(self, a: Any, b: Any, msg: Any = ...) -> None: ...
107109
def assertLess(self, a: Any, b: Any, msg: Any = ...) -> None: ...
108110
def assertLessEqual(self, a: Any, b: Any, msg: Any = ...) -> None: ...
111+
# `assertRaises`, `assertRaisesRegex`, and `assertRaisesRegexp`
112+
# are not using `ParamSpec` intentionally,
113+
# because they might be used with explicitly wrong arg types to raise some error in tests.
109114
@overload
110115
def assertRaises( # type: ignore[misc]
111116
self,
112117
expected_exception: type[BaseException] | tuple[type[BaseException], ...],
113-
callable: Callable[..., Any],
118+
callable: Callable[..., object],
114119
*args: Any,
115120
**kwargs: Any,
116121
) -> None: ...
@@ -121,7 +126,7 @@ class TestCase:
121126
self,
122127
expected_exception: type[BaseException] | tuple[type[BaseException], ...],
123128
expected_regex: str | bytes | Pattern[str] | Pattern[bytes],
124-
callable: Callable[..., Any],
129+
callable: Callable[..., object],
125130
*args: Any,
126131
**kwargs: Any,
127132
) -> None: ...
@@ -134,7 +139,11 @@ class TestCase:
134139
) -> _AssertRaisesContext[_E]: ...
135140
@overload
136141
def assertWarns( # type: ignore[misc]
137-
self, expected_warning: type[Warning] | tuple[type[Warning], ...], callable: Callable[..., Any], *args: Any, **kwargs: Any
142+
self,
143+
expected_warning: type[Warning] | tuple[type[Warning], ...],
144+
callable: Callable[_P, object],
145+
*args: _P.args,
146+
**kwargs: _P.kwargs,
138147
) -> None: ...
139148
@overload
140149
def assertWarns(self, expected_warning: type[Warning] | tuple[type[Warning], ...], msg: Any = ...) -> _AssertWarnsContext: ...
@@ -143,9 +152,9 @@ class TestCase:
143152
self,
144153
expected_warning: type[Warning] | tuple[type[Warning], ...],
145154
expected_regex: str | bytes | Pattern[str] | Pattern[bytes],
146-
callable: Callable[..., Any],
147-
*args: Any,
148-
**kwargs: Any,
155+
callable: Callable[_P, object],
156+
*args: _P.args,
157+
**kwargs: _P.kwargs,
149158
) -> None: ...
150159
@overload
151160
def assertWarnsRegex(
@@ -207,13 +216,13 @@ class TestCase:
207216
def id(self) -> str: ...
208217
def shortDescription(self) -> str | None: ...
209218
if sys.version_info >= (3, 8):
210-
def addCleanup(self, __function: Callable[..., Any], *args: Any, **kwargs: Any) -> None: ...
219+
def addCleanup(self, __function: Callable[_P, object], *args: _P.args, **kwargs: _P.kwargs) -> None: ...
211220
else:
212-
def addCleanup(self, function: Callable[..., Any], *args: Any, **kwargs: Any) -> None: ...
221+
def addCleanup(self, function: Callable[_P, object], *args: _P.args, **kwargs: _P.kwargs) -> None: ...
213222
def doCleanups(self) -> None: ...
214223
if sys.version_info >= (3, 8):
215224
@classmethod
216-
def addClassCleanup(cls, __function: Callable[..., Any], *args: Any, **kwargs: Any) -> None: ...
225+
def addClassCleanup(cls, __function: Callable[_P, object], *args: _P.args, **kwargs: _P.kwargs) -> None: ...
217226
@classmethod
218227
def doClassCleanups(cls) -> None: ...
219228
def _formatMessage(self, msg: str | None, standardMsg: str) -> str: ... # undocumented
@@ -230,9 +239,9 @@ class TestCase:
230239
def failUnlessRaises( # type: ignore[misc]
231240
self,
232241
exception: type[BaseException] | tuple[type[BaseException], ...],
233-
callable: Callable[..., Any] = ...,
234-
*args: Any,
235-
**kwargs: Any,
242+
callable: Callable[_P, object] = ...,
243+
*args: _P.args,
244+
**kwargs: _P.kwargs,
236245
) -> None: ...
237246
@overload
238247
def failUnlessRaises(self, exception: type[_E] | tuple[type[_E], ...], msg: Any = ...) -> _AssertRaisesContext[_E]: ...
@@ -251,7 +260,7 @@ class TestCase:
251260
self,
252261
exception: type[BaseException] | tuple[type[BaseException], ...],
253262
expected_regex: str | bytes | Pattern[str] | Pattern[bytes],
254-
callable: Callable[..., Any],
263+
callable: Callable[..., object],
255264
*args: Any,
256265
**kwargs: Any,
257266
) -> None: ...

0 commit comments

Comments
 (0)