From 31f2cf073df7cee35982cba2d1f39a3c96825457 Mon Sep 17 00:00:00 2001 From: xixi-box Date: Thu, 29 Oct 2026 10:01:21 +0800 Subject: [PATCH 1/2] Use the paired error's code for union operand notes When a binary operator fails with [arg-type] because an operand is a union, mypy emits two explanatory notes ("Left/Right operand is of type ..."), but they were always tagged with codes.OPERATOR. A `# type: ignore[arg-type]` therefore silenced the error and left the notes behind, and mypy then reported `Error code "operator" not covered by "type: ignore[arg-type]" comment` - noise naming a code the user never saw. Pass the paired error's code down to those notes. The default stays codes.OPERATOR, so every other call site behaves exactly as before. Closes #22230 --- mypy/checkexpr.py | 11 ++++++++--- mypy/messages.py | 16 ++++++++++++---- test-data/unit/check-errorcodes.test | 11 +++++++++++ 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index 2ed757eeeaa83..c7a9740cbd9cd 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -4433,13 +4433,18 @@ def check_op( recent_context = TempNode(NoneType()) recent_context.line = err.line recent_context.column = err.column + # Use the error code of the message these notes explain, so that an + # ignore comment for that code also silences the notes. + note_code = err.code or codes.OPERATOR if len(left_variants) >= 2 and len(right_variants) >= 2: - self.msg.warn_both_operands_are_from_unions(recent_context) + self.msg.warn_both_operands_are_from_unions(recent_context, code=note_code) elif len(left_variants) >= 2: - self.msg.warn_operand_was_from_union("Left", base_type, context=recent_context) + self.msg.warn_operand_was_from_union( + "Left", base_type, context=recent_context, code=note_code + ) elif len(right_variants) >= 2: self.msg.warn_operand_was_from_union( - "Right", right_type, context=recent_context + "Right", right_type, context=recent_context, code=note_code ) # See the comment in 'check_overload_call' for more details on why diff --git a/mypy/messages.py b/mypy/messages.py index 83af360821b7a..bcb355f87f4e6 100644 --- a/mypy/messages.py +++ b/mypy/messages.py @@ -1775,14 +1775,22 @@ def overloaded_signatures_ret_specific(self, index: int, context: Context) -> No context, ) - def warn_both_operands_are_from_unions(self, context: Context) -> None: - self.note("Both left and right operands are unions", context, code=codes.OPERATOR) + def warn_both_operands_are_from_unions( + self, context: Context, code: ErrorCode | None = codes.OPERATOR + ) -> None: + self.note("Both left and right operands are unions", context, code=code) - def warn_operand_was_from_union(self, side: str, original: Type, context: Context) -> None: + def warn_operand_was_from_union( + self, + side: str, + original: Type, + context: Context, + code: ErrorCode | None = codes.OPERATOR, + ) -> None: self.note( f"{side} operand is of type {format_type(original, self.options)}", context, - code=codes.OPERATOR, + code=code, ) def operator_method_signatures_overlap( diff --git a/test-data/unit/check-errorcodes.test b/test-data/unit/check-errorcodes.test index 455eb93d940bc..49525eb473d1c 100644 --- a/test-data/unit/check-errorcodes.test +++ b/test-data/unit/check-errorcodes.test @@ -1419,3 +1419,14 @@ def process(response1: int,response2: int) -> int: # E: Overloaded function sign def process(response1,response2)-> Union[float,int]: return response1 + response2 + +[case testErrorCodeUnionOperandNoteWithIgnore] +class OnlyA: + def __call__(self, other: "A") -> bool: + return True + +class A: + __le__ = OnlyA() + +def f(x: A, y: A | int) -> None: + x <= y # type: ignore[arg-type] From e3d09126dbf1398664e8cc61409c44a7658b0a9e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 29 Oct 2026 02:03:22 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mypy/messages.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/mypy/messages.py b/mypy/messages.py index bcb355f87f4e6..7a371986f955f 100644 --- a/mypy/messages.py +++ b/mypy/messages.py @@ -1781,16 +1781,10 @@ def warn_both_operands_are_from_unions( self.note("Both left and right operands are unions", context, code=code) def warn_operand_was_from_union( - self, - side: str, - original: Type, - context: Context, - code: ErrorCode | None = codes.OPERATOR, + self, side: str, original: Type, context: Context, code: ErrorCode | None = codes.OPERATOR ) -> None: self.note( - f"{side} operand is of type {format_type(original, self.options)}", - context, - code=code, + f"{side} operand is of type {format_type(original, self.options)}", context, code=code ) def operator_method_signatures_overlap(