Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Comparison operators emit "Unsupported operand types" when usingisinstance to narrowTypeVar to union of numeric types #19454

Open
Labels
bugmypy got something wrong
@bkeryan

Description

@bkeryan

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 frommypy.ini (and other config files): none
  • Python version used: 3.12.9

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp