[ty] Ensure that a function-literal type is always equivalent to itself#18227
Merged
AlexWaygood merged 2 commits intomainfrom May 20, 2025
Merged
[ty] Ensure that a function-literal type is always equivalent to itself#18227AlexWaygood merged 2 commits intomainfrom
AlexWaygood merged 2 commits intomainfrom
Conversation
AlexWaygood
commented
May 20, 2025
Member
Author
There was a problem hiding this comment.
The changes in this file are not actually necessary to fix the bug, just a driveby fix for something that I noticed while fixing the bug
Contributor
|
dcreager
reviewed
May 20, 2025
Comment on lines
7167
to
7169
| fn is_equivalent_to(self, db: &'db dyn Db, other: Self) -> bool { | ||
| self.body_scope(db) == other.body_scope(db) | ||
| && self | ||
| .into_callable_type(db) | ||
| .is_equivalent_to(db, other.into_callable_type(db)) | ||
| self.normalized(db) == other.normalized(db) | ||
| } |
Member
There was a problem hiding this comment.
Discussed in person — I think both is_subtype_of and is_equivalent_to should follow the pattern above, using the normalized check as a fast path, but falling back on the body scope + callable check that was there previously.
(I think a lot of this will hopefully be cleaned up as part of astral-sh/ty#462)
dcreager
approved these changes
May 20, 2025
Member
Author
|
1% slowdown on the cold benchmark: https://codspeed.io/astral-sh/ruff/branches/alex%2Fproperty-test-failure. I think that's acceptable here. It might well be addressed when we do astral-sh/ty#462. |
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
This is a short-term fix to some of our type-equivalence logic for function-literal and bound-method types. The issue was that two
Callabletypes will not be considered equivalent if any parameter in either type is unannotated (and it is obviously very common in methods for theselfparameter to be unannotated!). Following 97058e8, equivalence of twoFunctionLiteraltypes defers to equivalence of theCallableTypesupertypes of the two function-literal types.Long-term, we should clarify this situation by splitting
FunctionLiteralinto two separate variants, as described by @dcreager in astral-sh/ty#459 (comment) and astral-sh/ty#462Closes astral-sh/ty#459
Test Plan
cargo test -p ty_python_semanticQUICKCHECK_TESTS=100000 cargo test --release -p ty_python_semantic -- --ignored types::property_tests::stable