Skip to content

Commit 9bf9d15

Browse files
authored
Fix unittest for 3.13 (#12307)
1 parent f698bb6 commit 9bf9d15

File tree

6 files changed

+89
-59
lines changed

6 files changed

+89
-59
lines changed

stdlib/@tests/stubtest_allowlists/py313.txt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -137,20 +137,6 @@ tkinter.Text.count
137137
tkinter.Wm.wm_attributes
138138
trace.CoverageResults.write_results
139139
types.MappingProxyType.get
140-
unittest.IsolatedAsyncioTestCase.loop_factory
141-
unittest.TestProgram.usageExit
142-
unittest.__all__
143-
unittest.async_case.IsolatedAsyncioTestCase.loop_factory
144-
unittest.findTestCases
145-
unittest.getTestCaseNames
146-
unittest.loader.findTestCases
147-
unittest.loader.getTestCaseNames
148-
unittest.loader.makeSuite
149-
unittest.main.TestProgram.usageExit
150-
unittest.makeSuite
151-
unittest.mock.NonCallableMock._calls_repr
152-
unittest.mock.ThreadingMock
153-
unittest.mock.__all__
154140
zipfile.CompleteDirs.inject
155141
zipfile.ZipInfo.compress_level
156142
zipfile._path.CompleteDirs.inject

stdlib/unittest/__init__.pyi

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,7 @@ from .case import (
1111
skipIf as skipIf,
1212
skipUnless as skipUnless,
1313
)
14-
from .loader import (
15-
TestLoader as TestLoader,
16-
defaultTestLoader as defaultTestLoader,
17-
findTestCases as findTestCases,
18-
getTestCaseNames as getTestCaseNames,
19-
makeSuite as makeSuite,
20-
)
14+
from .loader import TestLoader as TestLoader, defaultTestLoader as defaultTestLoader
2115
from .main import TestProgram as TestProgram, main as main
2216
from .result import TestResult as TestResult
2317
from .runner import TextTestResult as TextTestResult, TextTestRunner as TextTestRunner
@@ -52,12 +46,14 @@ __all__ = [
5246
"registerResult",
5347
"removeResult",
5448
"removeHandler",
55-
"getTestCaseNames",
56-
"makeSuite",
57-
"findTestCases",
5849
"addModuleCleanup",
5950
]
6051

52+
if sys.version_info < (3, 13):
53+
from .loader import findTestCases as findTestCases, getTestCaseNames as getTestCaseNames, makeSuite as makeSuite
54+
55+
__all__ += ["getTestCaseNames", "makeSuite", "findTestCases"]
56+
6157
if sys.version_info >= (3, 11):
6258
__all__ += ["enterModuleContext", "doModuleCleanups"]
6359

stdlib/unittest/async_case.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
from asyncio.events import AbstractEventLoop
23
from collections.abc import Awaitable, Callable
34
from typing import TypeVar
45
from typing_extensions import ParamSpec
@@ -12,6 +13,9 @@ _T = TypeVar("_T")
1213
_P = ParamSpec("_P")
1314

1415
class IsolatedAsyncioTestCase(TestCase):
16+
if sys.version_info >= (3, 13):
17+
loop_factory: Callable[[], AbstractEventLoop] | None = None
18+
1519
async def asyncSetUp(self) -> None: ...
1620
async def asyncTearDown(self) -> None: ...
1721
def addAsyncCleanup(self, func: Callable[_P, Awaitable[object]], /, *args: _P.args, **kwargs: _P.kwargs) -> None: ...

stdlib/unittest/loader.pyi

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ from collections.abc import Callable, Sequence
55
from re import Pattern
66
from types import ModuleType
77
from typing import Any
8-
from typing_extensions import TypeAlias
8+
from typing_extensions import TypeAlias, deprecated
99

1010
_SortComparisonMethod: TypeAlias = Callable[[str, str], int]
1111
_SuiteClass: TypeAlias = Callable[[list[unittest.case.TestCase]], unittest.suite.TestSuite]
@@ -34,18 +34,22 @@ class TestLoader:
3434

3535
defaultTestLoader: TestLoader
3636

37-
def getTestCaseNames(
38-
testCaseClass: type[unittest.case.TestCase],
39-
prefix: str,
40-
sortUsing: _SortComparisonMethod = ...,
41-
testNamePatterns: list[str] | None = None,
42-
) -> Sequence[str]: ...
43-
def makeSuite(
44-
testCaseClass: type[unittest.case.TestCase],
45-
prefix: str = "test",
46-
sortUsing: _SortComparisonMethod = ...,
47-
suiteClass: _SuiteClass = ...,
48-
) -> unittest.suite.TestSuite: ...
49-
def findTestCases(
50-
module: ModuleType, prefix: str = "test", sortUsing: _SortComparisonMethod = ..., suiteClass: _SuiteClass = ...
51-
) -> unittest.suite.TestSuite: ...
37+
if sys.version_info < (3, 13):
38+
@deprecated("Deprecated in Python 3.11; removal scheduled for Python 3.13")
39+
def getTestCaseNames(
40+
testCaseClass: type[unittest.case.TestCase],
41+
prefix: str,
42+
sortUsing: _SortComparisonMethod = ...,
43+
testNamePatterns: list[str] | None = None,
44+
) -> Sequence[str]: ...
45+
@deprecated("Deprecated in Python 3.11; removal scheduled for Python 3.13")
46+
def makeSuite(
47+
testCaseClass: type[unittest.case.TestCase],
48+
prefix: str = "test",
49+
sortUsing: _SortComparisonMethod = ...,
50+
suiteClass: _SuiteClass = ...,
51+
) -> unittest.suite.TestSuite: ...
52+
@deprecated("Deprecated in Python 3.11; removal scheduled for Python 3.13")
53+
def findTestCases(
54+
module: ModuleType, prefix: str = "test", sortUsing: _SortComparisonMethod = ..., suiteClass: _SuiteClass = ...
55+
) -> unittest.suite.TestSuite: ...

stdlib/unittest/main.pyi

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import unittest.suite
66
from collections.abc import Iterable
77
from types import ModuleType
88
from typing import Any, Protocol
9+
from typing_extensions import deprecated
910

1011
MAIN_EXAMPLES: str
1112
MODULE_EXAMPLES: str
@@ -61,7 +62,10 @@ class TestProgram:
6162
tb_locals: bool = False,
6263
) -> None: ...
6364

64-
def usageExit(self, msg: Any = None) -> None: ...
65+
if sys.version_info < (3, 13):
66+
@deprecated("Deprecated in Python 3.11; removal scheduled for Python 3.13")
67+
def usageExit(self, msg: Any = None) -> None: ...
68+
6569
def parseArgs(self, argv: list[str]) -> None: ...
6670
def createTests(self, from_discovery: bool = False, Loader: unittest.loader.TestLoader | None = None) -> None: ...
6771
def runTests(self) -> None: ... # undocumented

stdlib/unittest/mock.pyi

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,44 @@ _F = TypeVar("_F", bound=Callable[..., Any])
1212
_AF = TypeVar("_AF", bound=Callable[..., Coroutine[Any, Any, Any]])
1313
_P = ParamSpec("_P")
1414

15-
__all__ = (
16-
"Mock",
17-
"MagicMock",
18-
"patch",
19-
"sentinel",
20-
"DEFAULT",
21-
"ANY",
22-
"call",
23-
"create_autospec",
24-
"AsyncMock",
25-
"FILTER_DIR",
26-
"NonCallableMock",
27-
"NonCallableMagicMock",
28-
"mock_open",
29-
"PropertyMock",
30-
"seal",
31-
)
15+
if sys.version_info >= (3, 13):
16+
# ThreadingMock added in 3.13
17+
__all__ = (
18+
"Mock",
19+
"MagicMock",
20+
"patch",
21+
"sentinel",
22+
"DEFAULT",
23+
"ANY",
24+
"call",
25+
"create_autospec",
26+
"ThreadingMock",
27+
"AsyncMock",
28+
"FILTER_DIR",
29+
"NonCallableMock",
30+
"NonCallableMagicMock",
31+
"mock_open",
32+
"PropertyMock",
33+
"seal",
34+
)
35+
else:
36+
__all__ = (
37+
"Mock",
38+
"MagicMock",
39+
"patch",
40+
"sentinel",
41+
"DEFAULT",
42+
"ANY",
43+
"call",
44+
"create_autospec",
45+
"AsyncMock",
46+
"FILTER_DIR",
47+
"NonCallableMock",
48+
"NonCallableMagicMock",
49+
"mock_open",
50+
"PropertyMock",
51+
"seal",
52+
)
3253

3354
if sys.version_info < (3, 9):
3455
__version__: Final[str]
@@ -124,7 +145,6 @@ class NonCallableMock(Base, Any):
124145
def __delattr__(self, name: str) -> None: ...
125146
def __setattr__(self, name: str, value: Any) -> None: ...
126147
def __dir__(self) -> list[str]: ...
127-
def _calls_repr(self, prefix: str = "Calls") -> str: ...
128148
def assert_called_with(self, *args: Any, **kwargs: Any) -> None: ...
129149
def assert_not_called(self) -> None: ...
130150
def assert_called_once_with(self, *args: Any, **kwargs: Any) -> None: ...
@@ -150,6 +170,10 @@ class NonCallableMock(Base, Any):
150170
def _format_mock_call_signature(self, args: Any, kwargs: Any) -> str: ...
151171
def _call_matcher(self, _call: tuple[_Call, ...]) -> _Call: ...
152172
def _get_child_mock(self, **kw: Any) -> NonCallableMock: ...
173+
if sys.version_info >= (3, 13):
174+
def _calls_repr(self) -> str: ...
175+
else:
176+
def _calls_repr(self, prefix: str = "Calls") -> str: ...
153177

154178
class CallableMixin(Base):
155179
side_effect: Any
@@ -427,4 +451,16 @@ class PropertyMock(Mock):
427451
def __get__(self, obj: _T, obj_type: type[_T] | None = None) -> Self: ...
428452
def __set__(self, obj: Any, val: Any) -> None: ...
429453

454+
if sys.version_info >= (3, 13):
455+
class ThreadingMixin(Base):
456+
DEFAULT_TIMEOUT: Final[float | None] = None
457+
458+
def __init__(self, /, *args: Any, timeout: float | None | _SentinelObject = ..., **kwargs: Any) -> None: ...
459+
# Same as `NonCallableMock.reset_mock.`
460+
def reset_mock(self, visited: Any = None, *, return_value: bool = False, side_effect: bool = False) -> None: ...
461+
def wait_until_called(self, *, timeout: float | None | _SentinelObject = ...) -> None: ...
462+
def wait_until_any_call_with(self, *args: Any, **kwargs: Any) -> None: ...
463+
464+
class ThreadingMock(ThreadingMixin, MagicMixin, Mock): ...
465+
430466
def seal(mock: Any) -> None: ...

0 commit comments

Comments
 (0)