Skip to content

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

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

fromtypingimportTypeVarT=TypeVar("T", int, str)
deffoo(var: T) ->T:
return"aa"ifisinstance(var, str) else0

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