Skip to content

Commit 777b2a3

Browse files
authored
Use PEP 585 collections (#18378)
1 parent 7982761 commit 777b2a3

Some content is hidden

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

50 files changed

+122
-156
lines changed

misc/analyze_cache.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
import os.path
88
from collections import Counter
99
from collections.abc import Iterable
10-
from typing import Any, Dict, Final
10+
from typing import Any, Final
1111
from typing_extensions import TypeAlias as _TypeAlias
1212

1313
ROOT: Final = ".mypy_cache/3.5"
1414

15-
JsonDict: _TypeAlias = Dict[str, Any]
15+
JsonDict: _TypeAlias = dict[str, Any]
1616

1717

1818
class CacheData:

misc/incremental_checker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@
4444
import textwrap
4545
import time
4646
from argparse import ArgumentParser, Namespace, RawDescriptionHelpFormatter
47-
from typing import Any, Dict, Final
47+
from typing import Any, Final
4848
from typing_extensions import TypeAlias as _TypeAlias
4949

5050
CACHE_PATH: Final = ".incremental_checker_cache.json"
5151
MYPY_REPO_URL: Final = "https://github.com/python/mypy.git"
5252
MYPY_TARGET_FILE: Final = "mypy"
5353
DAEMON_CMD: Final = ["python3", "-m", "mypy.dmypy"]
5454

55-
JsonDict: _TypeAlias = Dict[str, Any]
55+
JsonDict: _TypeAlias = dict[str, Any]
5656

5757

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

mypy/binder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from collections import defaultdict
44
from collections.abc import Iterator
55
from contextlib import contextmanager
6-
from typing import DefaultDict, List, NamedTuple, Optional, Tuple, Union
6+
from typing import NamedTuple, Optional, Union
77
from typing_extensions import TypeAlias as _TypeAlias
88

99
from mypy.erasetype import remove_instance_last_known_values
@@ -59,7 +59,7 @@ def __repr__(self) -> str:
5959
return f"Frame({self.id}, {self.types}, {self.unreachable}, {self.conditional_frame})"
6060

6161

62-
Assigns = DefaultDict[Expression, List[Tuple[Type, Optional[Type]]]]
62+
Assigns = defaultdict[Expression, list[tuple[Type, Optional[Type]]]]
6363

6464

6565
class ConditionalTypeBinder:

mypy/build.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
Any,
3333
Callable,
3434
ClassVar,
35-
Dict,
3635
Final,
3736
NamedTuple,
3837
NoReturn,
@@ -118,7 +117,7 @@
118117
}
119118

120119

121-
Graph: _TypeAlias = Dict[str, "State"]
120+
Graph: _TypeAlias = dict[str, "State"]
122121

123122

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

mypy/checker.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@
99
from typing import (
1010
AbstractSet,
1111
Callable,
12-
Dict,
1312
Final,
1413
Generic,
1514
NamedTuple,
1615
Optional,
17-
Tuple,
1816
TypeVar,
1917
Union,
2018
cast,
@@ -265,7 +263,7 @@ class FineGrainedDeferredNode(NamedTuple):
265263
# (such as two references to the same variable). TODO: it would
266264
# probably be better to have the dict keyed by the nodes' literal_hash
267265
# field instead.
268-
TypeMap: _TypeAlias = Optional[Dict[Expression, Type]]
266+
TypeMap: _TypeAlias = Optional[dict[Expression, Type]]
269267

270268

271269
# An object that represents either a precise type or a type with an upper bound;
@@ -7813,7 +7811,7 @@ def conditional_types_to_typemaps(
78137811
assert typ is not None
78147812
maps.append({expr: typ})
78157813

7816-
return cast(Tuple[TypeMap, TypeMap], tuple(maps))
7814+
return cast(tuple[TypeMap, TypeMap], tuple(maps))
78177815

78187816

78197817
def gen_unique_name(base: str, table: SymbolTable) -> str:

mypy/checkexpr.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from collections import defaultdict
99
from collections.abc import Iterable, Iterator, Sequence
1010
from contextlib import contextmanager
11-
from typing import Callable, ClassVar, Final, List, Optional, cast
11+
from typing import Callable, ClassVar, Final, Optional, cast
1212
from typing_extensions import TypeAlias as _TypeAlias, assert_never, overload
1313

1414
import mypy.checker
@@ -1966,7 +1966,7 @@ def infer_arg_types_in_context(
19661966
if not t:
19671967
res[i] = self.accept(args[i])
19681968
assert all(tp is not None for tp in res)
1969-
return cast(List[Type], res)
1969+
return cast(list[Type], res)
19701970

19711971
def infer_function_type_arguments_using_context(
19721972
self, callable: CallableType, error_context: Context

mypy/checkstrformat.py

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

1515
import re
1616
from re import Match, Pattern
17-
from typing import TYPE_CHECKING, Callable, Dict, Final, Tuple, Union, cast
17+
from typing import TYPE_CHECKING, Callable, Final, Union, cast
1818
from typing_extensions import TypeAlias as _TypeAlias
1919

2020
import mypy.errorcodes as codes
@@ -70,8 +70,8 @@
7070
from mypy.typeops import custom_special_method
7171

7272
FormatStringExpr: _TypeAlias = Union[StrExpr, BytesExpr]
73-
Checkers: _TypeAlias = Tuple[Callable[[Expression], None], Callable[[Type], bool]]
74-
MatchMap: _TypeAlias = Dict[Tuple[int, int], Match[str]] # span -> match
73+
Checkers: _TypeAlias = tuple[Callable[[Expression], None], Callable[[Type], bool]]
74+
MatchMap: _TypeAlias = dict[tuple[int, int], Match[str]] # span -> match
7575

7676

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

mypy/config_parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
import tomli as tomllib
1717

1818
from collections.abc import Iterable, Mapping, MutableMapping, Sequence
19-
from typing import Any, Callable, Dict, Final, List, TextIO, Tuple, Union
19+
from typing import Any, Callable, Final, TextIO, Union
2020
from typing_extensions import TypeAlias as _TypeAlias
2121

2222
from mypy import defaults
2323
from mypy.options import PER_MODULE_OPTIONS, Options
2424

2525
_CONFIG_VALUE_TYPES: _TypeAlias = Union[
26-
str, bool, int, float, Dict[str, str], List[str], Tuple[int, int]
26+
str, bool, int, float, dict[str, str], list[str], tuple[int, int]
2727
]
2828
_INI_PARSER_CALLABLE: _TypeAlias = Callable[[Any], _CONFIG_VALUE_TYPES]
2929

mypy/constraints.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import annotations
44

55
from collections.abc import Iterable, Sequence
6-
from typing import TYPE_CHECKING, Final, List
6+
from typing import TYPE_CHECKING, Final
77

88
import mypy.subtypes
99
import mypy.typeops
@@ -627,7 +627,7 @@ def visit_uninhabited_type(self, t: UninhabitedType) -> bool:
627627
return False
628628

629629

630-
class ConstraintBuilderVisitor(TypeVisitor[List[Constraint]]):
630+
class ConstraintBuilderVisitor(TypeVisitor[list[Constraint]]):
631631
"""Visitor class for inferring type constraints."""
632632

633633
# The type that is compared against a template

mypy/dmypy_server.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import traceback
1919
from collections.abc import Sequence
2020
from contextlib import redirect_stderr, redirect_stdout
21-
from typing import AbstractSet, Any, Callable, Final, List, Tuple
21+
from typing import AbstractSet, Any, Callable, Final
2222
from typing_extensions import TypeAlias as _TypeAlias
2323

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

164164

165-
ModulePathPair: _TypeAlias = Tuple[str, str]
166-
ModulePathPairs: _TypeAlias = List[ModulePathPair]
167-
ChangesAndRemovals: _TypeAlias = Tuple[ModulePathPairs, ModulePathPairs]
165+
ModulePathPair: _TypeAlias = tuple[str, str]
166+
ModulePathPairs: _TypeAlias = list[ModulePathPair]
167+
ChangesAndRemovals: _TypeAlias = tuple[ModulePathPairs, ModulePathPairs]
168168

169169

170170
class Server:

mypy/errors.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import traceback
66
from collections import defaultdict
77
from collections.abc import Iterable
8-
from typing import Callable, Final, NoReturn, Optional, TextIO, Tuple, TypeVar
8+
from typing import Callable, Final, NoReturn, Optional, TextIO, TypeVar
99
from typing_extensions import Literal, TypeAlias as _TypeAlias
1010

1111
from mypy import errorcodes as codes
@@ -152,7 +152,7 @@ def __init__(
152152

153153
# Type used internally to represent errors:
154154
# (path, line, column, end_line, end_column, severity, message, allow_dups, code)
155-
ErrorTuple: _TypeAlias = Tuple[
155+
ErrorTuple: _TypeAlias = tuple[
156156
Optional[str], int, int, int, int, str, str, bool, Optional[ErrorCode]
157157
]
158158

@@ -1328,7 +1328,7 @@ def __init__(
13281328

13291329

13301330
# (file_path, line, column)
1331-
_ErrorLocation = Tuple[str, int, int]
1331+
_ErrorLocation = tuple[str, int, int]
13321332

13331333

13341334
def create_errors(error_tuples: list[ErrorTuple]) -> list[MypyError]:

mypy/fastparse.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import sys
55
import warnings
66
from collections.abc import Sequence
7-
from typing import Any, Callable, Final, List, Optional, TypeVar, Union, cast
7+
from typing import Any, Callable, Final, Optional, TypeVar, Union, cast
88
from typing_extensions import Literal, overload
99

1010
from mypy import defaults, errorcodes as codes, message_registry
@@ -425,7 +425,7 @@ def translate_opt_expr_list(self, l: Sequence[AST | None]) -> list[Expression |
425425
return res
426426

427427
def translate_expr_list(self, l: Sequence[AST]) -> list[Expression]:
428-
return cast(List[Expression], self.translate_opt_expr_list(l))
428+
return cast(list[Expression], self.translate_opt_expr_list(l))
429429

430430
def get_lineno(self, node: ast3.expr | ast3.stmt) -> int:
431431
if (
@@ -668,7 +668,7 @@ def fix_function_overloads(self, stmts: list[Statement]) -> list[Statement]:
668668
current_overload.append(last_if_overload)
669669
last_if_stmt, last_if_overload = None, None
670670
if isinstance(if_block_with_overload.body[-1], OverloadedFuncDef):
671-
skipped_if_stmts.extend(cast(List[IfStmt], if_block_with_overload.body[:-1]))
671+
skipped_if_stmts.extend(cast(list[IfStmt], if_block_with_overload.body[:-1]))
672672
current_overload.extend(if_block_with_overload.body[-1].items)
673673
else:
674674
current_overload.append(
@@ -715,7 +715,7 @@ def fix_function_overloads(self, stmts: list[Statement]) -> list[Statement]:
715715
last_if_stmt_overload_name = None
716716
if if_block_with_overload is not None:
717717
skipped_if_stmts.extend(
718-
cast(List[IfStmt], if_block_with_overload.body[:-1])
718+
cast(list[IfStmt], if_block_with_overload.body[:-1])
719719
)
720720
last_if_overload = cast(
721721
Union[Decorator, FuncDef, OverloadedFuncDef],
@@ -939,7 +939,7 @@ def do_func_def(
939939
self.errors, line=lineno, override_column=n.col_offset
940940
).translate_expr_list(func_type_ast.argtypes)
941941
# Use a cast to work around `list` invariance
942-
arg_types = cast(List[Optional[Type]], translated_args)
942+
arg_types = cast(list[Optional[Type]], translated_args)
943943
return_type = TypeConverter(self.errors, line=lineno).visit(func_type_ast.returns)
944944

945945
# add implicit self type
@@ -1051,7 +1051,7 @@ def transform_args(
10511051
) -> list[Argument]:
10521052
new_args = []
10531053
names: list[ast3.arg] = []
1054-
posonlyargs = getattr(args, "posonlyargs", cast(List[ast3.arg], []))
1054+
posonlyargs = getattr(args, "posonlyargs", cast(list[ast3.arg], []))
10551055
args_args = posonlyargs + args.args
10561056
args_defaults = args.defaults
10571057
num_no_defaults = len(args_args) - len(args_defaults)
@@ -1589,7 +1589,7 @@ def visit_Call(self, n: Call) -> CallExpr:
15891589
self.visit(n.func),
15901590
arg_types,
15911591
arg_kinds,
1592-
cast("List[Optional[str]]", [None] * len(args)) + keyword_names,
1592+
cast("list[Optional[str]]", [None] * len(args)) + keyword_names,
15931593
)
15941594
return self.set_line(e, n)
15951595

mypy/literals.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from collections.abc import Iterable
4-
from typing import Any, Final, Optional, Tuple
4+
from typing import Any, Final, Optional
55
from typing_extensions import TypeAlias as _TypeAlias
66

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

131131

132-
Key: _TypeAlias = Tuple[Any, ...]
132+
Key: _TypeAlias = tuple[Any, ...]
133133

134134

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

mypy/memprofile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import sys
1111
from collections import defaultdict
1212
from collections.abc import Iterable
13-
from typing import Dict, cast
13+
from typing import cast
1414

1515
from mypy.nodes import FakeInfo, Node
1616
from mypy.types import Type
@@ -109,7 +109,7 @@ def visit(o: object) -> None:
109109
# Processing these would cause a crash.
110110
continue
111111
if type(obj) in (dict, defaultdict):
112-
for key, val in cast(Dict[object, object], obj).items():
112+
for key, val in cast(dict[object, object], obj).items():
113113
visit(key)
114114
visit(val)
115115
if type(obj) in (list, tuple, set):

mypy/messages.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from collections.abc import Collection, Iterable, Iterator, Sequence
1818
from contextlib import contextmanager
1919
from textwrap import dedent
20-
from typing import Any, Callable, Final, List, cast
20+
from typing import Any, Callable, Final, cast
2121

2222
import mypy.typeops
2323
from mypy import errorcodes as codes, message_registry
@@ -955,7 +955,7 @@ def too_few_arguments(
955955
msg = "Missing positional arguments"
956956
callee_name = callable_name(callee)
957957
if callee_name is not None and diff and all(d is not None for d in diff):
958-
args = '", "'.join(cast(List[str], diff))
958+
args = '", "'.join(cast(list[str], diff))
959959
msg += f' "{args}" in call to {callee_name}'
960960
else:
961961
msg = "Too few arguments" + for_function(callee)

mypy/modulefinder.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import subprocess
1414
import sys
1515
from enum import Enum, unique
16-
from typing import Dict, Final, List, Optional, Tuple, Union
16+
from typing import Final, Optional, Union
1717
from typing_extensions import TypeAlias as _TypeAlias
1818

1919
from mypy import pyinfo
@@ -53,11 +53,11 @@ def asdict(self) -> dict[str, tuple[str, ...]]:
5353

5454

5555
# Package dirs are a two-tuple of path to search and whether to verify the module
56-
OnePackageDir = Tuple[str, bool]
57-
PackageDirs = List[OnePackageDir]
56+
OnePackageDir = tuple[str, bool]
57+
PackageDirs = list[OnePackageDir]
5858

5959
# Minimum and maximum Python versions for modules in stdlib as (major, minor)
60-
StdlibVersions: _TypeAlias = Dict[str, Tuple[Tuple[int, int], Optional[Tuple[int, int]]]]
60+
StdlibVersions: _TypeAlias = dict[str, tuple[tuple[int, int], Optional[tuple[int, int]]]]
6161

6262
PYTHON_EXTENSIONS: Final = [".pyi", ".py"]
6363

0 commit comments

Comments
 (0)