mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Narrowing doesn't work on a typevar with a union bound

Open eltoder opened this issue 2 years ago • 0 comments

Bug Report

not isinstance check fails to narrow type for a TypeVar with a union bound.

To Reproduce

from typing import TypeVar

T = TypeVar("T", bound=int | str)

def foo(x: T) -> None:
    if not isinstance(x, int):
        reveal_type(x)

https://mypy-play.net/?mypy=latest&python=3.11&gist=2be94d37f0b12222d62a7f40e59fb152

Expected Behavior

Revealed type is "builtins.str".

Actual Behavior

Revealed type is "T`-1".

Your Environment

  • Mypy version used: 1.4.1
  • Python version used: 3.10.6

eltoder avatar Jul 09 '23 17:07 eltoder