mirror of
https://github.com/astral-sh/ruff.git
synced 2026-11-21 12:16:15 +08:00
ty used to abort with a stack overflow while checking this
`super().__init__` call. It now reports `invalid-argument-type` on
`value` in `super().__init__(value)`.
```python
class Box[T]:
def __init__[A, B](self: "Box[A]", value: A | tuple[B]) -> None: ...
class Child[T](Box[bytes]):
def __init__(self, value: T | tuple[T]) -> None:
super().__init__(value)
```
The receiver for that call is a `Child[T]`, which is already a
`Box[bytes]`. That fixes `A` to `bytes`. A one-element tuple is valid
because the initializer can infer `B = T`. But `Child[str]("hello")`
would forward a plain string where the initializer expects `bytes` or a
tuple.
ty now uses the receiver’s existing class type arguments when checking a
constructor method. Looking up the method on a bare generic class still
allows the call to infer those arguments.
The stack overflow happened while ty was trying to explain the failed
call. It combined type-variable bindings inferred from individual
arguments, then repeatedly substituted them into each other. A cycle
through a tuple element can add another layer of `tuple[...]` on every
substitution. The original reproducer had the same problem with a
callable’s return type. ty now drops the parts of a binding that would
keep growing and uses the remaining evidence to report the error.
Fixes https://github.com/astral-sh/ty/issues/5047
## Test plan
Compared the CLI on the merge base and this change using the example
above and the original reproducer in both PEP 695 and legacy `TypeVar`
syntax. All three aborted on the merge base and produced argument-type
diagnostics on this change. The example above passes with `Box[T]` as
the base.
## Ecosystem report
On `3c94d4a654a3c2cc6a05b4845d4057271599ebb0`, six new diagnostics
identify constructors that accept arguments incompatible with the
receiver’s specialization. The change also removes a false return
warning in PyMongo and simplifies two existing setuptools diagnostics.
All nine changes were reproduced, with no new regressions found. [Full
ecosystem
report](https://github.com/astral-sh/ruff/pull/30473#issuecomment-6931764426)