-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Extend Liskov checks to cover cases where methods are incompatibly overridden by non-methods #21611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
AlexWaygood
wants to merge
2
commits into
main
Choose a base branch
from
alex/liskov-method-overridden-by-non-method
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
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
Diagnostic diff on typing conformance testsChanges were detected when running ty on typing conformance tests--- old-output.txt 2025-11-25 10:33:16.881497031 +0000
+++ new-output.txt 2025-11-25 10:33:20.774498275 +0000
@@ -1,4 +1,4 @@
-fatal[panic] Panicked at /home/runner/.cargo/git/checkouts/salsa-e6f3bb7c2a062968/17bc55d/src/function/execute.rs:469:17 when checking `/home/runner/work/ruff/ruff/typing/conformance/tests/aliases_typealiastype.py`: `infer_definition_types(Id(1a6a3)): execute: too many cycle iterations`
+fatal[panic] Panicked at /home/runner/.cargo/git/checkouts/salsa-e6f3bb7c2a062968/17bc55d/src/function/execute.rs:469:17 when checking `/home/runner/work/ruff/ruff/typing/conformance/tests/aliases_typealiastype.py`: `infer_definition_types(Id(1baa3)): execute: too many cycle iterations`
_directives_deprecated_library.py:15:31: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `int`
_directives_deprecated_library.py:30:26: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `str`
_directives_deprecated_library.py:36:41: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `Self@__add__`
|
|
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
invalid-method-override |
716 | 6 | 0 |
unused-ignore-comment |
0 | 147 | 0 |
| Total | 716 | 153 | 0 |
CodSpeed Performance ReportMerging #21611 will degrade performances by 25.02%Comparing Summary
Benchmarks breakdown
|
b6b421c to
5c2ba94
Compare
fb40c27 to
6e9b58d
Compare
|
5c2ba94 to
29ea12a
Compare
6e9b58d to
b06682a
Compare
AlexWaygood
added a commit
that referenced
this pull request
Nov 24, 2025
…self` (#21614) ## Summary For something like this: ```py from typing import Callable def my_lossy_decorator(fn: Callable[..., int]) -> Callable[..., int]: return fn class MyClass: @my_lossy_decorator def method(self) -> int: return 42 ``` we will currently infer the type of `MyClass.method` as a function-like `Callable`, but we will infer the type of `MyClass().method` as a `Callable` that is _not_ function-like. That's because a `CallableType` currently "forgets" whether it was function-like or not during the `bound_self` transformation: https://github.com/astral-sh/ruff/blob/a57e29131125bf05db7379e90c7616eec32624fe/crates/ty_python_semantic/src/types.rs#L10985-L10987 This seems incorrect, and it's quite different to what we do when binding the `self` parameter of `FunctionLiteral` types: `BoundMethod` types are all seen as subtypes of function-like `Callable` supertypes -- here's `BoundMethodType::into_callable_type`: https://github.com/astral-sh/ruff/blob/a57e29131125bf05db7379e90c7616eec32624fe/crates/ty_python_semantic/src/types.rs#L10844-L10860 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 `self` whereas 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 binding `self`. 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 a `Callable` type, since the `__call__` method on the protocol would be seen as being function-like whereas the `Callable` type would not be seen as function-like. ## Test Plan Added an mdtest that fails on `main`
29ea12a to
40a9abf
Compare
b06682a to
94541ac
Compare
…ly overridden by non-methods
94541ac to
bff97d0
Compare
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
Test Plan