mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 02:38:25 +00:00
[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:
parent
3ccc0edfe4
commit
bdccb37b4a
5 changed files with 110 additions and 86 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue