Skip to content

Commit 3aaa7a7

Browse files
authored
Use more Final and TypeAlias types (#13433)
1 parent 9a9bc3b commit 3aaa7a7

13 files changed

+36
-28
lines changed

misc/analyze_cache.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
import os.path
88
from collections import Counter
99
from typing import Any, Dict, Iterable, List, Optional
10+
from typing_extensions import Final, TypeAlias as _TypeAlias
1011

11-
ROOT = ".mypy_cache/3.5"
12+
ROOT: Final = ".mypy_cache/3.5"
1213

13-
JsonDict = Dict[str, Any]
14+
JsonDict: _TypeAlias = Dict[str, Any]
1415

1516

1617
class CacheData:

misc/incremental_checker.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,14 @@
4545
import time
4646
from argparse import ArgumentParser, Namespace, RawDescriptionHelpFormatter
4747
from typing import Any, Dict, List, Optional, Tuple
48+
from typing_extensions import TypeAlias as _TypeAlias
4849

49-
CACHE_PATH = ".incremental_checker_cache.json"
50-
MYPY_REPO_URL = "https://github.com/python/mypy.git"
51-
MYPY_TARGET_FILE = "mypy"
52-
DAEMON_CMD = ["python3", "-m", "mypy.dmypy"]
50+
CACHE_PATH: Final = ".incremental_checker_cache.json"
51+
MYPY_REPO_URL: Final = "https://github.com/python/mypy.git"
52+
MYPY_TARGET_FILE: Final = "mypy"
53+
DAEMON_CMD: Final = ["python3", "-m", "mypy.dmypy"]
5354

54-
JsonDict = Dict[str, Any]
55+
JsonDict: _TypeAlias = Dict[str, Any]
5556

5657

5758
def print_offset(text: str, indent_length: int = 4) -> None:

mypy/dmypy_server.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import traceback
1919
from contextlib import redirect_stderr, redirect_stdout
2020
from typing import AbstractSet, Any, Callable, Dict, List, Optional, Sequence, Set, Tuple
21-
from typing_extensions import Final
21+
from typing_extensions import Final, TypeAlias as _TypeAlias
2222

2323
import mypy.build
2424
import mypy.errors
@@ -163,9 +163,9 @@ def ignore_suppressed_imports(module: str) -> bool:
163163
return module.startswith("encodings.")
164164

165165

166-
ModulePathPair = Tuple[str, str]
167-
ModulePathPairs = List[ModulePathPair]
168-
ChangesAndRemovals = Tuple[ModulePathPairs, ModulePathPairs]
166+
ModulePathPair: _TypeAlias = Tuple[str, str]
167+
ModulePathPairs: _TypeAlias = List[ModulePathPair]
168+
ChangesAndRemovals: _TypeAlias = Tuple[ModulePathPairs, ModulePathPairs]
169169

170170

171171
class Server:

mypy/errors.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import traceback
66
from collections import defaultdict
77
from typing import Callable, Dict, List, NoReturn, Optional, Set, TextIO, Tuple, TypeVar, Union
8-
from typing_extensions import Final, Literal
8+
from typing_extensions import Final, Literal, TypeAlias as _TypeAlias
99

1010
from mypy import errorcodes as codes
1111
from mypy.errorcodes import IMPORT, ErrorCode
@@ -125,7 +125,9 @@ def __init__(
125125

126126
# Type used internally to represent errors:
127127
# (path, line, column, end_line, end_column, severity, message, allow_dups, code)
128-
ErrorTuple = Tuple[Optional[str], int, int, int, int, str, str, bool, Optional[ErrorCode]]
128+
ErrorTuple: _TypeAlias = Tuple[
129+
Optional[str], int, int, int, int, str, str, bool, Optional[ErrorCode]
130+
]
129131

130132

131133
class ErrorWatcher:

mypy/literals.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from typing import Any, Iterable, Optional, Tuple, Union
4-
from typing_extensions import Final
4+
from typing_extensions import Final, TypeAlias as _TypeAlias
55

66
from mypy.nodes import (
77
LITERAL_NO,
@@ -128,7 +128,7 @@ def literal(e: Expression) -> int:
128128
return LITERAL_NO
129129

130130

131-
Key = Tuple[Any, ...]
131+
Key: _TypeAlias = Tuple[Any, ...]
132132

133133

134134
def subkeys(key: Key) -> Iterable[Key]:

mypy/plugins/singledispatch.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from typing import List, NamedTuple, Optional, Sequence, TypeVar, Union
4-
from typing_extensions import Final
4+
from typing_extensions import Final, TypeAlias as _TypeAlias
55

66
from mypy.messages import format_type
77
from mypy.nodes import ARG_POS, Argument, Block, ClassDef, Context, SymbolTable, TypeInfo, Var
@@ -76,7 +76,7 @@ def make_fake_register_class_instance(
7676
return Instance(info, type_args)
7777

7878

79-
PluginContext = Union[FunctionContext, MethodContext]
79+
PluginContext: _TypeAlias = Union[FunctionContext, MethodContext]
8080

8181

8282
def fail(ctx: PluginContext, msg: str, context: Optional[Context]) -> None:

mypy/semanal_main.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,9 @@ def process_top_level_function(
293293
analyzer.saved_locals.clear()
294294

295295

296-
TargetInfo = Tuple[str, Union[MypyFile, FuncDef, OverloadedFuncDef, Decorator], Optional[TypeInfo]]
296+
TargetInfo: _TypeAlias = Tuple[
297+
str, Union[MypyFile, FuncDef, OverloadedFuncDef, Decorator], Optional[TypeInfo]
298+
]
297299

298300

299301
def get_all_leaf_targets(file: MypyFile) -> List[TargetInfo]:

mypy/server/astdiff.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class level -- these are handled at attribute level (say, 'mod.Cls.method'
5353
from __future__ import annotations
5454

5555
from typing import Dict, Optional, Sequence, Set, Tuple, Union
56+
from typing_extensions import TypeAlias as _TypeAlias
5657

5758
from mypy.nodes import (
5859
UNBOUND_IMPORTED,
@@ -103,7 +104,7 @@ class level -- these are handled at attribute level (say, 'mod.Cls.method'
103104
# snapshots are immutable).
104105
#
105106
# For example, the snapshot of the 'int' type is ('Instance', 'builtins.int', ()).
106-
SnapshotItem = Tuple[object, ...]
107+
SnapshotItem: _TypeAlias = Tuple[object, ...]
107108

108109

109110
def compare_symbol_table_snapshots(

mypy/server/aststrip.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
from contextlib import contextmanager, nullcontext
3737
from typing import Dict, Iterator, Optional, Tuple, Union
38+
from typing_extensions import TypeAlias as _TypeAlias
3839

3940
from mypy.nodes import (
4041
CLASSDEF_NO_INFO,
@@ -66,7 +67,7 @@
6667
from mypy.types import CallableType
6768
from mypy.typestate import TypeState
6869

69-
SavedAttributes = Dict[Tuple[ClassDef, str], SymbolTableNode]
70+
SavedAttributes: _TypeAlias = Dict[Tuple[ClassDef, str], SymbolTableNode]
7071

7172

7273
def strip_target(

mypy/server/update.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
import sys
119119
import time
120120
from typing import Callable, Dict, List, NamedTuple, Optional, Sequence, Set, Tuple, Union
121-
from typing_extensions import Final
121+
from typing_extensions import Final, TypeAlias as _TypeAlias
122122

123123
from mypy.build import (
124124
DEBUG_FINE_GRAINED,
@@ -540,7 +540,7 @@ class BlockedUpdate(NamedTuple):
540540
messages: List[str]
541541

542542

543-
UpdateResult = Union[NormalUpdate, BlockedUpdate]
543+
UpdateResult: _TypeAlias = Union[NormalUpdate, BlockedUpdate]
544544

545545

546546
def update_module_isolated(

mypy/stubdoc.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
Sequence,
2121
Tuple,
2222
)
23-
from typing_extensions import Final
23+
from typing_extensions import Final, TypeAlias as _TypeAlias
2424

2525
# Type alias for signatures strings in format ('func_name', '(arg, opt_arg=False)').
26-
Sig = Tuple[str, str]
26+
Sig: _TypeAlias = Tuple[str, str]
2727

2828

2929
_TYPE_RE: Final = re.compile(r"^[a-zA-Z_][\w\[\], ]*(\.[a-zA-Z_][\w\[\], ]*)*$")

mypy/stubtest.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1176,7 +1176,7 @@ def verify_typealias(
11761176
# ====================
11771177

11781178

1179-
IGNORED_MODULE_DUNDERS = frozenset(
1179+
IGNORED_MODULE_DUNDERS: typing_extensions.Final = frozenset(
11801180
{
11811181
"__file__",
11821182
"__doc__",
@@ -1196,7 +1196,7 @@ def verify_typealias(
11961196
}
11971197
)
11981198

1199-
IGNORABLE_CLASS_DUNDERS = frozenset(
1199+
IGNORABLE_CLASS_DUNDERS: typing_extensions.Final = frozenset(
12001200
{
12011201
# Special attributes
12021202
"__dict__",

mypy/test/data.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import tempfile
1212
from abc import abstractmethod
1313
from typing import Any, Dict, Iterator, List, NamedTuple, Optional, Pattern, Set, Tuple, Union
14-
from typing_extensions import Final
14+
from typing_extensions import Final, TypeAlias as _TypeAlias
1515

1616
import pytest
1717

@@ -38,7 +38,7 @@ class DeleteFile(NamedTuple):
3838
path: str
3939

4040

41-
FileOperation = Union[UpdateFile, DeleteFile]
41+
FileOperation: _TypeAlias = Union[UpdateFile, DeleteFile]
4242

4343

4444
def parse_test_case(case: DataDrivenTestCase) -> None:

0 commit comments

Comments
 (0)