ruff/crates/ty_python_semantic
Carl Meyer c88e1e40ab
Fix stack overflow with recursive generic protocols
This fixes https://github.com/astral-sh/ty/issues/1736 where recursive
generic protocols with growing specializations caused a stack overflow.

The issue occurred with protocols like:
```python
class C[T](Protocol):
    a: 'C[set[T]]'
```

When checking `C[set[int]]` against `C[Unknown]`, member `a` requires
checking `C[set[set[int]]]`, which requires `C[set[set[set[int]]]]`,
etc. Each level has different type specializations, so the existing
cycle detection (using full types as cache keys) didn't catch the
infinite recursion.

The fix introduces `TypeRelationKey`, an enum that can be either a full
`Type` or a `ClassLiteral` (protocol class without specialization). For
protocol-to-protocol comparisons, we use `ClassLiteral` keys, which
detects when we're comparing the same protocol class regardless of
specialization. When a cycle is detected, we return the fallback value
(assume compatible) to safely terminate the recursion.
2025-12-08 18:38:26 -08:00
..
resources Fix stack overflow with recursive generic protocols 2025-12-08 18:38:26 -08:00
src Fix stack overflow with recursive generic protocols 2025-12-08 18:38:26 -08:00
tests [ty] Don't send publish diagnostics for clients supporting pull diagnostics (#21772) 2025-12-04 08:12:04 +01:00
Cargo.toml [ty] Add 'remove unused ignore comment' code action (#21582) 2025-11-25 08:08:21 -05:00
build.rs Rename Red Knot (#17820) 2025-05-03 19:49:15 +02:00
mdtest.py [ty] mdtests with external dependencies (#20904) 2025-12-08 11:44:20 +01:00
mdtest.py.lock Rename Red Knot (#17820) 2025-05-03 19:49:15 +02:00