Skip to content

[2.0 regression] TypeVar with constrains Sub and Super does not accept Sub as a default value #21424

Closed

Description

@hauntsaninja
λ cat test.py   
from typing import TypeVar
T = TypeVar("T", int, float)
def f(x: T = 0): ...
λ mypy test.py --no-incremental                       
test.py:3: error: Incompatible default for parameter "x" (default has type "int", parameter has type "T")  [assignment]
Found 1 error in 1 file (checked 1 source file)

Bisects to #21119

Activity

  1. ilevkivskyi commented on May 7, 2026

    @ilevkivskyi
    Member

    This is not specific to int vs float, i.e. not related to promotions. The same reproduces with:

    class B: ...
    class C(B): ...
    
    S = TypeVar("S", B, C)
    c = C()
    def g(x: S = c): ...
  2. changed the title [-][2.0 regression] int float constrained typevar with int default value[/-] [+][2.0 regression] TypeVar with constrains Sub and Super does not accept Sub as a default value[/+] on May 7, 2026
  3. ilevkivskyi commented on May 7, 2026

    @ilevkivskyi
    Member

    Anyway, it looks like this exposes pre-existing missing edge case in is_subtype() and there is an easy fix. I will make a PR soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions