[ty] Infer function call typevars in both directions (#18155)

This primarily comes up with annotated `self` parameters in
constructors:

```py
class C[T]:
    def __init__(self: C[int]): ...
```

Here, we want infer a specialization of `{T = int}` for a call that hits
this overload.

Normally when inferring a specialization of a function call, typevars
appear in the parameter annotations, and not in the argument types. In
this case, this is reversed: we need to verify that the `self` argument
(`C[T]`, as we have not yet completed specialization inference) is
assignable to the parameter type `C[int]`.

To do this, we simply look for a typevar/type in both directions when
performing inference, and apply the inferred specialization to argument
types as well as parameter types before verifying assignability.

As a wrinkle, this exposed that we were not checking
subtyping/assignability for function literals correctly. Our function
literal representation includes an optional specialization that should
be applied to the signature. Before, function literals were considered
subtypes of (assignable to) each other only if they were identical Salsa
objects. Two function literals with different specializations should
still be considered subtypes of (assignable to) each other if those
specializations result in the same function signature (typically because
the function doesn't use the typevars in the specialization).

Closes https://github.com/astral-sh/ty/issues/370
Closes https://github.com/astral-sh/ty/issues/100
Closes https://github.com/astral-sh/ty/issues/258

---------

Co-authored-by: Carl Meyer <carl@astral.sh>
This commit is contained in:
Douglas Creager 2025-05-19 11:45:40 -04:00 committed by GitHub
parent 569c94b71b
commit 97058e8093
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 366 additions and 53 deletions

View file

@ -341,6 +341,39 @@ reveal_type(C(1, True)) # revealed: C[Literal[1]]
wrong_innards: C[int] = C("five", 1)
```
### Some `__init__` overloads only apply to certain specializations
```py
from typing import overload, Generic, TypeVar
T = TypeVar("T")
class C(Generic[T]):
@overload
def __init__(self: "C[str]", x: str) -> None: ...
@overload
def __init__(self: "C[bytes]", x: bytes) -> None: ...
@overload
def __init__(self, x: int) -> None: ...
def __init__(self, x: str | bytes | int) -> None: ...
reveal_type(C("string")) # revealed: C[str]
reveal_type(C(b"bytes")) # revealed: C[bytes]
reveal_type(C(12)) # revealed: C[Unknown]
C[str]("string")
C[str](b"bytes") # error: [no-matching-overload]
C[str](12)
C[bytes]("string") # error: [no-matching-overload]
C[bytes](b"bytes")
C[bytes](12)
C[None]("string") # error: [no-matching-overload]
C[None](b"bytes") # error: [no-matching-overload]
C[None](12)
```
## Generic subclass
When a generic subclass fills its superclass's type parameter with one of its own, the actual types

View file

@ -279,6 +279,37 @@ reveal_type(C(1, True)) # revealed: C[Literal[1]]
wrong_innards: C[int] = C("five", 1)
```
### Some `__init__` overloads only apply to certain specializations
```py
from typing import overload
class C[T]:
@overload
def __init__(self: C[str], x: str) -> None: ...
@overload
def __init__(self: C[bytes], x: bytes) -> None: ...
@overload
def __init__(self, x: int) -> None: ...
def __init__(self, x: str | bytes | int) -> None: ...
reveal_type(C("string")) # revealed: C[str]
reveal_type(C(b"bytes")) # revealed: C[bytes]
reveal_type(C(12)) # revealed: C[Unknown]
C[str]("string")
C[str](b"bytes") # error: [no-matching-overload]
C[str](12)
C[bytes]("string") # error: [no-matching-overload]
C[bytes](b"bytes")
C[bytes](12)
C[None]("string") # error: [no-matching-overload]
C[None](b"bytes") # error: [no-matching-overload]
C[None](12)
```
## Generic subclass
When a generic subclass fills its superclass's type parameter with one of its own, the actual types

View file

@ -102,7 +102,7 @@ class C[T]:
return "a"
reveal_type(getattr_static(C[int], "f")) # revealed: def f(self, x: int) -> str
reveal_type(getattr_static(C[int], "f").__get__) # revealed: <method-wrapper `__get__` of `f[int]`>
reveal_type(getattr_static(C[int], "f").__get__) # revealed: <method-wrapper `__get__` of `f`>
reveal_type(getattr_static(C[int], "f").__get__(None, C[int])) # revealed: def f(self, x: int) -> str
# revealed: bound method C[int].f(x: int) -> str
reveal_type(getattr_static(C[int], "f").__get__(C[int](), C[int]))