Skip to content

typevar identity should not matter for generic function subtyping/assignability #95

@carljm

Description

@carljm

When we do subtype/assignability checks within a generic scope, we don't consider two different typevars to be equivalent, even if they have the same bounds/constraints, because they could specialize to two different types.

But when we compare two unspecialized generic signatures for assignability/subtyping, this consideration does not apply. Two identical signatures, except that they use a different typevar (with the same bounds/constraints), are the same signature; typevar identity is irrelevant. In this example, all six assertions should pass; currently only the first and third and fifth pass:

from typing import TypeVar, Callable

from ty_extensions import is_assignable_to, is_subtype_of, static_assert

T = TypeVar("T")
U = TypeVar("U")

static_assert(is_assignable_to(Callable[[T], T], Callable[[T], T]))
static_assert(is_assignable_to(Callable[[T], T], Callable[[U], U]))

static_assert(is_subtype_of(Callable[[T], T], Callable[[T], T]))
static_assert(is_subtype_of(Callable[[T], T], Callable[[U], U]))

static_assert(is_equivalent_to(Callable[[T], T], Callable[[T], T]))
static_assert(is_equivalent_to(Callable[[T], T], Callable[[U], U]))

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinggenericsBugs or features relating to ty's generics implementationtype propertiessubtyping, assignability, equivalence, and more

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions