Skip to content

Commit 6cfdd8c

Browse files
authored
Use PEP 604 annotations everywhere (#20320)
See #20154. Most of this was done with ruff, rest was done by hand
1 parent 1a6ff59 commit 6cfdd8c

32 files changed

+76
-90
lines changed

mypy/binder.py

Lines changed: 3 additions & 3 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 NamedTuple, Optional, Union
6+
from typing import NamedTuple
77
from typing_extensions import TypeAlias as _TypeAlias
88

99
from mypy.erasetype import remove_instance_last_known_values
@@ -39,7 +39,7 @@
3939
)
4040
from mypy.typevars import fill_typevars_with_any
4141

42-
BindableExpression: _TypeAlias = Union[IndexExpr, MemberExpr, NameExpr]
42+
BindableExpression: _TypeAlias = IndexExpr | MemberExpr | NameExpr
4343

4444

4545
class CurrentType(NamedTuple):
@@ -81,7 +81,7 @@ def __repr__(self) -> str:
8181
return f"Frame({self.id}, {self.types}, {self.unreachable}, {self.conditional_frame})"
8282

8383

84-
Assigns = defaultdict[Expression, list[tuple[Type, Optional[Type]]]]
84+
Assigns = defaultdict[Expression, list[tuple[Type, Type | None]]]
8585

8686

8787
class ConditionalTypeBinder:

mypy/cache.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
from __future__ import annotations
4949

5050
from collections.abc import Sequence
51-
from typing import Any, Final, Union
51+
from typing import Any, Final
5252
from typing_extensions import TypeAlias as _TypeAlias
5353

5454
from librt.internal import (
@@ -391,7 +391,7 @@ def write_str_opt_list(data: WriteBuffer, value: list[str | None]) -> None:
391391
write_str_opt(data, item)
392392

393393

394-
JsonValue: _TypeAlias = Union[None, int, str, bool, list["JsonValue"], dict[str, "JsonValue"]]
394+
JsonValue: _TypeAlias = None | int | str | bool | list["JsonValue"] | dict[str, "JsonValue"]
395395

396396

397397
def read_json_value(data: ReadBuffer) -> JsonValue:

mypy/checker.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,7 @@
66
from collections import defaultdict
77
from collections.abc import Iterable, Iterator, Mapping, Sequence, Set as AbstractSet
88
from contextlib import ExitStack, contextmanager
9-
from typing import (
10-
Callable,
11-
Final,
12-
Generic,
13-
Literal,
14-
NamedTuple,
15-
Optional,
16-
TypeVar,
17-
Union,
18-
cast,
19-
overload,
20-
)
9+
from typing import Callable, Final, Generic, Literal, NamedTuple, TypeVar, cast, overload
2110
from typing_extensions import TypeAlias as _TypeAlias, TypeGuard
2211

2312
import mypy.checkexpr
@@ -249,8 +238,8 @@
249238
# Maximum length of fixed tuple types inferred when narrowing from variadic tuples.
250239
MAX_PRECISE_TUPLE_SIZE: Final = 8
251240

252-
DeferredNodeType: _TypeAlias = Union[FuncDef, OverloadedFuncDef, Decorator]
253-
FineGrainedDeferredNodeType: _TypeAlias = Union[FuncDef, MypyFile, OverloadedFuncDef]
241+
DeferredNodeType: _TypeAlias = FuncDef | OverloadedFuncDef | Decorator
242+
FineGrainedDeferredNodeType: _TypeAlias = FuncDef | MypyFile | OverloadedFuncDef
254243

255244

256245
# A node which is postponed to be processed during the next pass.
@@ -283,7 +272,7 @@ class FineGrainedDeferredNode(NamedTuple):
283272
# (such as two references to the same variable). TODO: it would
284273
# probably be better to have the dict keyed by the nodes' literal_hash
285274
# field instead.
286-
TypeMap: _TypeAlias = Optional[dict[Expression, Type]]
275+
TypeMap: _TypeAlias = dict[Expression, Type] | None
287276

288277

289278
# Keeps track of partial types in a single scope. In fine-grained incremental

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, nullcontext
11-
from typing import Callable, ClassVar, Final, Optional, cast, overload
11+
from typing import Callable, ClassVar, Final, cast, overload
1212
from typing_extensions import TypeAlias as _TypeAlias, assert_never
1313

1414
import mypy.checker
@@ -217,7 +217,7 @@
217217
# Type of callback user for checking individual function arguments. See
218218
# check_args() below for details.
219219
ArgChecker: _TypeAlias = Callable[
220-
[Type, Type, ArgKind, Type, int, int, CallableType, Optional[Type], Context, Context], None
220+
[Type, Type, ArgKind, Type, int, int, CallableType, Type | None, Context, Context], None
221221
]
222222

223223
# Maximum nesting level for math union in overloads, setting this to large values

mypy/checkstrformat.py

Lines changed: 2 additions & 2 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 Callable, Final, Union, cast
17+
from typing import Callable, Final, cast
1818
from typing_extensions import TypeAlias as _TypeAlias
1919

2020
import mypy.errorcodes as codes
@@ -64,7 +64,7 @@
6464
get_proper_types,
6565
)
6666

67-
FormatStringExpr: _TypeAlias = Union[StrExpr, BytesExpr]
67+
FormatStringExpr: _TypeAlias = StrExpr | BytesExpr
6868
Checkers: _TypeAlias = tuple[Callable[[Expression], None], Callable[[Type], bool]]
6969
MatchMap: _TypeAlias = dict[tuple[int, int], Match[str]] # span -> match
7070

mypy/config_parser.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
import tomli as tomllib
1515

1616
from collections.abc import Mapping, MutableMapping, Sequence
17-
from typing import Any, Callable, Final, TextIO, Union
17+
from typing import Any, Callable, Final, TextIO
1818
from typing_extensions import Never, TypeAlias
1919

2020
from mypy import defaults
2121
from mypy.options import PER_MODULE_OPTIONS, Options
2222

23-
_CONFIG_VALUE_TYPES: TypeAlias = Union[
24-
str, bool, int, float, dict[str, str], list[str], tuple[int, int]
25-
]
23+
_CONFIG_VALUE_TYPES: TypeAlias = (
24+
str | bool | int | float | dict[str, str] | list[str] | tuple[int, int]
25+
)
2626
_INI_PARSER_CALLABLE: TypeAlias = Callable[[Any], _CONFIG_VALUE_TYPES]
2727

2828

mypy/constant_fold.py

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

66
from __future__ import annotations
77

8-
from typing import Final, Union
8+
from typing import Final
99

1010
from mypy.nodes import (
1111
ComplexExpr,
@@ -20,7 +20,7 @@
2020
)
2121

2222
# All possible result types of constant folding
23-
ConstantValue = Union[int, bool, float, complex, str]
23+
ConstantValue = int | bool | float | complex | str
2424
CONST_TYPES: Final = (int, bool, float, complex, str)
2525

2626

mypy/errors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from collections import defaultdict
77
from collections.abc import Iterable, Iterator
88
from itertools import chain
9-
from typing import Callable, Final, NoReturn, Optional, TextIO, TypeVar
9+
from typing import Callable, Final, NoReturn, TextIO, TypeVar
1010
from typing_extensions import Literal, Self, TypeAlias as _TypeAlias
1111

1212
from mypy import errorcodes as codes
@@ -155,7 +155,7 @@ def __init__(
155155

156156
# Type used internally to represent errors:
157157
# (path, line, column, end_line, end_column, severity, message, code)
158-
ErrorTuple: _TypeAlias = tuple[Optional[str], int, int, int, int, str, str, Optional[ErrorCode]]
158+
ErrorTuple: _TypeAlias = tuple[str | None, int, int, int, int, str, str, ErrorCode | None]
159159

160160

161161
class ErrorWatcher:

mypy/exportjson.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import argparse
1717
import json
1818
import sys
19-
from typing import Any, Union
19+
from typing import Any
2020
from typing_extensions import TypeAlias as _TypeAlias
2121

2222
from librt.internal import ReadBuffer
@@ -69,7 +69,7 @@
6969
get_proper_type,
7070
)
7171

72-
Json: _TypeAlias = Union[dict[str, Any], str]
72+
Json: _TypeAlias = dict[str, Any] | str
7373

7474

7575
class Config:

mypy/fastparse.py

Lines changed: 6 additions & 8 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, Literal, Optional, TypeVar, Union, cast, overload
7+
from typing import Any, Callable, Final, Literal, TypeVar, Union, cast, overload
88

99
from mypy import defaults, errorcodes as codes, message_registry
1010
from mypy.errors import Errors
@@ -714,7 +714,7 @@ def fix_function_overloads(self, stmts: list[Statement]) -> list[Statement]:
714714
current_overload.extend(if_block_with_overload.body[-1].items)
715715
else:
716716
current_overload.append(
717-
cast(Union[Decorator, FuncDef], if_block_with_overload.body[0])
717+
cast(Decorator | FuncDef, if_block_with_overload.body[0])
718718
)
719719
else:
720720
if last_if_stmt is not None:
@@ -760,7 +760,7 @@ def fix_function_overloads(self, stmts: list[Statement]) -> list[Statement]:
760760
cast(list[IfStmt], if_block_with_overload.body[:-1])
761761
)
762762
last_if_overload = cast(
763-
Union[Decorator, FuncDef, OverloadedFuncDef],
763+
Decorator | FuncDef | OverloadedFuncDef,
764764
if_block_with_overload.body[-1],
765765
)
766766
last_if_unknown_truth_value = if_unknown_truth_value
@@ -806,9 +806,7 @@ def _check_ifstmt_for_overloads(
806806
):
807807
return None
808808

809-
overload_name = cast(
810-
Union[Decorator, FuncDef, OverloadedFuncDef], stmt.body[0].body[-1]
811-
).name
809+
overload_name = cast(Decorator | FuncDef | OverloadedFuncDef, stmt.body[0].body[-1]).name
812810
if stmt.else_body is None:
813811
return overload_name
814812

@@ -991,7 +989,7 @@ def do_func_def(
991989
self.errors, line=lineno, override_column=n.col_offset
992990
).translate_expr_list(func_type_ast.argtypes)
993991
# Use a cast to work around `list` invariance
994-
arg_types = cast(list[Optional[Type]], translated_args)
992+
arg_types = cast(list[Type | None], translated_args)
995993
return_type = TypeConverter(self.errors, line=lineno).visit(func_type_ast.returns)
996994

997995
# add implicit self type
@@ -1646,7 +1644,7 @@ def visit_Call(self, n: Call) -> CallExpr:
16461644
self.visit(n.func),
16471645
arg_types,
16481646
arg_kinds,
1649-
cast("list[Optional[str]]", [None] * len(args)) + keyword_names,
1647+
cast("list[str | None]", [None] * len(args)) + keyword_names,
16501648
)
16511649
return self.set_line(e, n)
16521650

0 commit comments

Comments
 (0)