Skip to content

Commit 4273a83

Browse files
authored
unittest: Use a recursive type alias for assertIsInstance (#9770)
1 parent 5e6b172 commit 4273a83

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

stdlib/builtins.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,7 @@ def iter(__function: Callable[[], _T | None], __sentinel: None) -> Iterator[_T]:
13941394
@overload
13951395
def iter(__function: Callable[[], _T], __sentinel: object) -> Iterator[_T]: ...
13961396

1397+
# Keep this alias in sync with unittest.case._ClassInfo
13971398
if sys.version_info >= (3, 10):
13981399
_ClassInfo: TypeAlias = type | types.UnionType | tuple[_ClassInfo, ...]
13991400
else:

stdlib/unittest/case.pyi

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,13 @@ class SkipTest(Exception):
6868

6969
class _SupportsAbsAndDunderGE(SupportsDunderGE[Any], SupportsAbs[Any], Protocol): ...
7070

71+
# Keep this alias in sync with builtins._ClassInfo
72+
# We can't import it from builtins or pytype crashes,
73+
# due to the fact that pytype uses a custom builtins stub rather than typeshed's builtins stub
7174
if sys.version_info >= (3, 10):
72-
_IsInstanceClassInfo: TypeAlias = type | UnionType | tuple[type | UnionType | tuple[Any, ...], ...]
75+
_ClassInfo: TypeAlias = type | UnionType | tuple[_ClassInfo, ...]
7376
else:
74-
_IsInstanceClassInfo: TypeAlias = type | tuple[type | tuple[Any, ...], ...]
77+
_ClassInfo: TypeAlias = type | tuple[_ClassInfo, ...]
7578

7679
class TestCase:
7780
failureException: type[BaseException]
@@ -107,8 +110,8 @@ class TestCase:
107110
def assertIsNotNone(self, obj: object, msg: Any = None) -> None: ...
108111
def assertIn(self, member: Any, container: Iterable[Any] | Container[Any], msg: Any = None) -> None: ...
109112
def assertNotIn(self, member: Any, container: Iterable[Any] | Container[Any], msg: Any = None) -> None: ...
110-
def assertIsInstance(self, obj: object, cls: _IsInstanceClassInfo, msg: Any = None) -> None: ...
111-
def assertNotIsInstance(self, obj: object, cls: _IsInstanceClassInfo, msg: Any = None) -> None: ...
113+
def assertIsInstance(self, obj: object, cls: _ClassInfo, msg: Any = None) -> None: ...
114+
def assertNotIsInstance(self, obj: object, cls: _ClassInfo, msg: Any = None) -> None: ...
112115
@overload
113116
def assertGreater(self, a: SupportsDunderGT[_T], b: _T, msg: Any = None) -> None: ...
114117
@overload

0 commit comments

Comments
 (0)