Skip to content

Use more Final and TypeAlias types #13433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2022
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
5 changes: 3 additions & 2 deletions misc/analyze_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
import os.path
from collections import Counter
from typing import Any, Dict, Iterable, List, Optional
from typing_extensions import Final, TypeAlias as _TypeAlias

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

JsonDict = Dict[str, Any]
JsonDict: _TypeAlias = Dict[str, Any]


class CacheData:
Expand Down
11 changes: 6 additions & 5 deletions misc/incremental_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@
import time
from argparse import ArgumentParser, Namespace, RawDescriptionHelpFormatter
from typing import Any, Dict, List, Optional, Tuple
from typing_extensions import TypeAlias as _TypeAlias

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

JsonDict = Dict[str, Any]
JsonDict: _TypeAlias = Dict[str, Any]


def print_offset(text: str, indent_length: int = 4) -> None:
Expand Down
8 changes: 4 additions & 4 deletions mypy/dmypy_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import traceback
from contextlib import redirect_stderr, redirect_stdout
from typing import AbstractSet, Any, Callable, Dict, List, Optional, Sequence, Set, Tuple
from typing_extensions import Final
from typing_extensions import Final, TypeAlias as _TypeAlias

import mypy.build
import mypy.errors
Expand Down Expand Up @@ -161,9 +161,9 @@ def ignore_suppressed_imports(module: str) -> bool:
return module.startswith("encodings.")


ModulePathPair = Tuple[str, str]
ModulePathPairs = List[ModulePathPair]
ChangesAndRemovals = Tuple[ModulePathPairs, ModulePathPairs]
ModulePathPair: _TypeAlias = Tuple[str, str]
ModulePathPairs: _TypeAlias = List[ModulePathPair]
ChangesAndRemovals: _TypeAlias = Tuple[ModulePathPairs, ModulePathPairs]


class Server:
Expand Down
6 changes: 4 additions & 2 deletions mypy/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import traceback
from collections import defaultdict
from typing import Callable, Dict, List, NoReturn, Optional, Set, TextIO, Tuple, TypeVar, Union
from typing_extensions import Final, Literal
from typing_extensions import Final, Literal, TypeAlias as _TypeAlias

from mypy import errorcodes as codes
from mypy.errorcodes import IMPORT, ErrorCode
Expand Down Expand Up @@ -123,7 +123,9 @@ def __init__(

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


class ErrorWatcher:
Expand Down
4 changes: 2 additions & 2 deletions mypy/literals.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import Any, Iterable, Optional, Tuple, Union
from typing_extensions import Final
from typing_extensions import Final, TypeAlias as _TypeAlias

from mypy.nodes import (
LITERAL_NO,
Expand Down Expand Up @@ -123,7 +123,7 @@ def literal(e: Expression) -> int:
return LITERAL_NO


Key = Tuple[Any, ...]
Key: _TypeAlias = Tuple[Any, ...]


def subkeys(key: Key) -> Iterable[Key]:
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 typing import List, NamedTuple, Optional, Sequence, TypeVar, Union
from typing_extensions import Final
from typing_extensions import Final, TypeAlias as _TypeAlias

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


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


def fail(ctx: PluginContext, msg: str, context: Optional[Context]) -> None:
Expand Down
4 changes: 3 additions & 1 deletion mypy/semanal_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,9 @@ def process_top_level_function(
analyzer.saved_locals.clear()


TargetInfo = Tuple[str, Union[MypyFile, FuncDef, OverloadedFuncDef, Decorator], Optional[TypeInfo]]
TargetInfo: _TypeAlias = Tuple[
str, Union[MypyFile, FuncDef, OverloadedFuncDef, Decorator], Optional[TypeInfo]
]


def get_all_leaf_targets(file: MypyFile) -> List[TargetInfo]:
Expand Down
3 changes: 2 additions & 1 deletion mypy/server/astdiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class level -- these are handled at attribute level (say, 'mod.Cls.method'
from __future__ import annotations

from typing import Dict, Optional, Sequence, Set, Tuple, Union
from typing_extensions import TypeAlias as _TypeAlias

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


def compare_symbol_table_snapshots(
Expand Down
3 changes: 2 additions & 1 deletion mypy/server/aststrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

from contextlib import contextmanager, nullcontext
from typing import Dict, Iterator, Optional, Tuple, Union
from typing_extensions import TypeAlias as _TypeAlias

from mypy.nodes import (
CLASSDEF_NO_INFO,
Expand Down Expand Up @@ -66,7 +67,7 @@
from mypy.types import CallableType
from mypy.typestate import TypeState

SavedAttributes = Dict[Tuple[ClassDef, str], SymbolTableNode]
SavedAttributes: _TypeAlias = Dict[Tuple[ClassDef, str], SymbolTableNode]


def strip_target(
Expand Down
4 changes: 2 additions & 2 deletions mypy/server/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
import sys
import time
from typing import Callable, Dict, List, NamedTuple, Optional, Sequence, Set, Tuple, Union
from typing_extensions import Final
from typing_extensions import Final, TypeAlias as _TypeAlias

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


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


def update_module_isolated(
Expand Down
4 changes: 2 additions & 2 deletions mypy/stubdoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
Sequence,
Tuple,
)
from typing_extensions import Final
from typing_extensions import Final, TypeAlias as _TypeAlias

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


_TYPE_RE: Final = re.compile(r"^[a-zA-Z_][\w\[\], ]*(\.[a-zA-Z_][\w\[\], ]*)*$")
Expand Down
4 changes: 2 additions & 2 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1179,7 +1179,7 @@ def verify_typealias(
# ====================


IGNORED_MODULE_DUNDERS = frozenset(
IGNORED_MODULE_DUNDERS: typing_extensions.Final = frozenset(
{
"__file__",
"__doc__",
Expand All @@ -1199,7 +1199,7 @@ def verify_typealias(
}
)

IGNORABLE_CLASS_DUNDERS = frozenset(
IGNORABLE_CLASS_DUNDERS: typing_extensions.Final = frozenset(
{
# Special attributes
"__dict__",
Expand Down
4 changes: 2 additions & 2 deletions mypy/test/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import tempfile
from abc import abstractmethod
from typing import Any, Dict, Iterator, List, NamedTuple, Optional, Pattern, Set, Tuple, Union
from typing_extensions import Final
from typing_extensions import Final, TypeAlias as _TypeAlias

import pytest

Expand All @@ -38,7 +38,7 @@ class DeleteFile(NamedTuple):
path: str


FileOperation = Union[UpdateFile, DeleteFile]
FileOperation: _TypeAlias = Union[UpdateFile, DeleteFile]


def parse_test_case(case: "DataDrivenTestCase") -> None:
Expand Down