[ty] Use C[T] instead of C[Unknown] for the upper bound of Self (#20479)

### Summary

This PR includes two changes, both of which are necessary to resolve
https://github.com/astral-sh/ty/issues/1196:

* For a generic class `C[T]`, we previously used `C[Unknown]` as the
upper bound of the `Self` type variable. There were two problems with
this. For one, when `Self` appeared in contravariant position, we would
materialize its upper bound to `Bottom[C[Unknown]]` (which might
simplify to `C[Never]` if `C` is covariant in `T`) when accessing
methods on `Top[C[Unknown]]`. This would result in `invalid-argument`
errors on the `self` parameter. Also, using an upper bound of
`C[Unknown]` would mean that inside methods, references to `T` would be
treated as `Unknown`. This could lead to false negatives. To fix this,
we now use `C[T]` (with a "nested" typevar) as the upper bound for
`Self` on `C[T]`.
* In order to make this work, we needed to allow assignability/subtyping
of inferable typevars to other types, since we now check assignability
of e.g. `C[int]` to `C[T]` (when checking assignability to the upper
bound of `Self`) when calling an instance-method on `C[int]` whose
`self` parameter is annotated as `self: Self` (or implicitly `Self`,
following https://github.com/astral-sh/ruff/pull/18007).

closes https://github.com/astral-sh/ty/issues/1196
closes https://github.com/astral-sh/ty/issues/1208


### Test Plan

Regression tests for both issues.
This commit is contained in:
David Peter 2025-09-23 14:02:25 +02:00 committed by GitHub
parent fd5c48c539
commit 742f8a4ee6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 213 additions and 15 deletions

View file

@ -366,6 +366,31 @@ reveal_type(f(g("a"))) # revealed: tuple[Literal["a"] | None, int]
reveal_type(g(f("a"))) # revealed: tuple[Literal["a"], int] | None
```
## Passing generic functions to generic functions
```py
from typing import Callable, TypeVar
A = TypeVar("A")
B = TypeVar("B")
T = TypeVar("T")
def invoke(fn: Callable[[A], B], value: A) -> B:
return fn(value)
def identity(x: T) -> T:
return x
def head(xs: list[T]) -> T:
return xs[0]
# TODO: this should be `Literal[1]`
reveal_type(invoke(identity, 1)) # revealed: Unknown
# TODO: this should be `Unknown | int`
reveal_type(invoke(head, [1, 2, 3])) # revealed: Unknown
```
## Opaque decorators don't affect typevar binding
Inside the body of a generic function, we should be able to see that the typevars bound by that

View file

@ -323,6 +323,27 @@ reveal_type(f(g("a"))) # revealed: tuple[Literal["a"] | None, int]
reveal_type(g(f("a"))) # revealed: tuple[Literal["a"], int] | None
```
## Passing generic functions to generic functions
```py
from typing import Callable
def invoke[A, B](fn: Callable[[A], B], value: A) -> B:
return fn(value)
def identity[T](x: T) -> T:
return x
def head[T](xs: list[T]) -> T:
return xs[0]
# TODO: this should be `Literal[1]`
reveal_type(invoke(identity, 1)) # revealed: Unknown
# TODO: this should be `Unknown | int`
reveal_type(invoke(head, [1, 2, 3])) # revealed: Unknown
```
## Protocols as TypeVar bounds
Protocol types can be used as TypeVar bounds, just like nominal types.

View file

@ -321,8 +321,11 @@ a covariant generic, this is equivalent to using the upper bound of the type par
`object`):
```py
from typing import Self
class Covariant[T]:
def get(self) -> T:
# TODO: remove the explicit `Self` annotation, once we support the implicit type of `self`
def get(self: Self) -> T:
raise NotImplementedError
def _(x: object):
@ -335,7 +338,8 @@ Similarly, contravariant type parameters use their lower bound of `Never`:
```py
class Contravariant[T]:
def push(self, x: T) -> None: ...
# TODO: remove the explicit `Self` annotation, once we support the implicit type of `self`
def push(self: Self, x: T) -> None: ...
def _(x: object):
if isinstance(x, Contravariant):
@ -350,8 +354,10 @@ the type system, so we represent it with the internal `Top[]` special form.
```py
class Invariant[T]:
def push(self, x: T) -> None: ...
def get(self) -> T:
# TODO: remove the explicit `Self` annotation, once we support the implicit type of `self`
def push(self: Self, x: T) -> None: ...
# TODO: remove the explicit `Self` annotation, once we support the implicit type of `self`
def get(self: Self) -> T:
raise NotImplementedError
def _(x: object):

View file

@ -99,7 +99,7 @@ reveal_type(foo(b"")) # revealed: bytes
## Methods
```py
from typing import overload
from typing_extensions import Self, overload
class Foo1:
@overload
@ -126,6 +126,18 @@ foo2 = Foo2()
reveal_type(foo2.method) # revealed: Overload[() -> None, (x: str) -> str]
reveal_type(foo2.method()) # revealed: None
reveal_type(foo2.method("")) # revealed: str
class Foo3:
@overload
def takes_self_or_int(self: Self, x: Self) -> Self: ...
@overload
def takes_self_or_int(self: Self, x: int) -> int: ...
def takes_self_or_int(self: Self, x: Self | int) -> Self | int:
return x
foo3 = Foo3()
reveal_type(foo3.takes_self_or_int(foo3)) # revealed: Foo3
reveal_type(foo3.takes_self_or_int(1)) # revealed: int
```
## Constructor