From 4273a83bb7931385cc91694b9fff028ff2b01b56 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Mon, 20 Feb 2023 15:35:50 +0000 Subject: [PATCH] `unittest`: Use a recursive type alias for `assertIsInstance` (#9770) --- stdlib/builtins.pyi | 1 + stdlib/unittest/case.pyi | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 63cf288e2..6f26df3b2 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -1394,6 +1394,7 @@ def iter(__function: Callable[[], _T | None], __sentinel: None) -> Iterator[_T]: @overload def iter(__function: Callable[[], _T], __sentinel: object) -> Iterator[_T]: ... +# Keep this alias in sync with unittest.case._ClassInfo if sys.version_info >= (3, 10): _ClassInfo: TypeAlias = type | types.UnionType | tuple[_ClassInfo, ...] else: diff --git a/stdlib/unittest/case.pyi b/stdlib/unittest/case.pyi index 8f8cf4338..b118ecfca 100644 --- a/stdlib/unittest/case.pyi +++ b/stdlib/unittest/case.pyi @@ -68,10 +68,13 @@ class SkipTest(Exception): class _SupportsAbsAndDunderGE(SupportsDunderGE[Any], SupportsAbs[Any], Protocol): ... +# Keep this alias in sync with builtins._ClassInfo +# We can't import it from builtins or pytype crashes, +# due to the fact that pytype uses a custom builtins stub rather than typeshed's builtins stub if sys.version_info >= (3, 10): - _IsInstanceClassInfo: TypeAlias = type | UnionType | tuple[type | UnionType | tuple[Any, ...], ...] + _ClassInfo: TypeAlias = type | UnionType | tuple[_ClassInfo, ...] else: - _IsInstanceClassInfo: TypeAlias = type | tuple[type | tuple[Any, ...], ...] + _ClassInfo: TypeAlias = type | tuple[_ClassInfo, ...] class TestCase: failureException: type[BaseException] @@ -107,8 +110,8 @@ class TestCase: def assertIsNotNone(self, obj: object, msg: Any = None) -> None: ... def assertIn(self, member: Any, container: Iterable[Any] | Container[Any], msg: Any = None) -> None: ... def assertNotIn(self, member: Any, container: Iterable[Any] | Container[Any], msg: Any = None) -> None: ... - def assertIsInstance(self, obj: object, cls: _IsInstanceClassInfo, msg: Any = None) -> None: ... - def assertNotIsInstance(self, obj: object, cls: _IsInstanceClassInfo, msg: Any = None) -> None: ... + def assertIsInstance(self, obj: object, cls: _ClassInfo, msg: Any = None) -> None: ... + def assertNotIsInstance(self, obj: object, cls: _ClassInfo, msg: Any = None) -> None: ... @overload def assertGreater(self, a: SupportsDunderGT[_T], b: _T, msg: Any = None) -> None: ... @overload