Skip to content

Commit b093c90

Browse files
authored
Use TypeAlias for type aliases where possible, part II (#7667)
1 parent c653be7 commit b093c90

File tree

41 files changed

+91
-90
lines changed

Some content is hidden

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

41 files changed

+91
-90
lines changed

stdlib/_ast.pyi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import sys
22
from typing import Any, ClassVar
3-
from typing_extensions import Literal
3+
from typing_extensions import Literal, TypeAlias
44

55
PyCF_ONLY_AST: Literal[1024]
66
if sys.version_info >= (3, 8):
77
PyCF_TYPE_COMMENTS: Literal[4096]
88
PyCF_ALLOW_TOP_LEVEL_AWAIT: Literal[8192]
99

10-
_identifier = str
10+
_identifier: TypeAlias = str
1111

1212
class AST:
1313
if sys.version_info >= (3, 10):
@@ -366,10 +366,10 @@ class Attribute(expr):
366366
ctx: expr_context
367367

368368
if sys.version_info >= (3, 9):
369-
_SliceT = expr
369+
_SliceT: TypeAlias = expr
370370
else:
371371
class slice(AST): ...
372-
_SliceT = slice
372+
_SliceT: TypeAlias = slice
373373

374374
class Slice(_SliceT):
375375
if sys.version_info >= (3, 10):
@@ -524,7 +524,7 @@ if sys.version_info >= (3, 10):
524524

525525
class pattern(AST): ...
526526
# Without the alias, Pyright complains variables named pattern are recursively defined
527-
_pattern = pattern
527+
_pattern: TypeAlias = pattern
528528

529529
class match_case(AST):
530530
__match_args__ = ("pattern", "guard", "body")

stdlib/_socket.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ _CMSGArg: TypeAlias = tuple[int, int, ReadableBuffer]
1717
# Addresses can be either tuples of varying lengths (AF_INET, AF_INET6,
1818
# AF_NETLINK, AF_TIPC) or strings (AF_UNIX).
1919
_Address: TypeAlias = tuple[Any, ...] | str
20-
_RetAddress = Any
20+
_RetAddress: TypeAlias = Any
2121
# TODO Most methods allow bytes as address objects
2222

2323
# ----- Constants -----

stdlib/asyncio/streams.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ else:
120120

121121
if sys.platform != "win32":
122122
if sys.version_info >= (3, 7):
123-
_PathType = StrPath
123+
_PathType: TypeAlias = StrPath
124124
else:
125-
_PathType = str
125+
_PathType: TypeAlias = str
126126
if sys.version_info >= (3, 10):
127127
async def open_unix_connection(
128128
path: _PathType | None = ..., *, limit: int = ..., **kwds: Any

stdlib/cgi.pyi

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sys
22
from _typeshed import Self, SupportsGetItem, SupportsItemAccess
3-
from builtins import type as _type
3+
from builtins import list as _list, type as _type
44
from collections.abc import Iterable, Iterator, Mapping
55
from types import TracebackType
66
from typing import IO, Any, Protocol
@@ -87,8 +87,6 @@ class MiniFieldStorage:
8787
value: Any
8888
def __init__(self, name: Any, value: Any) -> None: ...
8989

90-
_list = list
91-
9290
class FieldStorage:
9391
FieldStorageClass: _type | None
9492
keep_blank_values: int

stdlib/configparser.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ _converters: TypeAlias = dict[str, _converter]
3636
_T = TypeVar("_T")
3737

3838
if sys.version_info >= (3, 7):
39-
_Path = StrOrBytesPath
39+
_Path: TypeAlias = StrOrBytesPath
4040
else:
41-
_Path = StrPath
41+
_Path: TypeAlias = StrPath
4242

4343
DEFAULTSECT: Literal["DEFAULT"]
4444
MAX_INTERPOLATION_DEPTH: Literal[10]

stdlib/datetime.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import sys
22
from _typeshed import Self
33
from time import struct_time
44
from typing import ClassVar, NamedTuple, NoReturn, SupportsAbs, TypeVar, overload
5-
from typing_extensions import Literal, final
5+
from typing_extensions import Literal, TypeAlias, final
66

77
if sys.version_info >= (3, 9):
88
__all__ = ("date", "datetime", "time", "timedelta", "timezone", "tzinfo", "MINYEAR", "MAXYEAR")
@@ -19,7 +19,7 @@ class tzinfo:
1919
def fromutc(self, __dt: datetime) -> datetime: ...
2020

2121
# Alias required to avoid name conflicts with date(time).tzinfo.
22-
_tzinfo = tzinfo
22+
_tzinfo: TypeAlias = tzinfo
2323

2424
@final
2525
class timezone(tzinfo):
@@ -150,8 +150,8 @@ class time:
150150
fold: int = ...,
151151
) -> Self: ...
152152

153-
_date = date
154-
_time = time
153+
_date: TypeAlias = date
154+
_time: TypeAlias = time
155155

156156
class timedelta(SupportsAbs[timedelta]):
157157
min: ClassVar[timedelta]

stdlib/email/message.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ _T = TypeVar("_T")
1313

1414
_PayloadType: TypeAlias = list[Message] | str | bytes
1515
_CharsetType: TypeAlias = Charset | str | None
16-
_HeaderType = Any
16+
_HeaderType: TypeAlias = Any
1717

1818
class Message:
1919
policy: Policy # undocumented

stdlib/functools.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class partial(Generic[_T]):
112112
def __class_getitem__(cls, item: Any) -> GenericAlias: ...
113113

114114
# With protocols, this could change into a generic protocol that defines __get__ and returns _T
115-
_Descriptor = Any
115+
_Descriptor: TypeAlias = Any
116116

117117
class partialmethod(Generic[_T]):
118118
func: Callable[..., _T] | _Descriptor

stdlib/inspect.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ def getcoroutinelocals(coroutine: Coroutine[Any, Any, Any]) -> dict[str, Any]: .
514514

515515
# Create private type alias to avoid conflict with symbol of same
516516
# name created in Attribute class.
517-
_Object = object
517+
_Object: TypeAlias = object
518518

519519
class Attribute(NamedTuple):
520520
name: str

stdlib/lib2to3/refactor.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from collections.abc import Container, Generator, Iterable, Mapping
22
from logging import Logger
33
from typing import Any, ClassVar, NoReturn
4+
from typing_extensions import TypeAlias
45

56
from .pgen2.grammar import Grammar
67

7-
_Driver = Any # really lib2to3.driver.Driver
8-
_BottomMatcher = Any # really lib2to3.btm_matcher.BottomMatcher
8+
_Driver: TypeAlias = Any # really lib2to3.driver.Driver
9+
_BottomMatcher: TypeAlias = Any # really lib2to3.btm_matcher.BottomMatcher
910

1011
def get_all_fix_names(fixer_pkg: str, remove_prefix: bool = ...) -> list[str]: ...
1112
def get_fixers_from_package(pkg_name: str) -> list[str]: ...

0 commit comments

Comments
 (0)