Commit d379116
authored
[ty] Correctly instantiate generic class that inherits
This is subtle, and the root cause became more apparent with astral-sh#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 astral-sh/ty#588__init__ from generic base class (astral-sh#19693)1 parent 580577e commit d379116
File tree
4 files changed
+122
-8
lines changed- crates/ty_python_semantic
- resources/mdtest/generics
- legacy
- pep695
- src/types
4 files changed
+122
-8
lines changedLines changed: 51 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
329 | 329 | | |
330 | 330 | | |
331 | 331 | | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
332 | 383 | | |
333 | 384 | | |
334 | 385 | | |
| |||
Lines changed: 36 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
308 | 308 | | |
309 | 309 | | |
310 | 310 | | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
311 | 347 | | |
312 | 348 | | |
313 | 349 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
571 | 571 | | |
572 | 572 | | |
573 | 573 | | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
574 | 579 | | |
575 | 580 | | |
576 | 581 | | |
577 | | - | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
578 | 588 | | |
579 | 589 | | |
580 | 590 | | |
| |||
590 | 600 | | |
591 | 601 | | |
592 | 602 | | |
593 | | - | |
| 603 | + | |
594 | 604 | | |
595 | 605 | | |
596 | 606 | | |
| |||
840 | 850 | | |
841 | 851 | | |
842 | 852 | | |
843 | | - | |
844 | | - | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
845 | 858 | | |
846 | 859 | | |
847 | 860 | | |
| |||
1668 | 1681 | | |
1669 | 1682 | | |
1670 | 1683 | | |
1671 | | - | |
| 1684 | + | |
| 1685 | + | |
| 1686 | + | |
| 1687 | + | |
1672 | 1688 | | |
1673 | 1689 | | |
1674 | 1690 | | |
| |||
1716 | 1732 | | |
1717 | 1733 | | |
1718 | 1734 | | |
| 1735 | + | |
1719 | 1736 | | |
1720 | 1737 | | |
1721 | 1738 | | |
| |||
1744 | 1761 | | |
1745 | 1762 | | |
1746 | 1763 | | |
1747 | | - | |
| 1764 | + | |
1748 | 1765 | | |
1749 | 1766 | | |
1750 | 1767 | | |
| |||
1926 | 1943 | | |
1927 | 1944 | | |
1928 | 1945 | | |
1929 | | - | |
| 1946 | + | |
1930 | 1947 | | |
1931 | 1948 | | |
1932 | 1949 | | |
| |||
4321 | 4338 | | |
4322 | 4339 | | |
4323 | 4340 | | |
4324 | | - | |
| 4341 | + | |
| 4342 | + | |
| 4343 | + | |
4325 | 4344 | | |
4326 | 4345 | | |
4327 | 4346 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
360 | 360 | | |
361 | 361 | | |
362 | 362 | | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
363 | 371 | | |
364 | 372 | | |
365 | 373 | | |
| |||
0 commit comments