Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions mypy/binder.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from collections import defaultdict
from collections.abc import Iterator
from contextlib import contextmanager
from typing import NamedTuple, Optional, Union
from typing import NamedTuple
from typing_extensions import TypeAlias as _TypeAlias

from mypy.erasetype import remove_instance_last_known_values
Expand Down Expand Up @@ -39,7 +39,7 @@
)
from mypy.typevars import fill_typevars_with_any

BindableExpression: _TypeAlias = Union[IndexExpr, MemberExpr, NameExpr]
BindableExpression: _TypeAlias = IndexExpr | MemberExpr | NameExpr


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


Assigns = defaultdict[Expression, list[tuple[Type, Optional[Type]]]]
Assigns = defaultdict[Expression, list[tuple[Type, Type | None]]]


class ConditionalTypeBinder:
Expand Down
4 changes: 2 additions & 2 deletions mypy/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
from __future__ import annotations

from collections.abc import Sequence
from typing import Any, Final, Union
from typing import Any, Final
from typing_extensions import TypeAlias as _TypeAlias

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


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


def read_json_value(data: ReadBuffer) -> JsonValue:
Expand Down
19 changes: 4 additions & 15 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,7 @@
from collections import defaultdict
from collections.abc import Iterable, Iterator, Mapping, Sequence, Set as AbstractSet
from contextlib import ExitStack, contextmanager
from typing import (
Callable,
Final,
Generic,
Literal,
NamedTuple,
Optional,
TypeVar,
Union,
cast,
overload,
)
from typing import Callable, Final, Generic, Literal, NamedTuple, TypeVar, cast, overload
from typing_extensions import TypeAlias as _TypeAlias, TypeGuard

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

DeferredNodeType: _TypeAlias = Union[FuncDef, OverloadedFuncDef, Decorator]
FineGrainedDeferredNodeType: _TypeAlias = Union[FuncDef, MypyFile, OverloadedFuncDef]
DeferredNodeType: _TypeAlias = FuncDef | OverloadedFuncDef | Decorator
FineGrainedDeferredNodeType: _TypeAlias = FuncDef | MypyFile | OverloadedFuncDef


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


# Keeps track of partial types in a single scope. In fine-grained incremental
Expand Down
4 changes: 2 additions & 2 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from collections import defaultdict
from collections.abc import Iterable, Iterator, Sequence
from contextlib import contextmanager, nullcontext
from typing import Callable, ClassVar, Final, Optional, cast, overload
from typing import Callable, ClassVar, Final, cast, overload
from typing_extensions import TypeAlias as _TypeAlias, assert_never

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

# Maximum nesting level for math union in overloads, setting this to large values
Expand Down
4 changes: 2 additions & 2 deletions mypy/checkstrformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import re
from re import Match, Pattern
from typing import Callable, Final, Union, cast
from typing import Callable, Final, cast
from typing_extensions import TypeAlias as _TypeAlias

import mypy.errorcodes as codes
Expand Down Expand Up @@ -64,7 +64,7 @@
get_proper_types,
)

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

Expand Down
8 changes: 4 additions & 4 deletions mypy/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
import tomli as tomllib

from collections.abc import Mapping, MutableMapping, Sequence
from typing import Any, Callable, Final, TextIO, Union
from typing import Any, Callable, Final, TextIO
from typing_extensions import Never, TypeAlias

from mypy import defaults
from mypy.options import PER_MODULE_OPTIONS, Options

_CONFIG_VALUE_TYPES: TypeAlias = Union[
str, bool, int, float, dict[str, str], list[str], tuple[int, int]
]
_CONFIG_VALUE_TYPES: TypeAlias = (
str | bool | int | float | dict[str, str] | list[str] | tuple[int, int]
)
_INI_PARSER_CALLABLE: TypeAlias = Callable[[Any], _CONFIG_VALUE_TYPES]


Expand Down
4 changes: 2 additions & 2 deletions mypy/constant_fold.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from __future__ import annotations

from typing import Final, Union
from typing import Final

from mypy.nodes import (
ComplexExpr,
Expand All @@ -20,7 +20,7 @@
)

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


Expand Down
4 changes: 2 additions & 2 deletions mypy/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from collections import defaultdict
from collections.abc import Iterable, Iterator
from itertools import chain
from typing import Callable, Final, NoReturn, Optional, TextIO, TypeVar
from typing import Callable, Final, NoReturn, TextIO, TypeVar
from typing_extensions import Literal, Self, TypeAlias as _TypeAlias

from mypy import errorcodes as codes
Expand Down Expand Up @@ -155,7 +155,7 @@ def __init__(

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


class ErrorWatcher:
Expand Down
4 changes: 2 additions & 2 deletions mypy/exportjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import argparse
import json
import sys
from typing import Any, Union
from typing import Any
from typing_extensions import TypeAlias as _TypeAlias

from librt.internal import ReadBuffer
Expand Down Expand Up @@ -69,7 +69,7 @@
get_proper_type,
)

Json: _TypeAlias = Union[dict[str, Any], str]
Json: _TypeAlias = dict[str, Any] | str


class Config:
Expand Down
14 changes: 6 additions & 8 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
import warnings
from collections.abc import Sequence
from typing import Any, Callable, Final, Literal, Optional, TypeVar, Union, cast, overload
from typing import Any, Callable, Final, Literal, TypeVar, Union, cast, overload

from mypy import defaults, errorcodes as codes, message_registry
from mypy.errors import Errors
Expand Down Expand Up @@ -714,7 +714,7 @@ def fix_function_overloads(self, stmts: list[Statement]) -> list[Statement]:
current_overload.extend(if_block_with_overload.body[-1].items)
else:
current_overload.append(
cast(Union[Decorator, FuncDef], if_block_with_overload.body[0])
cast(Decorator | FuncDef, if_block_with_overload.body[0])
)
else:
if last_if_stmt is not None:
Expand Down Expand Up @@ -760,7 +760,7 @@ def fix_function_overloads(self, stmts: list[Statement]) -> list[Statement]:
cast(list[IfStmt], if_block_with_overload.body[:-1])
)
last_if_overload = cast(
Union[Decorator, FuncDef, OverloadedFuncDef],
Decorator | FuncDef | OverloadedFuncDef,
if_block_with_overload.body[-1],
)
last_if_unknown_truth_value = if_unknown_truth_value
Expand Down Expand Up @@ -806,9 +806,7 @@ def _check_ifstmt_for_overloads(
):
return None

overload_name = cast(
Union[Decorator, FuncDef, OverloadedFuncDef], stmt.body[0].body[-1]
).name
overload_name = cast(Decorator | FuncDef | OverloadedFuncDef, stmt.body[0].body[-1]).name
if stmt.else_body is None:
return overload_name

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

# add implicit self type
Expand Down Expand Up @@ -1646,7 +1644,7 @@ def visit_Call(self, n: Call) -> CallExpr:
self.visit(n.func),
arg_types,
arg_kinds,
cast("list[Optional[str]]", [None] * len(args)) + keyword_names,
cast("list[str | None]", [None] * len(args)) + keyword_names,
)
return self.set_line(e, n)

Expand Down
4 changes: 2 additions & 2 deletions mypy/literals.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from collections.abc import Iterable
from typing import Any, Final, Optional
from typing import Any, Final
from typing_extensions import TypeAlias as _TypeAlias

from mypy.nodes import (
Expand Down Expand Up @@ -164,7 +164,7 @@ def extract_var_from_literal_hash(key: Key) -> Var | None:
return None


class _Hasher(ExpressionVisitor[Optional[Key]]):
class _Hasher(ExpressionVisitor[Key | None]):
def visit_int_expr(self, e: IntExpr) -> Key:
return ("Literal", e.value)

Expand Down
6 changes: 3 additions & 3 deletions mypy/modulefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import subprocess
import sys
from enum import Enum, unique
from typing import Final, Optional, Union
from typing import Final
from typing_extensions import TypeAlias as _TypeAlias

from pathspec import PathSpec
Expand Down Expand Up @@ -60,7 +60,7 @@ def asdict(self) -> dict[str, tuple[str, ...]]:
PackageDirs = list[OnePackageDir]

# Minimum and maximum Python versions for modules in stdlib as (major, minor)
StdlibVersions: _TypeAlias = dict[str, tuple[tuple[int, int], Optional[tuple[int, int]]]]
StdlibVersions: _TypeAlias = dict[str, tuple[tuple[int, int], tuple[int, int] | None]]

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

Expand Down Expand Up @@ -118,7 +118,7 @@ def error_message_templates(self, daemon: bool) -> tuple[str, list[str]]:

# If we found the module, returns the path to the module as a str.
# Otherwise, returns the reason why the module wasn't found.
ModuleSearchResult = Union[str, ModuleNotFoundReason]
ModuleSearchResult = str | ModuleNotFoundReason


class BuildSource:
Expand Down
4 changes: 2 additions & 2 deletions mypy/plugins/singledispatch.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from collections.abc import Sequence
from typing import NamedTuple, TypeVar, Union
from typing import NamedTuple, TypeVar
from typing_extensions import TypeAlias as _TypeAlias

from mypy.messages import format_type
Expand Down Expand Up @@ -67,7 +67,7 @@ def make_fake_register_class_instance(
return Instance(info, type_args)


PluginContext: _TypeAlias = Union[FunctionContext, MethodContext]
PluginContext: _TypeAlias = FunctionContext | MethodContext


def fail(ctx: PluginContext, msg: str, context: Context | None) -> None:
Expand Down
3 changes: 1 addition & 2 deletions mypy/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@

from collections.abc import Iterator
from contextlib import contextmanager, nullcontext
from typing import Optional
from typing_extensions import TypeAlias as _TypeAlias

from mypy.nodes import FuncBase, TypeInfo

SavedScope: _TypeAlias = tuple[str, Optional[TypeInfo], Optional[FuncBase]]
SavedScope: _TypeAlias = tuple[str, TypeInfo | None, FuncBase | None]


class Scope:
Expand Down
6 changes: 3 additions & 3 deletions mypy/semanal_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from collections.abc import Iterator
from contextlib import nullcontext
from itertools import groupby
from typing import TYPE_CHECKING, Callable, Final, Optional, Union
from typing import TYPE_CHECKING, Callable, Final
from typing_extensions import TypeAlias as _TypeAlias

import mypy.build
Expand Down Expand Up @@ -358,12 +358,12 @@ def process_top_level_function(


TargetInfo: _TypeAlias = tuple[
str, Union[MypyFile, FuncDef, OverloadedFuncDef, Decorator], Optional[TypeInfo]
str, MypyFile | FuncDef | OverloadedFuncDef | Decorator, TypeInfo | None
]

# Same as above but includes module as first item.
FullTargetInfo: _TypeAlias = tuple[
str, str, Union[MypyFile, FuncDef, OverloadedFuncDef, Decorator], Optional[TypeInfo]
str, str, MypyFile | FuncDef | OverloadedFuncDef | Decorator, TypeInfo | None
]


Expand Down
2 changes: 1 addition & 1 deletion mypy/server/astdiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class level -- these are handled at attribute level (say, 'mod.Cls.method'
# For example, the snapshot of the 'int' type is ('Instance', 'builtins.int', ()).

# Type snapshots are strict, they must be hashable and ordered (e.g. for Unions).
Primitive: _TypeAlias = Union[str, float, int, bool] # float is for Literal[3.14] support.
Primitive: _TypeAlias = str | float | int | bool # float is for Literal[3.14] support.
SnapshotItem: _TypeAlias = tuple[Union[Primitive, "SnapshotItem"], ...]

# Symbol snapshots can be more lenient.
Expand Down
4 changes: 2 additions & 2 deletions mypy/server/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
import sys
import time
from collections.abc import Sequence
from typing import Callable, Final, NamedTuple, Union
from typing import Callable, Final, NamedTuple
from typing_extensions import TypeAlias as _TypeAlias

from mypy.build import (
Expand Down Expand Up @@ -555,7 +555,7 @@ class BlockedUpdate(NamedTuple):
messages: list[str]


UpdateResult: _TypeAlias = Union[NormalUpdate, BlockedUpdate]
UpdateResult: _TypeAlias = NormalUpdate | BlockedUpdate


def update_module_isolated(
Expand Down
2 changes: 1 addition & 1 deletion mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __repr__(self) -> str:
MISSING: Final = Missing()

T = TypeVar("T")
MaybeMissing: typing_extensions.TypeAlias = Union[T, Missing]
MaybeMissing: typing_extensions.TypeAlias = T | Missing


class Unrepresentable:
Expand Down
Loading