[ty] fix stack overflow when comparing recursive NamedTuple types with is_disjoint_from (#20538)
Some checks are pending
CI / cargo build (release) (push) Waiting to run
CI / mkdocs (push) Waiting to run
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 (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 / 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

I found this bug while working on #20528.
The minimum reproducible code is:

```python
from __future__ import annotations

from typing import NamedTuple
from ty_extensions import is_disjoint_from, static_assert

class Path(NamedTuple):
    prev: Path | None
    key: str

static_assert(not is_disjoint_from(Path, Path))
```

A stack overflow occurs when a nominal instance type inherits from
`NamedTuple` and is defined recursively.
This PR fixes this bug.

## Test Plan

mdtest updated
This commit is contained in:
Shunsuke Shibayama 2025-09-24 02:29:03 +09:00 committed by GitHub
parent dbc5983503
commit 722f1a7d7a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 29 additions and 3 deletions

View file

@ -616,6 +616,31 @@ class BarNone(Protocol):
static_assert(is_disjoint_from(type[Foo], BarNone))
```
### `NamedTuple`
```py
from __future__ import annotations
from typing import NamedTuple, final
from ty_extensions import is_disjoint_from, static_assert
@final
class Path(NamedTuple):
prev: Path | None
key: str
@final
class Path2(NamedTuple):
prev: Path2 | None
key: str
static_assert(not is_disjoint_from(Path, Path))
static_assert(not is_disjoint_from(Path, tuple[Path | None, str]))
static_assert(is_disjoint_from(Path, tuple[Path | None]))
static_assert(is_disjoint_from(Path, tuple[Path | None, str, int]))
static_assert(is_disjoint_from(Path, Path2))
```
## Callables
No two callable types are disjoint because there exists a non-empty callable type

View file

@ -2553,9 +2553,10 @@ impl<'db> Type<'db> {
other.is_disjoint_from_impl(db, KnownClass::ModuleType.to_instance(db), visitor)
}
(Type::NominalInstance(left), Type::NominalInstance(right)) => {
left.is_disjoint_from_impl(db, right, visitor)
}
(Type::NominalInstance(left), Type::NominalInstance(right)) => visitor
.visit((self, other), || {
left.is_disjoint_from_impl(db, right, visitor)
}),
(Type::PropertyInstance(_), other) | (other, Type::PropertyInstance(_)) => {
KnownClass::Property