Skip to content

Migrate remaining failure messages to mypy.messages_registry (ircheck.py) #15346

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

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
14f1b5c
Migrate remaining failure messages to mypy.messages_registry (ircheck…
SaintLeonar May 21, 2023
0c0a446
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 1, 2023
41ea519
import ErrorMessage
SaintLeonar Jun 1, 2023
a7a8944
Merge branch 'master' of https://github.com/SaintLeonar/mypy
SaintLeonar Jun 1, 2023
8136760
Change desc parameter type in FnError
SaintLeonar Jun 1, 2023
04900ca
Add consts in test_ircheck.py
SaintLeonar Jun 1, 2023
2b5a69f
Merge branch 'master' into master
SaintLeonar Jun 1, 2023
4caf8c5
Migrate remaining failure messages to mypy.messages_registry (semanal…
SaintLeonar Jun 5, 2023
79f8979
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 5, 2023
9b4d9ba
Migrate failure messages (semanal_newtype.py)
SaintLeonar Jun 5, 2023
69a23c5
Revert "[pre-commit.ci] auto fixes from pre-commit.com hooks"
SaintLeonar Jun 5, 2023
9b20286
Revert "Migrate remaining failure messages to mypy.messages_registry …
SaintLeonar Jun 5, 2023
3333916
Update .fail signature to accept ErrorMessage
SaintLeonar Jun 5, 2023
03c4721
Update function signatures to accept ErrorMessage
SaintLeonar Jun 5, 2023
ead09d4
Removing some conflicts
SaintLeonar Jun 5, 2023
8c41f7c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 5, 2023
57fa031
Removing other conflicts
SaintLeonar Jun 5, 2023
21adbc8
Merge branch 'master' of https://github.com/SaintLeonar/mypy
SaintLeonar Jun 5, 2023
c894d7a
Removing conflicts
SaintLeonar Jun 5, 2023
915db99
Revert "Removing other conflicts"
SaintLeonar Jun 5, 2023
c689917
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 5, 2023
cde4b8f
Removing conflicts
SaintLeonar Jun 5, 2023
c8ba4ae
Merge branch 'master' of https://github.com/SaintLeonar/mypy
SaintLeonar Jun 5, 2023
c1aa9c6
Merge pull request #1 from SaintLeonar/Semanal_NewType_errMessages
SaintLeonar Jun 5, 2023
e556494
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 5, 2023
9221468
Revert "Merge pull request #1 from SaintLeonar/Semanal_NewType_errMes…
SaintLeonar Jun 5, 2023
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
2 changes: 1 addition & 1 deletion docs/source/config_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ section of the command line docs.

.. confval:: no_site_packages

:type: boolean
:type: bool
:default: False

Disables using type information in installed packages (see :pep:`561`).
Expand Down
35 changes: 35 additions & 0 deletions mypy/message_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ def with_additional_msg(self, info: str) -> ErrorMessage:
INVALID_TYPE_RAW_ENUM_VALUE: Final = ErrorMessage(
"Invalid type: try using Literal[{}.{}] instead?", codes.VALID_TYPE
)
INVALID_TYPE_FROZENSET_LITERAL: Final = ErrorMessage(
"Invalid type for item of frozenset literal: {})"
)
INVALID_TYPE_TUPLE_LITERAL: Final = ErrorMessage("Invalid type for item of tuple literal: {}}")
FLOAT_NOT_EXPECTED: Final = ErrorMessage("Float not expected")
FLOAT_EXPECTED: Final = ErrorMessage("Float expected (actual type is {})")

# Type checker error message constants
NO_RETURN_VALUE_EXPECTED: Final = ErrorMessage("No return value expected", codes.RETURN_VALUE)
Expand Down Expand Up @@ -162,6 +168,13 @@ def with_additional_msg(self, info: str) -> ErrorMessage:
)
NOT_CALLABLE: Final = "{} not callable"
TYPE_MUST_BE_USED: Final = "Value of type {} must be used"
TYPES_NOT_COMPATIBLE: Final = ErrorMessage("{} and {} are not compatible")
CANNOT_COERSE_SRC_DEST: Final = ErrorMessage("Cannot coerce source type {} to dest type {}")
INVALID_CONTROL_OPERATION_TARGET: Final = ErrorMessage("Invalid control operation target: {}")
INCORRECT_NUMBER_ARGS: Final = ErrorMessage("Incorrect number of args for method call.")
INVALID_LITERAL_VALUE_TYPE: Final = ErrorMessage(
"Invalid literal value for type: value has type {}, but op has type {}"
)

# Generic
GENERIC_INSTANCE_VAR_CLASS_ACCESS: Final = (
Expand Down Expand Up @@ -316,3 +329,25 @@ def with_additional_msg(self, info: str) -> ErrorMessage:
ARG_NAME_EXPECTED_STRING_LITERAL: Final = ErrorMessage(
"Expected string literal for argument name, got {}", codes.SYNTAX
)
# Semanal newtype
NEWTYPE_PROTOCOL_CLASSES: Final = ErrorMessage("NewType cannot be used with protocol classes")
ARG_1_NEWTYPE_LITERAL: Final = ErrorMessage("Argument 1 to NewType(...) must be a string literal")
ARG_2_NEWTYPE_SUBCLASSABLE: Final = ErrorMessage(
"Argument 2 to NewType(...) must be subclassable (got {})"
)
ARG_2_NEWTYPE_INVALID_TYPE: Final = ErrorMessage("Argument 2 to NewType(...) must be a valid type")
CANNOT_DECLARE_TYPE: Final = ErrorMessage("Cannot declare the type of a NewType declaration")
CANNOT_REDEFINE_NEWTYPE: Final = ErrorMessage("Cannot redefine {} as a NewType")
EXCEPT_TWO_POSITIONAL_ARGS: Final = ErrorMessage(
"NewType(...) expects exactly two positional arguments"
)
MATCH_ERROR_STR_ARG1_VAR_NAME: Final = ErrorMessage(
"String argument 1 {} to NewType(...) does not match variable name {}"
)
# Blocks
BLOCK_NOT_TERMINATED: Final = ErrorMessage("Block not terminated")
OPERATIONS_AFTER_CONTROL: Final = ErrorMessage("Block has operations after control op")
# Op
FUNC_DUPLICATE_OP: Final = ErrorMessage("Func has a duplicate op")
INVALID_OP_REFERENCE_TYPE: Final = ErrorMessage("Invalid op reference to op of type {}")
INVALID_OP_REFERENCE_REGISTER: Final = ErrorMessage("Invalid op reference to register {}")
19 changes: 4 additions & 15 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2189,10 +2189,6 @@ def configure_base_classes(
if not self.verify_base_classes(defn):
self.set_dummy_mro(defn.info)
return
if not self.verify_duplicate_base_classes(defn):
# We don't want to block the typechecking process,
# so, we just insert `Any` as the base class and show an error.
self.set_any_mro(defn.info)
self.calculate_class_mro(defn, self.object_type)

def configure_tuple_base_class(self, defn: ClassDef, base: TupleType) -> Instance:
Expand Down Expand Up @@ -2222,11 +2218,6 @@ def set_dummy_mro(self, info: TypeInfo) -> None:
info.mro = [info, self.object_type().type]
info.bad_mro = True

def set_any_mro(self, info: TypeInfo) -> None:
# Give it an MRO consisting direct `Any` subclass.
info.fallback_to_any = True
info.mro = [info, self.object_type().type]

def calculate_class_mro(
self, defn: ClassDef, obj_type: Callable[[], Instance] | None = None
) -> None:
Expand Down Expand Up @@ -2307,13 +2298,11 @@ def verify_base_classes(self, defn: ClassDef) -> bool:
if self.is_base_class(info, baseinfo):
self.fail("Cycle in inheritance hierarchy", defn)
cycle = True
return not cycle

def verify_duplicate_base_classes(self, defn: ClassDef) -> bool:
dup = find_duplicate(defn.info.direct_base_classes())
dup = find_duplicate(info.direct_base_classes())
if dup:
self.fail(f'Duplicate base class "{dup.name}"', defn)
return not dup
self.fail(f'Duplicate base class "{dup.name}"', defn, blocker=True)
return False
return not cycle

def is_base_class(self, t: TypeInfo, s: TypeInfo) -> bool:
"""Determine if t is a base class of s (but do not use mro)."""
Expand Down
50 changes: 29 additions & 21 deletions mypyc/analysis/ircheck.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Utilities for checking that internal ir is valid and consistent."""
from __future__ import annotations

from mypy import message_registry
from mypy.message_registry import ErrorMessage
from mypyc.ir.func_ir import FUNC_STATICMETHOD, FuncIR
from mypyc.ir.ops import (
Assign,
Expand Down Expand Up @@ -69,7 +71,7 @@


class FnError:
def __init__(self, source: Op | BasicBlock, desc: str) -> None:
def __init__(self, source: Op | BasicBlock, desc: ErrorMessage) -> None:
self.source = source
self.desc = desc

Expand All @@ -91,14 +93,17 @@ def check_func_ir(fn: FuncIR) -> list[FnError]:
for block in fn.blocks:
if not block.terminated:
errors.append(
FnError(source=block.ops[-1] if block.ops else block, desc="Block not terminated")
FnError(
source=block.ops[-1] if block.ops else block,
desc=message_registry.BLOCK_NOT_TERMINATED,
)
)
for op in block.ops[:-1]:
if isinstance(op, ControlOp):
errors.append(FnError(source=op, desc="Block has operations after control op"))
errors.append(FnError(source=op, desc=message_registry.OPERATIONS_AFTER_CONTROL))

if op in op_set:
errors.append(FnError(source=op, desc="Func has a duplicate op"))
errors.append(FnError(source=op, desc=message_registry.FUNC_DUPLICATE_OP))
op_set.add(op)

errors.extend(check_op_sources_valid(fn))
Expand All @@ -122,7 +127,7 @@ def assert_func_ir_valid(fn: FuncIR) -> None:
if errors:
raise IrCheckException(
"Internal error: Generated invalid IR: \n"
+ "\n".join(format_func(fn, [(e.source, e.desc) for e in errors]))
+ "\n".join(format_func(fn, [(e.source, e.desc.value) for e in errors]))
)


Expand Down Expand Up @@ -152,14 +157,19 @@ def check_op_sources_valid(fn: FuncIR) -> list[FnError]:
errors.append(
FnError(
source=op,
desc=f"Invalid op reference to op of type {type(source).__name__}",
desc=message_registry.INVALID_OP_REFERENCE_TYPE.format(
type(source).__name__
),
)
)
elif isinstance(source, Register):
if source not in valid_registers:
errors.append(
FnError(
source=op, desc=f"Invalid op reference to register {source.name!r}"
source=op,
desc=message_registry.INVALID_OP_REFERENCE_REGISTER.format(
source.name
),
)
)

Expand Down Expand Up @@ -208,31 +218,31 @@ def __init__(self, parent_fn: FuncIR) -> None:
self.parent_fn = parent_fn
self.errors: list[FnError] = []

def fail(self, source: Op, desc: str) -> None:
def fail(self, source: Op, desc: ErrorMessage) -> None:
self.errors.append(FnError(source=source, desc=desc))

def check_control_op_targets(self, op: ControlOp) -> None:
for target in op.targets():
if target not in self.parent_fn.blocks:
self.fail(source=op, desc=f"Invalid control operation target: {target.label}")
self.fail(
op, message_registry.INVALID_CONTROL_OPERATION_TARGET.format(target.label)
)

def check_type_coercion(self, op: Op, src: RType, dest: RType) -> None:
if not can_coerce_to(src, dest):
self.fail(
source=op, desc=f"Cannot coerce source type {src.name} to dest type {dest.name}"
)
self.fail(op, message_registry.CANNOT_COERSE_SRC_DEST.format(src.name, dest.name))

def check_compatibility(self, op: Op, t: RType, s: RType) -> None:
if not can_coerce_to(t, s) or not can_coerce_to(s, t):
self.fail(source=op, desc=f"{t.name} and {s.name} are not compatible")
self.fail(op, message_registry.TYPES_NOT_COMPATIBLE.format(t.name, s.name))

def expect_float(self, op: Op, v: Value) -> None:
if not is_float_rprimitive(v.type):
self.fail(op, f"Float expected (actual type is {v.type})")
self.fail(op, message_registry.FLOAT_EXPECTED.format(v.type))

def expect_non_float(self, op: Op, v: Value) -> None:
if is_float_rprimitive(v.type):
self.fail(op, "Float not expected")
self.fail(op, message_registry.FLOAT_NOT_EXPECTED)

def visit_goto(self, op: Goto) -> None:
self.check_control_op_targets(op)
Expand Down Expand Up @@ -265,7 +275,7 @@ def visit_load_error_value(self, op: LoadErrorValue) -> None:
def check_tuple_items_valid_literals(self, op: LoadLiteral, t: tuple[object, ...]) -> None:
for x in t:
if x is not None and not isinstance(x, (str, bytes, bool, int, float, complex, tuple)):
self.fail(op, f"Invalid type for item of tuple literal: {type(x)})")
self.fail(op, message_registry.INVALID_TYPE_TUPLE_LITERAL.format(type(x)))
if isinstance(x, tuple):
self.check_tuple_items_valid_literals(op, x)

Expand All @@ -276,7 +286,7 @@ def check_frozenset_items_valid_literals(self, op: LoadLiteral, s: frozenset[obj
elif isinstance(x, tuple):
self.check_tuple_items_valid_literals(op, x)
else:
self.fail(op, f"Invalid type for item of frozenset literal: {type(x)})")
self.fail(op, message_registry.INVALID_TYPE_FROZENSET_LITERAL.format(type(x)))

def visit_load_literal(self, op: LoadLiteral) -> None:
expected_type = None
Expand Down Expand Up @@ -307,9 +317,7 @@ def visit_load_literal(self, op: LoadLiteral) -> None:

if op.type.name not in [expected_type, "builtins.object"]:
self.fail(
op,
f"Invalid literal value for type: value has "
f"type {expected_type}, but op has type {op.type.name}",
op, message_registry.INVALID_LITERAL_VALUE_TYPE.format(expected_type, op.type.name)
)

def visit_get_attr(self, op: GetAttr) -> None:
Expand Down Expand Up @@ -358,7 +366,7 @@ def visit_method_call(self, op: MethodCall) -> None:
decl_index = 1

if len(op.args) + decl_index != len(method_decl.sig.args):
self.fail(op, "Incorrect number of args for method call.")
self.fail(op, message_registry.INCORRECT_NUMBER_ARGS)

# Skip the receiver argument (self)
for arg_value, arg_runtime in zip(op.args, method_decl.sig.args[decl_index:]):
Expand Down
2 changes: 0 additions & 2 deletions mypyc/codegen/emitfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,8 +775,6 @@ def reg(self, reg: Value) -> str:
return "INFINITY"
elif r == "-inf":
return "-INFINITY"
elif r == "nan":
return "NAN"
return r
else:
return self.emitter.reg(reg)
Expand Down
2 changes: 0 additions & 2 deletions mypyc/codegen/literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,6 @@ def float_to_c(x: float) -> str:
return "INFINITY"
elif s == "-inf":
return "-INFINITY"
elif s == "nan":
return "NAN"
return s


Expand Down
15 changes: 5 additions & 10 deletions mypyc/irbuild/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
)
from mypy.types import (
AnyType,
DeletedType,
Instance,
ProperType,
TupleType,
Expand Down Expand Up @@ -302,9 +301,6 @@ def load_bytes_from_str_literal(self, value: str) -> Value:
def load_int(self, value: int) -> Value:
return self.builder.load_int(value)

def load_float(self, value: float) -> Value:
return self.builder.load_float(value)

def unary_op(self, lreg: Value, expr_op: str, line: int) -> Value:
return self.builder.unary_op(lreg, expr_op, line)

Expand Down Expand Up @@ -574,22 +570,21 @@ def get_assignment_target(
self.error("Cannot assign to the first argument of classmethod", line)
if lvalue.kind == LDEF:
if symbol not in self.symtables[-1]:
if isinstance(symbol, Var) and not isinstance(symbol.type, DeletedType):
reg_type = self.type_to_rtype(symbol.type)
else:
reg_type = self.node_type(lvalue)
# If the function is a generator function, then first define a new variable
# in the current function's environment class. Next, define a target that
# refers to the newly defined variable in that environment class. Add the
# target to the table containing class environment variables, as well as the
# current environment.
if self.fn_info.is_generator:
return self.add_var_to_env_class(
symbol, reg_type, self.fn_info.generator_class, reassign=False
symbol,
self.node_type(lvalue),
self.fn_info.generator_class,
reassign=False,
)

# Otherwise define a new local variable.
return self.add_local_reg(symbol, reg_type)
return self.add_local_reg(symbol, self.node_type(lvalue))
else:
# Assign to a previously defined variable.
return self.lookup(symbol)
Expand Down
24 changes: 0 additions & 24 deletions mypyc/irbuild/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from __future__ import annotations

import math
from typing import Callable, Sequence

from mypy.nodes import (
Expand Down Expand Up @@ -129,10 +128,6 @@ def transform_name_expr(builder: IRBuilder, expr: NameExpr) -> Value:
if fullname == "builtins.False":
return builder.false()

math_literal = transform_math_literal(builder, fullname)
if math_literal is not None:
return math_literal

if isinstance(expr.node, Var) and expr.node.is_final:
value = builder.emit_load_final(
expr.node,
Expand Down Expand Up @@ -197,10 +192,6 @@ def transform_member_expr(builder: IRBuilder, expr: MemberExpr) -> Value:
if value is not None:
return value

math_literal = transform_math_literal(builder, expr.fullname)
if math_literal is not None:
return math_literal

if isinstance(expr.node, MypyFile) and expr.node.fullname in builder.imports:
return builder.load_module(expr.node.fullname)

Expand Down Expand Up @@ -1052,18 +1043,3 @@ def transform_assignment_expr(builder: IRBuilder, o: AssignmentExpr) -> Value:
target = builder.get_assignment_target(o.target)
builder.assign(target, value, o.line)
return value


def transform_math_literal(builder: IRBuilder, fullname: str) -> Value | None:
if fullname == "math.e":
return builder.load_float(math.e)
if fullname == "math.pi":
return builder.load_float(math.pi)
if fullname == "math.inf":
return builder.load_float(math.inf)
if fullname == "math.nan":
return builder.load_float(math.nan)
if fullname == "math.tau":
return builder.load_float(math.tau)

return None
Loading