[ty] Apply function specialization to all overloads (#18020)

Function literals have an optional specialization, which is applied to
the parameter/return type annotations lazily when the function's
signature is requested. We were previously only applying this
specialization to the final overload of an overloaded function.

This manifested most visibly for `list.__add__`, which has an overloaded
definition in the typeshed:


b398b83631/crates/ty_vendored/vendor/typeshed/stdlib/builtins.pyi (L1069-L1072)

Closes https://github.com/astral-sh/ty/issues/314
This commit is contained in:
Douglas Creager 2025-05-12 13:48:54 -04:00 committed by GitHub
parent 3ccc0edfe4
commit bdccb37b4a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 110 additions and 86 deletions

View file

@ -454,6 +454,28 @@ reveal_type(d.method3()) # revealed: SomeProtocol[int]
reveal_type(d.method3().x) # revealed: int
```
When a method is overloaded, the specialization is applied to all overloads.
```py
from typing import overload, Generic, TypeVar
S = TypeVar("S")
class WithOverloadedMethod(Generic[T]):
@overload
def method(self, x: T) -> T:
return x
@overload
def method(self, x: S) -> S | T:
return x
def method(self, x: S | T) -> S | T:
return x
reveal_type(WithOverloadedMethod[int].method) # revealed: Overload[(self, x: int) -> int, (self, x: S) -> S | int]
```
## Cyclic class definitions
### F-bounded quantification

View file

@ -347,6 +347,26 @@ reveal_type(c.method2()) # revealed: str
reveal_type(c.method3()) # revealed: LinkedList[int]
```
When a method is overloaded, the specialization is applied to all overloads.
```py
from typing import overload
class WithOverloadedMethod[T]:
@overload
def method(self, x: T) -> T:
return x
@overload
def method[S](self, x: S) -> S | T:
return x
def method[S](self, x: S | T) -> S | T:
return x
reveal_type(WithOverloadedMethod[int].method) # revealed: Overload[(self, x: int) -> int, (self, x: S) -> S | int]
```
## Cyclic class definitions
### F-bounded quantification