Skip to content

Unexpected failure of union type to unify with generic type with value restriction #19141

Closed as not planned
@bwo

Description

@bwo

gist link

code:

from typing import TypeVar


class A:
    pass


class B:
    pass


class C:
    pass


T = TypeVar("T", A, B, C)


def foo(o: T) -> None:
    assert False


def bar(o: A | B) -> None:
    return foo(o)

This fails: main.py:24: error: Value of type variable "T" of "foo" cannot be "A | B" [type-var]

In this case, where there is exactly one parameter with type T, I would expect it to succeed: in the call foo(o), with o: A | B, o must be either A (which is part of T's range), or B (likewise).

I understand that this would also fail and should fail:

def foo2(a: T, b: T) -> None:
    assert False

def bar2(a: A | B, b: A | B) -> None:
    return foo2(a, b)

Because a could be A and b could be B, which would violate the restriction in foo2 that they be the same type (whatever type they might be). But with foo as written, no such mischief can arise.

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