Skip to content

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

Closed as not planned
Closed as not planned
@eltoder

Description

@eltoder

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

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