ruff
4fe2a2a9 - [ty] Preserve Self when checking universal protocols (#29207)

Commit
26 days ago
[ty] Preserve Self when checking universal protocols (#29207) Protocols declaring a writable `__class__: type[object]` currently are incorrectly considered "equivalent to `object`" and accept arbitrary objects. Each subclass's inherited setter accepts only `type[Self]`, so these protocols are not equivalent to `object`. Check the full protocol against an arbitrary receiver type, preserving receiver-specific `Self` requirements in attributes and methods. Protocol attribute annotations also retain their `Self` binding scope through inheritance and unannotated defaults. Read-only `__class__` protocols and writable contracts accepting only `type[Self]` or `Never` retain their existing behavior. ## Ecosystem report At head [`588c1f8392`](https://github.com/astral-sh/ruff/commit/588c1f8392da077263686f10d41f89c1e5901cf2), all 21 stable scipy-stubs changes are expected: corrected overload selection removes 12 type assertion failures and 7 incorrect deprecation warnings, and adds 2 correct deprecation warnings. No ecosystem regressions found. [Full analysis](https://github.com/astral-sh/ruff/pull/29207#issuecomment-6048924278). ## Ecosystem report At head [`3a0dfbcd01`](https://github.com/astral-sh/ruff/commit/3a0dfbcd01b2eddeb47d42904585dc41dde3457a), all 38 ecosystem changes are expected. Corrected sentinel-overload selection in scipy-stubs fixes result types and deprecation reporting. Binding `Self` in protocol attributes makes Werkzeug's header classes satisfy their protocol; the recovered types expose two optional warnings, including an existing descriptor annotation that omits `None`. No ty regressions were found. [Full analysis](https://github.com/astral-sh/ruff/pull/29207#issuecomment-6048924278).
Author
Parents
Loading