Skip to content

Commit f497645

Browse files
authored
Add TypeAlias annotations to mypy code (#11321)
1 parent d3fe55a commit f497645

14 files changed

+58
-49
lines changed

mypy/binder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from collections import defaultdict
33

44
from typing import Dict, List, Set, Iterator, Union, Optional, Tuple, cast
5-
from typing_extensions import DefaultDict
5+
from typing_extensions import DefaultDict, TypeAlias as _TypeAlias
66

77
from mypy.types import (
88
Type, AnyType, PartialType, UnionType, TypeOfAny, NoneType, get_proper_type
@@ -16,7 +16,7 @@
1616
from mypy.nodes import IndexExpr, MemberExpr, AssignmentExpr, NameExpr
1717

1818

19-
BindableExpression = Union[IndexExpr, MemberExpr, AssignmentExpr, NameExpr]
19+
BindableExpression: _TypeAlias = Union[IndexExpr, MemberExpr, AssignmentExpr, NameExpr]
2020

2121

2222
class Frame:

mypy/build.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
from typing import (AbstractSet, Any, Dict, Iterable, Iterator, List, Sequence,
2525
Mapping, NamedTuple, Optional, Set, Tuple, TypeVar, Union, Callable, TextIO)
26-
from typing_extensions import ClassVar, Final, TYPE_CHECKING
26+
from typing_extensions import ClassVar, Final, TYPE_CHECKING, TypeAlias as _TypeAlias
2727
from mypy_extensions import TypedDict
2828

2929
from mypy.nodes import MypyFile, ImportBase, Import, ImportFrom, ImportAll, SymbolTable
@@ -81,7 +81,7 @@
8181
}
8282

8383

84-
Graph = Dict[str, 'State']
84+
Graph: _TypeAlias = Dict[str, 'State']
8585

8686

8787
# TODO: Get rid of BuildResult. We might as well return a BuildManager.

mypy/checker.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
Any, Dict, Set, List, cast, Tuple, TypeVar, Union, Optional, NamedTuple, Iterator,
99
Iterable, Sequence, Mapping, Generic, AbstractSet, Callable
1010
)
11-
from typing_extensions import Final
11+
from typing_extensions import Final, TypeAlias as _TypeAlias
1212

1313
from mypy.backports import nullcontext
1414
from mypy.errors import Errors, report_internal_error
@@ -86,8 +86,8 @@
8686

8787
DEFAULT_LAST_PASS: Final = 1 # Pass numbers start at 0
8888

89-
DeferredNodeType = Union[FuncDef, LambdaExpr, OverloadedFuncDef, Decorator]
90-
FineGrainedDeferredNodeType = Union[FuncDef, MypyFile, OverloadedFuncDef]
89+
DeferredNodeType: _TypeAlias = Union[FuncDef, LambdaExpr, OverloadedFuncDef, Decorator]
90+
FineGrainedDeferredNodeType: _TypeAlias = Union[FuncDef, MypyFile, OverloadedFuncDef]
9191

9292
# A node which is postponed to be processed during the next pass.
9393
# In normal mode one can defer functions and methods (also decorated and/or overloaded)
@@ -124,7 +124,7 @@
124124
# probably be better to have the dict keyed by the nodes' literal_hash
125125
# field instead.
126126

127-
TypeMap = Optional[Dict[Expression, Type]]
127+
TypeMap: _TypeAlias = Optional[Dict[Expression, Type]]
128128

129129
# An object that represents either a precise type or a type with an upper bound;
130130
# it is important for correct type inference with isinstance.

mypy/checkexpr.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from typing import (
88
Any, cast, Dict, Set, List, Tuple, Callable, Union, Optional, Sequence, Iterator
99
)
10-
from typing_extensions import ClassVar, Final, overload
10+
from typing_extensions import ClassVar, Final, overload, TypeAlias as _TypeAlias
1111

1212
from mypy.errors import report_internal_error
1313
from mypy.typeanal import (
@@ -72,18 +72,21 @@
7272

7373
# Type of callback user for checking individual function arguments. See
7474
# check_args() below for details.
75-
ArgChecker = Callable[[Type,
76-
Type,
77-
ArgKind,
78-
Type,
79-
int,
80-
int,
81-
CallableType,
82-
Optional[Type],
83-
Context,
84-
Context,
85-
MessageBuilder],
86-
None]
75+
ArgChecker: _TypeAlias = Callable[[
76+
Type,
77+
Type,
78+
ArgKind,
79+
Type,
80+
int,
81+
int,
82+
CallableType,
83+
Optional[Type],
84+
Context,
85+
Context,
86+
MessageBuilder,
87+
],
88+
None,
89+
]
8790

8891
# Maximum nesting level for math union in overloads, setting this to large values
8992
# may cause performance issues. The reason is that although union math algorithm we use

mypy/checkstrformat.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from typing import (
1616
cast, List, Tuple, Dict, Callable, Union, Optional, Pattern, Match, Set
1717
)
18-
from typing_extensions import Final, TYPE_CHECKING
18+
from typing_extensions import Final, TYPE_CHECKING, TypeAlias as _TypeAlias
1919

2020
from mypy.types import (
2121
Type, AnyType, TupleType, Instance, UnionType, TypeOfAny, get_proper_type, TypeVarType,
@@ -39,9 +39,9 @@
3939
from mypy.subtypes import is_subtype
4040
from mypy.parse import parse
4141

42-
FormatStringExpr = Union[StrExpr, BytesExpr, UnicodeExpr]
43-
Checkers = Tuple[Callable[[Expression], None], Callable[[Type], bool]]
44-
MatchMap = Dict[Tuple[int, int], Match[str]] # span -> match
42+
FormatStringExpr: _TypeAlias = Union[StrExpr, BytesExpr, UnicodeExpr]
43+
Checkers: _TypeAlias = Tuple[Callable[[Expression], None], Callable[[Type], bool]]
44+
MatchMap: _TypeAlias = Dict[Tuple[int, int], Match[str]] # span -> match
4545

4646

4747
def compile_format_re() -> Pattern[str]:

mypy/config_parser.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
import tomli
1010
from typing import (Any, Callable, Dict, List, Mapping, MutableMapping, Optional, Sequence,
1111
TextIO, Tuple, Union)
12-
from typing_extensions import Final
12+
from typing_extensions import Final, TypeAlias as _TypeAlias
1313

1414
from mypy import defaults
1515
from mypy.options import Options, PER_MODULE_OPTIONS
1616

17-
_CONFIG_VALUE_TYPES = Union[str, bool, int, float, Dict[str, str], List[str], Tuple[int, int]]
18-
_INI_PARSER_CALLABLE = Callable[[Any], _CONFIG_VALUE_TYPES]
17+
_CONFIG_VALUE_TYPES: _TypeAlias = Union[
18+
str, bool, int, float, Dict[str, str], List[str], Tuple[int, int],
19+
]
20+
_INI_PARSER_CALLABLE: _TypeAlias = Callable[[Any], _CONFIG_VALUE_TYPES]
1921

2022

2123
def parse_version(v: str) -> Tuple[int, int]:

mypy/nodes.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from typing import (
99
Any, TypeVar, List, Tuple, cast, Set, Dict, Union, Optional, Callable, Sequence, Iterator
1010
)
11-
from typing_extensions import DefaultDict, Final, TYPE_CHECKING
11+
from typing_extensions import DefaultDict, Final, TYPE_CHECKING, TypeAlias as _TypeAlias
1212
from mypy_extensions import trait
1313

1414
import mypy.strconv
@@ -64,7 +64,7 @@ def get_column(self) -> int:
6464

6565
T = TypeVar('T')
6666

67-
JsonDict = Dict[str, Any]
67+
JsonDict: _TypeAlias = Dict[str, Any]
6868

6969

7070
# Symbol table node kinds
@@ -208,7 +208,7 @@ class FakeExpression(Expression):
208208
# TODO:
209209
# Lvalue = Union['NameExpr', 'MemberExpr', 'IndexExpr', 'SuperExpr', 'StarExpr'
210210
# 'TupleExpr']; see #1783.
211-
Lvalue = Expression
211+
Lvalue: _TypeAlias = Expression
212212

213213

214214
@trait
@@ -241,7 +241,7 @@ def deserialize(cls, data: JsonDict) -> 'SymbolNode':
241241

242242

243243
# Items: fullname, related symbol table node, surrounding type (if any)
244-
Definition = Tuple[str, 'SymbolTableNode', Optional['TypeInfo']]
244+
Definition: _TypeAlias = Tuple[str, 'SymbolTableNode', Optional['TypeInfo']]
245245

246246

247247
class MypyFile(SymbolNode):
@@ -514,7 +514,7 @@ def fullname(self) -> Bogus[str]:
514514
return self._fullname
515515

516516

517-
OverloadPart = Union['FuncDef', 'Decorator']
517+
OverloadPart: _TypeAlias = Union['FuncDef', 'Decorator']
518518

519519

520520
class OverloadedFuncDef(FuncBase, SymbolNode, Statement):

mypy/report.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import typing
1616
from typing import Any, Callable, Dict, List, Optional, Tuple, cast, Iterator
17-
from typing_extensions import Final
17+
from typing_extensions import Final, TypeAlias as _TypeAlias
1818

1919
from mypy.nodes import MypyFile, Expression, FuncDef
2020
from mypy import stats
@@ -45,7 +45,10 @@
4545
]
4646
)
4747

48-
ReporterClasses = Dict[str, Tuple[Callable[['Reports', str], 'AbstractReporter'], bool]]
48+
ReporterClasses: _TypeAlias = Dict[
49+
str,
50+
Tuple[Callable[['Reports', str], 'AbstractReporter'], bool],
51+
]
4952

5053
reporter_classes: Final[ReporterClasses] = {}
5154

mypy/scope.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55

66
from contextlib import contextmanager
77
from typing import List, Optional, Iterator, Tuple
8+
from typing_extensions import TypeAlias as _TypeAlias
89

910
from mypy.backports import nullcontext
1011
from mypy.nodes import TypeInfo, FuncBase
1112

1213

13-
SavedScope = Tuple[str, Optional[TypeInfo], Optional[FuncBase]]
14+
SavedScope: _TypeAlias = Tuple[str, Optional[TypeInfo], Optional[FuncBase]]
1415

1516

1617
class Scope:

mypy/semanal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
from typing import (
5454
List, Dict, Set, Tuple, cast, TypeVar, Union, Optional, Callable, Iterator, Iterable
5555
)
56-
from typing_extensions import Final
56+
from typing_extensions import Final, TypeAlias as _TypeAlias
5757

5858
from mypy.nodes import (
5959
MypyFile, TypeInfo, Node, AssignmentStmt, FuncDef, OverloadedFuncDef,
@@ -147,7 +147,7 @@
147147

148148

149149
# Used for tracking incomplete references
150-
Tag = int
150+
Tag: _TypeAlias = int
151151

152152

153153
class SemanticAnalyzer(NodeVisitor[None],

0 commit comments

Comments
 (0)