[ty] Fix panic on recursive class definitions in a stub that use constrained type variables (#20955)

This commit is contained in:
Alex Waygood 2025-10-18 14:02:55 +01:00 committed by GitHub
parent 7532155c9b
commit 16efe53a72
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 55 additions and 4 deletions

View file

@ -753,5 +753,29 @@ class C(C, Generic[T]): ...
class D(D[int], Generic[T]): ...
```
### Cyclic inheritance in a stub file combined with constrained type variables
This is a regression test for <https://github.com/astral-sh/ty/issues/1390>; we used to panic on
this:
`stub.pyi`:
```pyi
from typing import Generic, TypeVar
class A(B): ...
class G: ...
T = TypeVar("T", G, A)
class C(Generic[T]): ...
class B(C[A]): ...
class D(C[G]): ...
def func(x: D): ...
func(G()) # error: [invalid-argument-type]
```
[crtp]: https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern
[f-bound]: https://en.wikipedia.org/wiki/Bounded_quantification#F-bounded_quantification

View file

@ -638,6 +638,25 @@ class C[T](C): ...
class D[T](D[int]): ...
```
### Cyclic inheritance in a stub file combined with constrained type variables
This is a regression test for <https://github.com/astral-sh/ty/issues/1390>; we used to panic on
this:
`stub.pyi`:
```pyi
class A(B): ...
class G: ...
class C[T: (G, A)]: ...
class B(C[A]): ...
class D(C[G]): ...
def func(x: D): ...
func(G()) # error: [invalid-argument-type]
```
## Tuple as a PEP-695 generic class
Our special handling for `tuple` does not break if `tuple` is defined as a PEP-695 generic class in