[ty] Fix class literal subtyping with object fallback (#20521)
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks instrumented (ruff) (push) Blocked by required conditions
CI / benchmarks instrumented (ty) (push) Blocked by required conditions
CI / benchmarks-walltime (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run

## Summary

@ibraheemdev notes this example failed

```py
from typing import Callable

class X:
    ...

def f(callable: Callable[[], X]) -> X:
    return callable()

x = f(X)
```

Resolves https://github.com/astral-sh/ty/issues/1210

The issue was that we set the `Self` to the class type instead of the
instance type of the class.

## Test Plan

Fix tests in `is_subtype_of.md`
This commit is contained in:
Matthew Mckee 2025-09-23 01:26:25 +01:00 committed by GitHub
parent 094bf70a60
commit 68ae9c8a15
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 7 deletions

View file

@ -1753,12 +1753,6 @@ static_assert(is_subtype_of(TypeOf[B], ReturnsWithArgument[int, B]))
static_assert(is_subtype_of(TypeOf[B], Callable[[], B]))
static_assert(is_subtype_of(TypeOf[B], Returns[B]))
class C: ...
# TODO: These assertions should be true once we understand `Self`
static_assert(is_subtype_of(TypeOf[C], Callable[[], C])) # error: [static-assert-error]
static_assert(is_subtype_of(TypeOf[C], Returns[C])) # error: [static-assert-error]
class D[T]:
def __init__(self, x: T) -> None: ...
@ -1875,6 +1869,21 @@ static_assert(not is_subtype_of(TypeOf[F], Callable[[int], F]))
static_assert(not is_subtype_of(TypeOf[F], ReturnsWithArgument[int, F]))
```
### Classes with no constructor methods
```py
from typing import Callable, Protocol
from ty_extensions import TypeOf, static_assert, is_subtype_of
class Returns[T](Protocol):
def __call__(self) -> T: ...
class A: ...
static_assert(is_subtype_of(TypeOf[A], Callable[[], A]))
static_assert(is_subtype_of(TypeOf[A], Returns[A]))
```
### Subclass of
#### Type of a class with constructor methods

View file

@ -1185,7 +1185,7 @@ impl<'db> ClassType<'db> {
if let Place::Type(Type::FunctionLiteral(new_function), _) = new_function_symbol {
Type::Callable(
new_function
.into_bound_method_type(db, self_ty)
.into_bound_method_type(db, correct_return_type)
.into_callable_type(db),
)
} else {