Skip to content
Closed
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
8 changes: 5 additions & 3 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4423,12 +4423,14 @@ def check_op(
recent_context.line = err.line
recent_context.column = err.column
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, parent_error=err)
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, parent_error=err
)
elif len(right_variants) >= 2:
self.msg.warn_operand_was_from_union(
"Right", right_type, context=recent_context
"Right", right_type, context=recent_context, parent_error=err
)

# See the comment in 'check_overload_call' for more details on why
Expand Down
12 changes: 8 additions & 4 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1775,14 +1775,18 @@ 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, *, parent_error: ErrorInfo
) -> None:
self.note("Both left and right operands are unions", context, parent_error=parent_error)

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, *, parent_error: ErrorInfo
) -> None:
self.note(
f"{side} operand is of type {format_type(original, self.options)}",
context,
code=codes.OPERATOR,
parent_error=parent_error,
)

def operator_method_signatures_overlap(
Expand Down
12 changes: 12 additions & 0 deletions test-data/unit/check-errorcodes.test
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,18 @@ y = 1 # type: int, int # type: ignore[syntax]
# N: Error code "attr-defined" not covered by "type: ignore[xyz]" comment
'x'.foobar # type: ignore

[case testOperatorUnionNoteWithIgnoredArgType]
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]
[out]

[case testErrorCodeIgnore2]
a = 'x'.foobar # type: int # type: ignore[attr-defined]
b = 'x'.foobar # type: int # type: ignore[xyz] # E: "str" has no attribute "foobar" [attr-defined] \
Expand Down
Loading