-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Description
Bug Report
I have a generic class that wraps a bool, float, int, or str. It implements the comparison operator magic methods like __lt__
etc., using isinstance
to ensure that callers don't mix bool/float/int with str. With mypy 1.16.1, this does not error, but with mypy 1.17.0, the comparison operators started emitting "error: Unsupported operand types for < (likely involving Union) [operator]"
To Reproduce
Minimal reproduction (using a generic function instead of a generic class): https://mypy-play.net/?mypy=latest&python=3.12&gist=dd4696c3ba71103c49d7a4aaf34b2aef
def f[T: float | int | str](x: T, y: T) -> bool:
if isinstance(x, (float, int)) and isinstance(y, (float, int)):
return x < y
elif isinstance(x, str) and isinstance(y, str):
return x < y
else:
raise TypeError
Expected Behavior
No errors.
Actual Behavior
test3.py:3: error: Unsupported operand types for < (likely involving Union) [operator]
Found 1 error in 1 file (checked 1 source file)
It does not error if I remove the type variable and use union types directly:
def f(x: float | int | str, y: float | int | str) -> bool:
Your Environment
- Mypy version used: 1.17.0
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini
(and other config files): none - Python version used: 3.12.9