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
9 changes: 5 additions & 4 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 Literal, NamedTuple, TypeAlias as _TypeAlias
from typing import Final, Literal, TypeAlias as _TypeAlias

from mypy.erasetype import remove_instance_last_known_values
from mypy.literals import Key, extract_var_from_literal_hash, literal, literal_hash, subkeys
Expand Down Expand Up @@ -42,9 +42,10 @@
BindableExpression: _TypeAlias = IndexExpr | MemberExpr | NameExpr


class CurrentType(NamedTuple):
type: Type
from_assignment: bool
class CurrentType:
def __init__(self, type: Type, from_assignment: bool) -> None:
self.type: Final = type
self.from_assignment: Final = from_assignment


class Frame:
Expand Down
22 changes: 15 additions & 7 deletions mypy/build_worker/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import platform
import sys
import time
from typing import NamedTuple
from typing import Final

from librt.internal import ReadBuffer, read_tag

Expand Down Expand Up @@ -65,12 +65,20 @@
CONNECTION_NAME = "build_worker"


class ServerContext(NamedTuple):
options: Options
disable_error_code: list[str]
enable_error_code: list[str]
errors: Errors
fscache: FileSystemCache
class ServerContext:
def __init__(
self,
options: Options,
disable_error_code: list[str],
enable_error_code: list[str],
errors: Errors,
fscache: FileSystemCache,
) -> None:
self.options: Final = options
self.disable_error_code: Final = disable_error_code
self.enable_error_code: Final = enable_error_code
self.errors: Final = errors
self.fscache: Final = fscache


def main(argv: list[str]) -> None:
Expand Down
15 changes: 8 additions & 7 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,11 @@ class FineGrainedDeferredNode(NamedTuple):
# Keeps track of partial types in a single scope. In fine-grained incremental
# mode partial types initially defined at the top level cannot be completed in
# a function, and we use the 'is_function' attribute to enforce this.
class PartialTypeScope(NamedTuple):
map: dict[Var, Context]
is_function: bool
is_local: bool
class PartialTypeScope:
def __init__(self, map: dict[Var, Context], is_function: bool, is_local: bool) -> None:
self.map: Final = map
self.is_function: Final = is_function
self.is_local: Final = is_local


class LocalTypeMap:
Expand Down Expand Up @@ -1604,14 +1605,14 @@ def check_func_def(
else:
msg = message_registry.MISSING_RETURN_STATEMENT
if body_is_trivial:
msg = msg._replace(code=codes.EMPTY_BODY)
msg = ErrorMessage(msg.value, code=codes.EMPTY_BODY)
self.fail(msg, defn)
if may_be_abstract:
self.note(message_registry.EMPTY_BODY_ABSTRACT, defn)
else:
msg = message_registry.INCOMPATIBLE_RETURN_VALUE_TYPE
if body_is_trivial:
msg = msg._replace(code=codes.EMPTY_BODY)
msg = ErrorMessage(msg.value, code=codes.EMPTY_BODY)
# similar to code in check_return_stmt
if (
not self.check_subtype(
Expand Down Expand Up @@ -7857,7 +7858,7 @@ def enter_partial_types(
self.options.check_untyped_defs and self.dynamic_funcs and self.dynamic_funcs[-1]
)

partial_types, _, _ = self.partial_types.pop()
partial_types = self.partial_types.pop().map
if not self.current_node_deferred:
for var, context in partial_types.items():
if isinstance(var.type, PartialType) and var.type.type is None and not permissive:
Expand Down
9 changes: 5 additions & 4 deletions mypy/checker_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from abc import abstractmethod
from collections.abc import Iterator, Sequence, Set as AbstractSet
from contextlib import contextmanager
from typing import NamedTuple, overload
from typing import Final, overload

from mypy_extensions import trait

Expand Down Expand Up @@ -42,9 +42,10 @@

# An object that represents either a precise type or a type with an upper bound;
# it is important for correct type inference with isinstance.
class TypeRange(NamedTuple):
item: Type
is_upper_bound: bool # False => precise type
class TypeRange:
def __init__(self, item: Type, is_upper_bound: bool) -> None:
self.item: Final = item
self.is_upper_bound: Final = is_upper_bound # False => precise type


@trait
Expand Down
9 changes: 5 additions & 4 deletions mypy/infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import annotations

from collections.abc import Sequence
from typing import NamedTuple
from typing import Final

from mypy.constraints import (
SUBTYPE_OF,
Expand All @@ -16,7 +16,7 @@
from mypy.types import CallableType, Instance, Type, TypeVarLikeType


class ArgumentInferContext(NamedTuple):
class ArgumentInferContext:
"""Type argument inference context.

We need this because we pass around ``Mapping`` and ``Iterable`` types.
Expand All @@ -26,8 +26,9 @@ class ArgumentInferContext(NamedTuple):
https://github.com/python/mypy/issues/11144
"""

mapping_type: Instance
iterable_type: Instance
def __init__(self, mapping_type: Instance, iterable_type: Instance) -> None:
self.mapping_type: Final = mapping_type
self.iterable_type: Final = iterable_type


def infer_function_type_arguments(
Expand Down
9 changes: 5 additions & 4 deletions mypy/message_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@

from __future__ import annotations

from typing import Final, NamedTuple
from typing import Final

from mypy import errorcodes as codes
from mypy.errorcodes import ErrorCode


class ErrorMessage(NamedTuple):
value: str
code: ErrorCode | None = None
class ErrorMessage:
def __init__(self, value: str, code: ErrorCode | None = None) -> None:
self.value: Final = value
self.code: Final = code

def format(self, *args: object, **kwargs: object) -> ErrorMessage:
return ErrorMessage(self.value.format(*args, **kwargs), code=self.code)
Expand Down
9 changes: 5 additions & 4 deletions mypy/plugins/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import annotations

from typing import Final, NamedTuple
from typing import Final

import mypy.checker
import mypy.plugin
Expand Down Expand Up @@ -43,9 +43,10 @@
PARTIAL: Final = "functools.partial"


class _MethodInfo(NamedTuple):
is_static: bool
type: CallableType
class _MethodInfo:
def __init__(self, is_static: bool, type: CallableType) -> None:
self.is_static: Final = is_static
self.type: Final = type


def functools_total_ordering_maker_callback(
Expand Down
Loading