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

@@ -9,7 +9,7 @@ from asyncio.transports import BaseTransport, ReadTransport, SubprocessTransport
from collections.abc import Iterable
from socket import AddressFamily, SocketKind, _Address, _RetAddress, socket
from typing import IO, Any, Awaitable, Callable, Coroutine, Generator, Sequence, TypeVar, overload
from typing_extensions import Literal
from typing_extensions import Literal, TypeAlias
if sys.version_info >= (3, 7):
from contextvars import Context
@@ -23,10 +23,10 @@ else:
_T = TypeVar("_T")
_ProtocolT = TypeVar("_ProtocolT", bound=BaseProtocol)
_Context = dict[str, Any]
_ExceptionHandler = Callable[[AbstractEventLoop, _Context], Any]
_ProtocolFactory = Callable[[], BaseProtocol]
_SSLContext = bool | None | ssl.SSLContext
_Context: TypeAlias = dict[str, Any]
_ExceptionHandler: TypeAlias = Callable[[AbstractEventLoop, _Context], Any]
_ProtocolFactory: TypeAlias = Callable[[], BaseProtocol]
_SSLContext: TypeAlias = bool | None | ssl.SSLContext
class Server(AbstractServer):
if sys.version_info >= (3, 7):

View File

@@ -1,10 +1,11 @@
import subprocess
from collections import deque
from typing import IO, Any, Callable, Sequence
from typing_extensions import TypeAlias
from . import events, futures, protocols, transports
_File = int | IO[Any] | None
_File: TypeAlias = int | IO[Any] | None
class BaseSubprocessTransport(transports.SubprocessTransport):

View File

@@ -4,7 +4,7 @@ from _typeshed import FileDescriptorLike, Self
from abc import ABCMeta, abstractmethod
from socket import AddressFamily, SocketKind, _Address, _RetAddress, socket
from typing import IO, Any, Awaitable, Callable, Coroutine, Generator, Sequence, TypeVar, overload
from typing_extensions import Literal
from typing_extensions import Literal, TypeAlias
from .base_events import Server
from .futures import Future
@@ -75,10 +75,10 @@ else:
_T = TypeVar("_T")
_ProtocolT = TypeVar("_ProtocolT", bound=BaseProtocol)
_Context = dict[str, Any]
_ExceptionHandler = Callable[[AbstractEventLoop, _Context], Any]
_ProtocolFactory = Callable[[], BaseProtocol]
_SSLContext = bool | None | ssl.SSLContext
_Context: TypeAlias = dict[str, Any]
_ExceptionHandler: TypeAlias = Callable[[AbstractEventLoop, _Context], Any]
_ProtocolFactory: TypeAlias = Callable[[], BaseProtocol]
_SSLContext: TypeAlias = bool | None | ssl.SSLContext
class Handle:
_cancelled: bool

View File

@@ -2,11 +2,12 @@ import functools
import traceback
from types import FrameType, FunctionType
from typing import Any, Iterable, overload
from typing_extensions import TypeAlias
class _HasWrapper:
__wrapper__: _HasWrapper | FunctionType
_FuncType = FunctionType | _HasWrapper | functools.partial[Any] | functools.partialmethod[Any]
_FuncType: TypeAlias = FunctionType | _HasWrapper | functools.partial[Any] | functools.partialmethod[Any]
@overload
def _get_function_source(func: _FuncType) -> tuple[str, int]: ...

View File

@@ -1,6 +1,7 @@
import sys
from _typeshed import Self, StrPath
from typing import Any, AsyncIterator, Awaitable, Callable, Iterable, Sequence
from typing_extensions import TypeAlias
from . import events, protocols, transports
from .base_events import Server
@@ -64,7 +65,7 @@ else:
"start_unix_server",
]
_ClientConnectedCallback = Callable[[StreamReader, StreamWriter], Awaitable[None] | None]
_ClientConnectedCallback: TypeAlias = Callable[[StreamReader, StreamWriter], Awaitable[None] | None]
if sys.version_info < (3, 8):
class IncompleteReadError(EOFError):

View File

@@ -3,7 +3,7 @@ import sys
from _typeshed import StrOrBytesPath
from asyncio import events, protocols, streams, transports
from typing import IO, Any, Callable
from typing_extensions import Literal
from typing_extensions import Literal, TypeAlias
if sys.version_info >= (3, 7):
__all__ = ("create_subprocess_exec", "create_subprocess_shell")
@@ -11,9 +11,9 @@ else:
__all__ = ["create_subprocess_exec", "create_subprocess_shell"]
if sys.version_info >= (3, 8):
_ExecArg = StrOrBytesPath
_ExecArg: TypeAlias = StrOrBytesPath
else:
_ExecArg = str | bytes
_ExecArg: TypeAlias = str | bytes
PIPE: int
STDOUT: int

View File

@@ -3,7 +3,7 @@ import sys
from collections.abc import Awaitable, Generator, Iterable, Iterator
from types import FrameType
from typing import Any, Coroutine, Generic, TextIO, TypeVar, overload
from typing_extensions import Literal
from typing_extensions import Literal, TypeAlias
from .events import AbstractEventLoop
from .futures import Future
@@ -56,8 +56,8 @@ _T3 = TypeVar("_T3")
_T4 = TypeVar("_T4")
_T5 = TypeVar("_T5")
_FT = TypeVar("_FT", bound=Future[Any])
_FutureT = Future[_T] | Generator[Any, None, _T] | Awaitable[_T]
_TaskYieldType = Future[object] | None
_FutureT: TypeAlias = Future[_T] | Generator[Any, None, _T] | Awaitable[_T]
_TaskYieldType: TypeAlias = Future[object] | None
FIRST_COMPLETED = concurrent.futures.FIRST_COMPLETED
FIRST_EXCEPTION = concurrent.futures.FIRST_EXCEPTION

View File

@@ -3,12 +3,13 @@ import sys
from builtins import type as Type # alias to avoid name clashes with property named "type"
from types import TracebackType
from typing import Any, BinaryIO, Iterable, NoReturn, overload
from typing_extensions import TypeAlias
# These are based in socket, maybe move them out into _typeshed.pyi or such
_Address = tuple[Any, ...] | str
_RetAddress = Any
_WriteBuffer = bytearray | memoryview
_CMSG = tuple[int, int, bytes]
_Address: TypeAlias = tuple[Any, ...] | str
_RetAddress: TypeAlias = Any
_WriteBuffer: TypeAlias = bytearray | memoryview
_CMSG: TypeAlias = tuple[int, int, bytes]
class TransportSocket:
def __init__(self, sock: socket.socket) -> None: ...