Skip to content

redundant-expr does not work with TypeVar with multiple constraints #18434

Open
@kasium

Description

@kasium

Bug Report
A TypeVar with multiple constraints can be seen as a union. However, the redundant-expr does not understand it and emits errors which are wrong.

To Reproduce

from typing import TypeVar

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

def foo(var: T) -> T:
    return "aa" if isinstance(var, str) else 0

Expected Behavior

no error

Actual Behavior

foo.py:9:20: error: If condition is always false  [redundant-expr]
        return "aa" if isinstance(var, str) else 0
                       ^~~~~~~~~~~~~~~~~~~~
foo.py:9:20: error: If condition is always true  [redundant-expr]
        return "aa" if isinstance(var, str) else 0
                       ^~~~~~~~~~~~~~~~~~~~
Found 2 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.14.1
  • Mypy command-line flags: --enable-error-code redundant-expr
  • Python version used: 3.13.0

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions