Skip to content

Commit b650d96

Browse files
authored
Revert use of recursive alias in typeshed (#14130)
Easy crash to repro: `mypy --any-exprs-report=out -c 'pass' --show-traceback`
1 parent 0062994 commit b650d96

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

mypy/typeshed/stdlib/_typeshed/__init__.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,5 @@ StrOrLiteralStr = TypeVar("StrOrLiteralStr", LiteralString, str) # noqa: Y001
276276
ProfileFunction: TypeAlias = Callable[[FrameType, str, Any], object]
277277

278278
# Objects suitable to be passed to sys.settrace, threading.settrace, and similar
279-
TraceFunction: TypeAlias = Callable[[FrameType, str, Any], TraceFunction | None]
279+
# TODO: Ideally this would be a recursive type alias
280+
TraceFunction: TypeAlias = Callable[[FrameType, str, Any], Callable[[FrameType, str, Any], Any] | None]

mypy/typeshed/stdlib/builtins.pyi

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,13 +1232,19 @@ def iter(__function: Callable[[], _T | None], __sentinel: None) -> Iterator[_T]:
12321232
@overload
12331233
def iter(__function: Callable[[], _T], __sentinel: object) -> Iterator[_T]: ...
12341234

1235+
# We need recursive types to express the type of the second argument to `isinstance` properly, hence the use of `Any`
12351236
if sys.version_info >= (3, 10):
1236-
_ClassInfo: TypeAlias = type | types.UnionType | tuple[_ClassInfo, ...]
1237+
def isinstance(
1238+
__obj: object, __class_or_tuple: type | types.UnionType | tuple[type | types.UnionType | tuple[Any, ...], ...]
1239+
) -> bool: ...
1240+
def issubclass(
1241+
__cls: type, __class_or_tuple: type | types.UnionType | tuple[type | types.UnionType | tuple[Any, ...], ...]
1242+
) -> bool: ...
1243+
12371244
else:
1238-
_ClassInfo: TypeAlias = type | tuple[_ClassInfo, ...]
1245+
def isinstance(__obj: object, __class_or_tuple: type | tuple[type | tuple[Any, ...], ...]) -> bool: ...
1246+
def issubclass(__cls: type, __class_or_tuple: type | tuple[type | tuple[Any, ...], ...]) -> bool: ...
12391247

1240-
def isinstance(__obj: object, __class_or_tuple: _ClassInfo) -> bool: ...
1241-
def issubclass(__cls: type, __class_or_tuple: _ClassInfo) -> bool: ...
12421248
def len(__obj: Sized) -> int: ...
12431249
def license() -> None: ...
12441250
def locals() -> dict[str, Any]: ...

0 commit comments

Comments
 (0)