Skip to content

Commit 740193a

Browse files
authored
Use TypeAlias where possible for type aliases (#7630)
1 parent c0e6dd3 commit 740193a

File tree

218 files changed

+760
-625
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

218 files changed

+760
-625
lines changed

stdlib/_codecs.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import codecs
22
import sys
33
from typing import Any, Callable
4+
from typing_extensions import TypeAlias
45

56
# This type is not exposed; it is defined in unicodeobject.c
67
class _EncodingMap:
78
def size(self) -> int: ...
89

9-
_MapT = dict[int, int] | _EncodingMap
10-
_Handler = Callable[[Exception], tuple[str, int]]
10+
_MapT: TypeAlias = dict[int, int] | _EncodingMap
11+
_Handler: TypeAlias = Callable[[Exception], tuple[str, int]]
1112

1213
def register(__search_function: Callable[[str], Any]) -> None: ...
1314
def register_error(__errors: str, __handler: _Handler) -> None: ...

stdlib/_csv.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from typing import Any, Iterable, Iterator, Protocol, Union
2-
from typing_extensions import Literal
2+
from typing_extensions import Literal, TypeAlias
33

44
__version__: str
55

@@ -21,7 +21,7 @@ class Dialect:
2121
strict: int
2222
def __init__(self) -> None: ...
2323

24-
_DialectLike = Union[str, Dialect, type[Dialect]]
24+
_DialectLike: TypeAlias = Union[str, Dialect, type[Dialect]]
2525

2626
class _reader(Iterator[list[str]]):
2727
dialect: Dialect

stdlib/_curses.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import sys
22
from _typeshed import SupportsRead
33
from typing import IO, Any, NamedTuple, overload
4-
from typing_extensions import final
4+
from typing_extensions import TypeAlias, final
55

66
if sys.platform != "win32":
7-
_chtype = str | bytes | int
7+
_chtype: TypeAlias = str | bytes | int
88

99
# ACS codes are only initialized after initscr is called
1010
ACS_BBSS: int

stdlib/_dummy_threading.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import sys
22
from types import FrameType, TracebackType
33
from typing import Any, Callable, Iterable, Mapping, TypeVar
4+
from typing_extensions import TypeAlias
45

56
# TODO recursive type
6-
_TF = Callable[[FrameType, str, Any], Callable[..., Any] | None]
7+
_TF: TypeAlias = Callable[[FrameType, str, Any], Callable[..., Any] | None]
78

8-
_PF = Callable[[FrameType, str, Any], None]
9+
_PF: TypeAlias = Callable[[FrameType, str, Any], None]
910
_T = TypeVar("_T")
1011

1112
if sys.version_info >= (3, 8):

stdlib/_operator.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ from typing import (
1515
TypeVar,
1616
overload,
1717
)
18-
from typing_extensions import ParamSpec, SupportsIndex, final
18+
from typing_extensions import ParamSpec, SupportsIndex, TypeAlias, final
1919

2020
_R = TypeVar("_R")
2121
_T = TypeVar("_T")
@@ -40,7 +40,7 @@ class _SupportsDunderLE(Protocol):
4040
class _SupportsDunderGE(Protocol):
4141
def __ge__(self, __other: Any) -> Any: ...
4242

43-
_SupportsComparison = _SupportsDunderLE | _SupportsDunderGE | _SupportsDunderGT | _SupportsDunderLT
43+
_SupportsComparison: TypeAlias = _SupportsDunderLE | _SupportsDunderGE | _SupportsDunderGT | _SupportsDunderLT
4444

4545
class _SupportsInversion(Protocol[_T_co]):
4646
def __invert__(self) -> _T_co: ...

stdlib/_random.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from typing_extensions import TypeAlias
2+
13
# Actually Tuple[(int,) * 625]
2-
_State = tuple[int, ...]
4+
_State: TypeAlias = tuple[int, ...]
35

46
class Random:
57
def __init__(self, seed: object = ...) -> None: ...

stdlib/_socket.pyi

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@ import sys
22
from _typeshed import ReadableBuffer, WriteableBuffer
33
from collections.abc import Iterable
44
from typing import Any, SupportsInt, overload
5+
from typing_extensions import TypeAlias
56

67
if sys.version_info >= (3, 8):
78
from typing import SupportsIndex
89

9-
_FD = SupportsIndex
10+
_FD: TypeAlias = SupportsIndex
1011
else:
11-
_FD = SupportsInt
12+
_FD: TypeAlias = SupportsInt
1213

13-
_CMSG = tuple[int, int, bytes]
14-
_CMSGArg = tuple[int, int, ReadableBuffer]
14+
_CMSG: TypeAlias = tuple[int, int, bytes]
15+
_CMSGArg: TypeAlias = tuple[int, int, ReadableBuffer]
1516

1617
# Addresses can be either tuples of varying lengths (AF_INET, AF_INET6,
1718
# AF_NETLINK, AF_TIPC) or strings (AF_UNIX).
18-
_Address = tuple[Any, ...] | str
19+
_Address: TypeAlias = tuple[Any, ...] | str
1920
_RetAddress = Any
2021
# TODO Most methods allow bytes as address objects
2122

stdlib/_threading_local.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from typing import Any
2+
from typing_extensions import TypeAlias
23
from weakref import ReferenceType
34

45
__all__ = ["local"]
5-
localdict = dict[Any, Any]
6+
localdict: TypeAlias = dict[Any, Any]
67

78
class _localimpl:
89
key: str

stdlib/_typeshed/__init__.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ StrPath: TypeAlias = str | PathLike[str] # stable
9999
BytesPath: TypeAlias = bytes | PathLike[bytes] # stable
100100
StrOrBytesPath: TypeAlias = str | bytes | PathLike[str] | PathLike[bytes] # stable
101101

102-
OpenTextModeUpdating = Literal[
102+
OpenTextModeUpdating: TypeAlias = Literal[
103103
"r+",
104104
"+r",
105105
"rt+",

stdlib/aifc.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import sys
22
from _typeshed import Self
33
from types import TracebackType
44
from typing import IO, Any, NamedTuple, overload
5-
from typing_extensions import Literal
5+
from typing_extensions import Literal, TypeAlias
66

77
if sys.version_info >= (3, 9):
88
__all__ = ["Error", "open"]
@@ -19,8 +19,8 @@ class _aifc_params(NamedTuple):
1919
comptype: bytes
2020
compname: bytes
2121

22-
_File = str | IO[bytes]
23-
_Marker = tuple[int, int, bytes]
22+
_File: TypeAlias = str | IO[bytes]
23+
_Marker: TypeAlias = tuple[int, int, bytes]
2424

2525
class Aifc_read:
2626
def __init__(self, f: _File) -> None: ...

stdlib/array.pyi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import sys
22
from _typeshed import Self
33
from typing import Any, BinaryIO, Generic, Iterable, MutableSequence, TypeVar, overload
4-
from typing_extensions import Literal, SupportsIndex
4+
from typing_extensions import Literal, SupportsIndex, TypeAlias
55

6-
_IntTypeCode = Literal["b", "B", "h", "H", "i", "I", "l", "L", "q", "Q"]
7-
_FloatTypeCode = Literal["f", "d"]
8-
_UnicodeTypeCode = Literal["u"]
9-
_TypeCode = _IntTypeCode | _FloatTypeCode | _UnicodeTypeCode
6+
_IntTypeCode: TypeAlias = Literal["b", "B", "h", "H", "i", "I", "l", "L", "q", "Q"]
7+
_FloatTypeCode: TypeAlias = Literal["f", "d"]
8+
_UnicodeTypeCode: TypeAlias = Literal["u"]
9+
_TypeCode: TypeAlias = _IntTypeCode | _FloatTypeCode | _UnicodeTypeCode
1010

1111
_T = TypeVar("_T", int, float, str)
1212

stdlib/asyncio/base_events.pyi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ from asyncio.transports import BaseTransport, ReadTransport, SubprocessTransport
99
from collections.abc import Iterable
1010
from socket import AddressFamily, SocketKind, _Address, _RetAddress, socket
1111
from typing import IO, Any, Awaitable, Callable, Coroutine, Generator, Sequence, TypeVar, overload
12-
from typing_extensions import Literal
12+
from typing_extensions import Literal, TypeAlias
1313

1414
if sys.version_info >= (3, 7):
1515
from contextvars import Context
@@ -23,10 +23,10 @@ else:
2323

2424
_T = TypeVar("_T")
2525
_ProtocolT = TypeVar("_ProtocolT", bound=BaseProtocol)
26-
_Context = dict[str, Any]
27-
_ExceptionHandler = Callable[[AbstractEventLoop, _Context], Any]
28-
_ProtocolFactory = Callable[[], BaseProtocol]
29-
_SSLContext = bool | None | ssl.SSLContext
26+
_Context: TypeAlias = dict[str, Any]
27+
_ExceptionHandler: TypeAlias = Callable[[AbstractEventLoop, _Context], Any]
28+
_ProtocolFactory: TypeAlias = Callable[[], BaseProtocol]
29+
_SSLContext: TypeAlias = bool | None | ssl.SSLContext
3030

3131
class Server(AbstractServer):
3232
if sys.version_info >= (3, 7):

stdlib/asyncio/base_subprocess.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import subprocess
22
from collections import deque
33
from typing import IO, Any, Callable, Sequence
4+
from typing_extensions import TypeAlias
45

56
from . import events, futures, protocols, transports
67

7-
_File = int | IO[Any] | None
8+
_File: TypeAlias = int | IO[Any] | None
89

910
class BaseSubprocessTransport(transports.SubprocessTransport):
1011

stdlib/asyncio/events.pyi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ from _typeshed import FileDescriptorLike, Self
44
from abc import ABCMeta, abstractmethod
55
from socket import AddressFamily, SocketKind, _Address, _RetAddress, socket
66
from typing import IO, Any, Awaitable, Callable, Coroutine, Generator, Sequence, TypeVar, overload
7-
from typing_extensions import Literal
7+
from typing_extensions import Literal, TypeAlias
88

99
from .base_events import Server
1010
from .futures import Future
@@ -75,10 +75,10 @@ else:
7575

7676
_T = TypeVar("_T")
7777
_ProtocolT = TypeVar("_ProtocolT", bound=BaseProtocol)
78-
_Context = dict[str, Any]
79-
_ExceptionHandler = Callable[[AbstractEventLoop, _Context], Any]
80-
_ProtocolFactory = Callable[[], BaseProtocol]
81-
_SSLContext = bool | None | ssl.SSLContext
78+
_Context: TypeAlias = dict[str, Any]
79+
_ExceptionHandler: TypeAlias = Callable[[AbstractEventLoop, _Context], Any]
80+
_ProtocolFactory: TypeAlias = Callable[[], BaseProtocol]
81+
_SSLContext: TypeAlias = bool | None | ssl.SSLContext
8282

8383
class Handle:
8484
_cancelled: bool

stdlib/asyncio/format_helpers.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import functools
22
import traceback
33
from types import FrameType, FunctionType
44
from typing import Any, Iterable, overload
5+
from typing_extensions import TypeAlias
56

67
class _HasWrapper:
78
__wrapper__: _HasWrapper | FunctionType
89

9-
_FuncType = FunctionType | _HasWrapper | functools.partial[Any] | functools.partialmethod[Any]
10+
_FuncType: TypeAlias = FunctionType | _HasWrapper | functools.partial[Any] | functools.partialmethod[Any]
1011

1112
@overload
1213
def _get_function_source(func: _FuncType) -> tuple[str, int]: ...

stdlib/asyncio/streams.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import sys
22
from _typeshed import Self, StrPath
33
from typing import Any, AsyncIterator, Awaitable, Callable, Iterable, Sequence
4+
from typing_extensions import TypeAlias
45

56
from . import events, protocols, transports
67
from .base_events import Server
@@ -64,7 +65,7 @@ else:
6465
"start_unix_server",
6566
]
6667

67-
_ClientConnectedCallback = Callable[[StreamReader, StreamWriter], Awaitable[None] | None]
68+
_ClientConnectedCallback: TypeAlias = Callable[[StreamReader, StreamWriter], Awaitable[None] | None]
6869

6970
if sys.version_info < (3, 8):
7071
class IncompleteReadError(EOFError):

stdlib/asyncio/subprocess.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ import sys
33
from _typeshed import StrOrBytesPath
44
from asyncio import events, protocols, streams, transports
55
from typing import IO, Any, Callable
6-
from typing_extensions import Literal
6+
from typing_extensions import Literal, TypeAlias
77

88
if sys.version_info >= (3, 7):
99
__all__ = ("create_subprocess_exec", "create_subprocess_shell")
1010
else:
1111
__all__ = ["create_subprocess_exec", "create_subprocess_shell"]
1212

1313
if sys.version_info >= (3, 8):
14-
_ExecArg = StrOrBytesPath
14+
_ExecArg: TypeAlias = StrOrBytesPath
1515
else:
16-
_ExecArg = str | bytes
16+
_ExecArg: TypeAlias = str | bytes
1717

1818
PIPE: int
1919
STDOUT: int

stdlib/asyncio/tasks.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import sys
33
from collections.abc import Awaitable, Generator, Iterable, Iterator
44
from types import FrameType
55
from typing import Any, Coroutine, Generic, TextIO, TypeVar, overload
6-
from typing_extensions import Literal
6+
from typing_extensions import Literal, TypeAlias
77

88
from .events import AbstractEventLoop
99
from .futures import Future
@@ -56,8 +56,8 @@ _T3 = TypeVar("_T3")
5656
_T4 = TypeVar("_T4")
5757
_T5 = TypeVar("_T5")
5858
_FT = TypeVar("_FT", bound=Future[Any])
59-
_FutureT = Future[_T] | Generator[Any, None, _T] | Awaitable[_T]
60-
_TaskYieldType = Future[object] | None
59+
_FutureT: TypeAlias = Future[_T] | Generator[Any, None, _T] | Awaitable[_T]
60+
_TaskYieldType: TypeAlias = Future[object] | None
6161

6262
FIRST_COMPLETED = concurrent.futures.FIRST_COMPLETED
6363
FIRST_EXCEPTION = concurrent.futures.FIRST_EXCEPTION

stdlib/asyncio/trsock.pyi

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import sys
33
from builtins import type as Type # alias to avoid name clashes with property named "type"
44
from types import TracebackType
55
from typing import Any, BinaryIO, Iterable, NoReturn, overload
6+
from typing_extensions import TypeAlias
67

78
# These are based in socket, maybe move them out into _typeshed.pyi or such
8-
_Address = tuple[Any, ...] | str
9-
_RetAddress = Any
10-
_WriteBuffer = bytearray | memoryview
11-
_CMSG = tuple[int, int, bytes]
9+
_Address: TypeAlias = tuple[Any, ...] | str
10+
_RetAddress: TypeAlias = Any
11+
_WriteBuffer: TypeAlias = bytearray | memoryview
12+
_CMSG: TypeAlias = tuple[int, int, bytes]
1213

1314
class TransportSocket:
1415
def __init__(self, sock: socket.socket) -> None: ...

stdlib/asyncore.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import sys
22
from _typeshed import FileDescriptorLike
33
from socket import socket
44
from typing import Any, overload
5+
from typing_extensions import TypeAlias
56

67
# cyclic dependence with asynchat
7-
_maptype = dict[int, Any]
8-
_socket = socket
8+
_maptype: TypeAlias = dict[int, Any]
9+
_socket: TypeAlias = socket
910

1011
socket_map: _maptype # undocumented
1112

stdlib/audioop.pyi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
AdpcmState = tuple[int, int]
2-
RatecvState = tuple[int, tuple[tuple[int, int], ...]]
1+
from typing_extensions import TypeAlias
2+
3+
AdpcmState: TypeAlias = tuple[int, int]
4+
RatecvState: TypeAlias = tuple[int, tuple[tuple[int, int], ...]]
35

46
class error(Exception): ...
57

stdlib/bdb.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from types import CodeType, FrameType, TracebackType
22
from typing import IO, Any, Callable, Iterable, Mapping, SupportsInt, TypeVar
3-
from typing_extensions import Literal, ParamSpec
3+
from typing_extensions import Literal, ParamSpec, TypeAlias
44

55
__all__ = ["BdbQuit", "Bdb", "Breakpoint"]
66

77
_T = TypeVar("_T")
88
_P = ParamSpec("_P")
9-
_TraceDispatch = Callable[[FrameType, str, Any], Any] # TODO: Recursive type
10-
_ExcInfo = tuple[type[BaseException], BaseException, FrameType]
9+
_TraceDispatch: TypeAlias = Callable[[FrameType, str, Any], Any] # TODO: Recursive type
10+
_ExcInfo: TypeAlias = tuple[type[BaseException], BaseException, FrameType]
1111

1212
GENERATOR_AND_COROUTINE_FLAGS: Literal[672]
1313

stdlib/binhex.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from typing import IO, Any
2-
from typing_extensions import Literal
2+
from typing_extensions import Literal, TypeAlias
33

44
__all__ = ["binhex", "hexbin", "Error"]
55

@@ -15,8 +15,8 @@ class FInfo:
1515
Creator: str
1616
Flags: int
1717

18-
_FileInfoTuple = tuple[str, FInfo, int, int]
19-
_FileHandleUnion = str | IO[bytes]
18+
_FileInfoTuple: TypeAlias = tuple[str, FInfo, int, int]
19+
_FileHandleUnion: TypeAlias = str | IO[bytes]
2020

2121
def getfileinfo(name: str) -> _FileInfoTuple: ...
2222

0 commit comments

Comments
 (0)