Use TypeAlias where possible for type aliases (#7630)

This commit is contained in:
Alex Waygood
2022-04-16 02:01:00 +01:00
committed by GitHub
parent c0e6dd3f3f
commit 740193a8fc
218 changed files with 760 additions and 625 deletions

View File

@@ -6,13 +6,13 @@ from importlib.machinery import ModuleSpec
from io import TextIOWrapper
from types import FrameType, ModuleType, TracebackType
from typing import Any, AsyncGenerator, Callable, Coroutine, NoReturn, Protocol, Sequence, TextIO, TypeVar, Union, overload
from typing_extensions import Literal, final
from typing_extensions import Literal, TypeAlias, final
_T = TypeVar("_T")
# The following type alias are stub-only and do not exist during runtime
_ExcInfo = tuple[type[BaseException], BaseException, TracebackType]
_OptExcInfo = Union[_ExcInfo, tuple[None, None, None]]
_ExcInfo: TypeAlias = tuple[type[BaseException], BaseException, TracebackType]
_OptExcInfo: TypeAlias = Union[_ExcInfo, tuple[None, None, None]]
# Intentionally omits one deprecated and one optional method of `importlib.abc.MetaPathFinder`
class _MetaPathFinder(Protocol):
@@ -76,16 +76,16 @@ _xoptions: dict[Any, Any]
# Type alias used as a mixin for structseq classes that cannot be instantiated at runtime
# This can't be represented in the type system, so we just use `structseq[Any]`
_uninstantiable_structseq = structseq[Any]
_uninstantiable_structseq: TypeAlias = structseq[Any]
flags: _flags
if sys.version_info >= (3, 10):
_FlagTuple = tuple[int, int, int, int, int, int, int, int, int, int, int, int, int, bool, int, int]
_FlagTuple: TypeAlias = tuple[int, int, int, int, int, int, int, int, int, int, int, int, int, bool, int, int]
elif sys.version_info >= (3, 7):
_FlagTuple = tuple[int, int, int, int, int, int, int, int, int, int, int, int, int, bool, int]
_FlagTuple: TypeAlias = tuple[int, int, int, int, int, int, int, int, int, int, int, int, int, bool, int]
else:
_FlagTuple = tuple[int, int, int, int, int, int, int, int, int, int, int, int, int]
_FlagTuple: TypeAlias = tuple[int, int, int, int, int, int, int, int, int, int, int, int, int]
@final
class _flags(_uninstantiable_structseq, _FlagTuple):
@@ -237,12 +237,12 @@ def getsizeof(obj: object) -> int: ...
def getsizeof(obj: object, default: int) -> int: ...
def getswitchinterval() -> float: ...
_ProfileFunc = Callable[[FrameType, str, Any], Any]
_ProfileFunc: TypeAlias = Callable[[FrameType, str, Any], Any]
def getprofile() -> _ProfileFunc | None: ...
def setprofile(profilefunc: _ProfileFunc | None) -> None: ...
_TraceFunc = Callable[[FrameType, str, Any], Callable[[FrameType, str, Any], Any] | None]
_TraceFunc: TypeAlias = Callable[[FrameType, str, Any], Callable[[FrameType, str, Any], Any] | None]
def gettrace() -> _TraceFunc | None: ...
def settrace(tracefunc: _TraceFunc | None) -> None: ...
@@ -309,7 +309,7 @@ if sys.version_info >= (3, 8):
def addaudithook(hook: Callable[[str, tuple[Any, ...]], Any]) -> None: ...
def audit(__event: str, *args: Any) -> None: ...
_AsyncgenHook = Callable[[AsyncGenerator[Any, Any]], None] | None
_AsyncgenHook: TypeAlias = Callable[[AsyncGenerator[Any, Any]], None] | None
@final
class _asyncgen_hooks(structseq[_AsyncgenHook], tuple[_AsyncgenHook, _AsyncgenHook]):
@@ -330,6 +330,6 @@ if sys.version_info >= (3, 7):
def set_coroutine_origin_tracking_depth(depth: int) -> None: ...
if sys.version_info < (3, 8):
_CoroWrapper = Callable[[Coroutine[Any, Any, Any]], Any]
_CoroWrapper: TypeAlias = Callable[[Coroutine[Any, Any, Any]], Any]
def set_coroutine_wrapper(__wrapper: _CoroWrapper) -> None: ...
def get_coroutine_wrapper() -> _CoroWrapper: ...