[ty] Retain the function-like-ness of Callable types when binding self#21614
Merged
AlexWaygood merged 2 commits intomainfrom Nov 24, 2025
Merged
[ty] Retain the function-like-ness of Callable types when binding self#21614AlexWaygood merged 2 commits intomainfrom
Callable types when binding self#21614AlexWaygood merged 2 commits intomainfrom
Conversation
Diagnostic diff on typing conformance testsNo changes detected when running ty on typing conformance tests ✅ |
|
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
invalid-assignment |
0 | 2 | 0 |
possibly-missing-attribute |
2 | 0 | 0 |
unresolved-attribute |
0 | 2 | 0 |
| Total | 2 | 4 | 0 |
2d90c24 to
cd635e8
Compare
Member
Author
|
The ecosystem diff shows two false-positive diagnostics going away. It also shows two pre-existing diagnostics becoming slightly more confusing due to the fact that we don't have a great way of distinguishing between function-like callables and non-function-like callables in our |
8023aaa to
70c526e
Compare
70c526e to
e204348
Compare
carljm
added a commit
to mtshiba/ruff
that referenced
this pull request
Nov 25, 2025
* main: [ty] Extend Liskov checks to also cover classmethods and staticmethods (astral-sh#21598) Dogfood ty on the `scripts` directory (astral-sh#21617) [ty] support generic aliases in `type[...]`, like `type[C[int]]` (astral-sh#21552) [ty] Retain the function-like-ness of `Callable` types when binding `self` (astral-sh#21614) [ty] Distinguish "unconstrained" from "constrained to any type" (astral-sh#21539) Disable ty workspace diagnostics for VSCode users (astral-sh#21620) [ty] Double click to insert inlay hint (astral-sh#21600) [ty] Switch the error code from `unresolved-attribute` to `possibly-missing-attribute` for submodules that may not be available (astral-sh#21618) [ty] Substitute for `typing.Self` when checking protocol members (astral-sh#21569) [ty] Don't suggest things that aren't subclasses of `BaseException` after `raise` [ty] Add hint about resolved Python version when a user attempts to import a member added on a newer version (astral-sh#21615)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
For something like this:
we will currently infer the type of
MyClass.methodas a function-likeCallable, but we will infer the type ofMyClass().methodas aCallablethat is not function-like. That's because aCallableTypecurrently "forgets" whether it was function-like or not during thebound_selftransformation:ruff/crates/ty_python_semantic/src/types.rs
Lines 10985 to 10987 in a57e291
This seems incorrect, and it's quite different to what we do when binding the
selfparameter ofFunctionLiteraltypes:BoundMethodtypes are all seen as subtypes of function-likeCallablesupertypes -- here'sBoundMethodType::into_callable_type:ruff/crates/ty_python_semantic/src/types.rs
Lines 10844 to 10860 in a57e291
The bug here is also causing lots of false positives in the ecosystem report on #21611: a decorated method on a subclass is currently not seen as validly overriding an undecorated method with the same signature on a superclass, because the undecorated superclass method is seen as function-like after binding
selfwhereas the decorated subclass method is not.Fixing the bug required adding a new API in
protocol_class.rs, because it turns out that for our purposes in protocol subtyping/assignability, we really do want a callable type to forget its function-like-ness when bindingself.I initially tried out this change without changing anything in
protocol_class.rs. However, it resulted in many ecosystem false positives and new false positives on the typing conformance test suite. This is because it would mean that no protocol with a__call__method would ever be seen as a subtype of aCallabletype, since the__call__method on the protocol would be seen as being function-like whereas theCallabletype would not be seen as function-like.Test Plan
Added an mdtest that fails on
main