Skip to content

Wrong comparison in type error for comparisons #22316

Open

Description

@A5rocks

Bug Report

See title.

To Reproduce

def f(x: int | None) -> bool:
    return x >= 0

Expected Behavior

main.py:2: error: Unsupported operand types for >= ("int" and "None")  [operator]
main.py:2: note: Left operand is of type "int | None"

Actual Behavior

main.py:2: error: Unsupported operand types for <= ("int" and "None")  [operator]
main.py:2: note: Left operand is of type "int | None"

Note <=.

Your Environment

Reproduced in mypy-play.

  • Mypy version used: 2.4.1
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.12

Activity

  1. inchang-ing commented on Oct 29, 2026

    @inchang-ing
    Contributor

    I can reproduce this on master (2.5.0+dev @ f501cd0):

    def f(x: int | None) -> bool:
        return x >= 0
    main.py:2: error: Unsupported operand types for <= ("int" and "None")  [operator]
    main.py:2: note: Left operand is of type "int | None"
    

    Root cause: the message is rendered in mypy/messages.py:646 (incompatible_argument, the elif name.startswith('"__') branch) purely from the name of the method that failed:

    for method, op in op_methods_to_symbols.items():
        for variant in method, "__r" + method[2:]:
            if name.startswith(f'"{variant}" of'):

    NoneType has no __ge__, so check_op_reversible falls back to the reflected call (mypy/checkexpr.py:4215, rev_op_name = operators.reverse_op_methods[op_name]) and the callee that actually fails is "__le__" of "int". The symbol is therefore rendered as <= and the operands are printed in reflected order; nothing maps the reflected method back to the operator the user actually wrote. The result also disagrees with CPython's runtime message ('>=' not supported between instances of 'NoneType' and 'int').

    All four orderings are mirrored on master when the left operand lacks the forward method:

    written reported
    x >= 0 <= ("int" and "None")
    x <= 0 >= ("int" and "None")
    x > 0 < ("int" and "None")
    x < 0 > ("int" and "None")

    (When the forward method exists the symbol is correct, e.g. x: int; s: str; x >= s -> >= ("int" and "str").)

    I'd like to work on this. One design question first, because the mirrored rendering is baked into existing assertions, e.g. test-data/unit/check-incremental.test:3101-3103 (1 <= A(1) -> >=, 1 >= A(1) -> <=) and test-data/unit/check-plugin-attrs.test:208-210 (roughly 8-12 assertions total):

    • Option A: always report the operator as written, with operands in source order (x >= 0 -> Unsupported operand types for >= ...), and update those assertions.
    • Option B: keep the reflected rendering, but add a note saying the error comes from the reflected call.

    My lean is A (it matches both the source text and CPython's message), but I'll follow your preference.

  2. added a commit that references this issue on Nov 6, 2026
    825cf42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions