Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3k
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., usingisinstance
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
deff[T:float|int|str](x:T,y:T)->bool:ifisinstance(x, (float,int))andisinstance(y, (float,int)):returnx<yelifisinstance(x,str)andisinstance(y,str):returnx<yelse:raiseTypeError
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