Skip to content

[2.0 regression] Bad result from heuristic non-narrowing of type[T] #21701

Description

@neutrinoceros

Bug Report

(A clear and concise description of what the bug is.)

To Reproduce

frommathimportisnanfromtypingimportTypeVarV=TypeVar("V")
defassert_scalar_equal(s1: V, s2: V, /) ->None:
asserttype(s1) istype(s2)
iftype(s1) isfloatandtype(s2) isfloatandisnan(s2):
assertisnan(s1)
else:
asserts1==s2

Expected Behavior

This snippet should type check.

Actual Behavior

mypy.ini: No [mypy] section in config file
t.py:9: error: Argument 1 to "isnan" has incompatible type "V"; expected "SupportsFloat | SupportsIndex" [arg-type]
t.py:10: error: Argument 1 to "isnan" has incompatible type "V"; expected "SupportsFloat | SupportsIndex" [arg-type]
Found 2 errors in 1 file (checked 1 source file)

Even more minimal reprod:

frommathimportisnanfromtypingimportTypeVarV=TypeVar("V")
deffoo(s1: V, /) ->None:
iftype(s1) isfloat:
assertisnan(s1)
mypy.ini: No [mypy] section in config file
t.py:9: error: Argument 1 to "isnan" has incompatible type "V"; expected "SupportsFloat | SupportsIndex" [arg-type]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 2.0.0 or 2.1.0
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: tested 3.11.14 and 3.14.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-type-narrowingConditional type narrowing / binder

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions