ruff/crates/ty_python_semantic/src
Douglas Creager d37911685f
[ty] Correctly instantiate generic class that inherits `__init__` from generic base class (#19693)
This is subtle, and the root cause became more apparent with #19604,
since we now have many more cases of superclasses and subclasses using
different typevars. The issue is easiest to see in the following:

```py
class C[T]:
    def __init__(self, t: T) -> None: ...

class D[U](C[T]):
    pass

reveal_type(C(1))  # revealed: C[int]
reveal_type(D(1))  # should be: D[int]
```

When instantiating a generic class, the `__init__` method inherits the
generic context of that class. This lets our call binding machinery
infer a specialization for that context.

Prior to this PR, the instantiation of `C` worked just fine. Its
`__init__` method would inherit the `[T]` generic context, and we would
infer `{T = int}` as the specialization based on the argument
parameters.

It didn't work for `D`. The issue is that the `__init__` method was
inheriting the generic context of the class where `__init__` was defined
(here, `C` and `[T]`). At the call site, we would then infer `{T = int}`
as the specialization — but that wouldn't help us specialize `D[U]`,
since `D` does not have `T` in its generic context!

Instead, the `__init__` method should inherit the generic context of the
class that we are performing the lookup on (here, `D` and `[U]`). That
lets us correctly infer `{U = int}` as the specialization, which we can
successfully apply to `D[U]`.

(Note that `__init__` refers to `C`'s typevars in its signature, but
that's okay; our member lookup logic already applies the `T = U`
specialization when returning a member of `C` while performing a lookup
on `D`, transforming its signature from `(Self, T) -> None` to `(Self,
U) -> None`.)

Closes https://github.com/astral-sh/ty/issues/588
2025-08-01 15:29:18 -04:00
..
module_resolver [ty] Remove `KnownModule::is_enum` (#19681) 2025-08-01 10:31:12 +02:00
semantic_index [ty] Track different uses of legacy typevars, including context when rendering typevars (#19604) 2025-08-01 12:20:32 -04:00
types [ty] Correctly instantiate generic class that inherits `__init__` from generic base class (#19693) 2025-08-01 15:29:18 -04:00
util [ty] Simplify lifetime requirements for `PySlice` trait (#19687) 2025-08-01 15:13:47 +01:00
ast_node_ref.rs [ty] Add environment variable to dump Salsa memory usage stats (#18928) 2025-06-26 21:27:51 +00:00
db.rs [ty] Track open files in the server (#19264) 2025-07-18 19:33:35 +05:30
dunder_all.rs Simplify `get_size2` usage (#19643) 2025-07-30 15:31:37 -04:00
lib.rs [ty] Remove `KnownModule::is_enum` (#19681) 2025-08-01 10:31:12 +02:00
lint.rs [ty] Add environment variable to dump Salsa memory usage stats (#18928) 2025-06-26 21:27:51 +00:00
list.rs [ty] Add environment variable to dump Salsa memory usage stats (#18928) 2025-06-26 21:27:51 +00:00
module_name.rs [ty] Make `Module` a Salsa ingredient 2025-07-23 09:46:40 -04:00
node_key.rs [ty] Add environment variable to dump Salsa memory usage stats (#18928) 2025-06-26 21:27:51 +00:00
place.rs [ty] Remove `KnownModule::is_enum` (#19681) 2025-08-01 10:31:12 +02:00
program.rs [ty] Use python version and path from Python extension (#19012) 2025-07-14 09:47:27 +00:00
pull_types.rs Update Rust toolchain to 1.88 and MSRV to 1.86 (#19011) 2025-06-28 20:24:00 +02:00
python_platform.rs Hug closing `}` when f-string expression has a format specifier (#18704) 2025-06-17 07:39:42 +02:00
rank.rs [ty] Garbage-collect reachability constraints (#19414) 2025-07-21 14:16:27 -04:00
semantic_index.rs Simplify `get_size2` usage (#19643) 2025-07-30 15:31:37 -04:00
semantic_model.rs [ty] Split `ScopedPlaceId` into `ScopedSymbolId` and `ScopedMemberId` (#19497) 2025-07-25 13:54:33 +02:00
site_packages.rs [ty] Use python version and path from Python extension (#19012) 2025-07-14 09:47:27 +00:00
suppression.rs Simplify `get_size2` usage (#19643) 2025-07-30 15:31:37 -04:00
types.rs [ty] Track different uses of legacy typevars, including context when rendering typevars (#19604) 2025-08-01 12:20:32 -04:00
unpack.rs [ty] Async for loops and async iterables (#19634) 2025-07-30 17:40:24 +02:00