python / python/mypy

Direction of comparison operator in error message does not depend on direction in code

Open
#17,460 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug priority-2-low
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

When encountering an error due to using a comparison operator with None as one of the arguments, Mypy adjusts the direction of < and > operators in its messages based on the types involved so that it always reports None as the second type (e.g. Unsupported operand types for {comparisson operator direction} ("int" and "None")). This becomes increasingly confusing when both variables in the comparison could potentially be a large number of types.

To reproduce

For the following code

1 < None
1 > None
None < 1
None > 1

Expected Behavior

file.py:1: error: Unsupported operand types for < ("int" and "None")  [operator]
file.py:2: error: Unsupported operand types for > ("int" and "None")  [operator]
file.py:3: error: Unsupported operand types for < ("None" and "int")  [operator]
file.py:4: error: Unsupported operand types for > ("None" and "int")  [operator]

Actual Behavior

file.py:1: error: Unsupported operand types for < ("int" and "None")  [operator]
file.py:2: error: Unsupported operand types for > ("int" and "None")  [operator]
file.py:3: error: Unsupported operand types for > ("int" and "None")  [operator]
file.py:4: error: Unsupported operand types for < ("int" and "None")  [operator]

Your Environment

  • Mypy version used: 1.10
  • Python version used: 3.12

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the four comparison cases from the issue with mypy 1.10, then trace the diagnostic path that formats unsupported operand types. Add regression coverage for both operand orders and verify that each message preserves the source operator and operand order shown in the expected output.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.