ruff
1d5d4c52 - [ty] Infer generic protocols from class objects (#27812)

Commit
17 hours ago
[ty] Infer generic protocols from class objects (#27812) ## Summary Passing a class object to a generic protocol parameter could leave the protocol's type arguments as `Unknown`, even when its attributes or methods supplied enough information to infer them. Ordinary instance arguments and unions already used structural constraints, but individual class objects fell through to inference that only considered `__call__`. Use the existing structural constraint machinery for class literals, specialized generic classes, and `type[C]` arguments. This preserves the class object's own interface and also fixes `list(EnumClass)` inferring `list[Unknown]`. Fixes https://github.com/astral-sh/ty/issues/4291 ## Test plan Added mdtests for inference through plain class attributes, class methods, static methods, ordinary-method protocols, specialized generic class objects, and unions of class types. Callback regressions cover preserving `T` when passing `type[T]` and inferring from a data member alongside `__call__`. The tests also retain instance behavior and rejection of an incompatible return type. Updated the enum iteration test to expect the precise member type. ### Ecosystem The ecosystem report includes a new `invalid-assignment` diagnostic in [Scrapy's telnet test](https://github.com/scrapy/scrapy/blob/488a762d7173922190ea027e0fb6f6d9d3b59880/tests/test_extension_telnet.py#L46), which replaces a zero-argument bound method with `dict`. This PR correctly infers `TelnetConsole` where the result was previously `Unknown`, revealing an orthogonal, pre-existing limitation in ty's instance-method assignment checking. The directly typed assignment is rejected on both the base and PR revisions; the inference change does not introduce that restriction. Compatible class-level method replacement was addressed in [#26158](https://github.com/astral-sh/ruff/pull/26158), while instance-method replacement remains separate work related to [ty#350](https://github.com/astral-sh/ty/issues/350). This is just better type inference exposing an unrelated issue, so we accept it for this PR. Other ecosystem changes are desirable improved inference.
Author
Parents
Loading