[red-knot] Check gradual equivalence between callable types#16634
Merged
dhruvmanila merged 2 commits intomainfrom Mar 13, 2025
Merged
[red-knot] Check gradual equivalence between callable types#16634dhruvmanila merged 2 commits intomainfrom
dhruvmanila merged 2 commits intomainfrom
Conversation
5e2ccec to
26676b7
Compare
19 tasks
Contributor
|
sharkdp
approved these changes
Mar 11, 2025
Contributor
sharkdp
left a comment
There was a problem hiding this comment.
Apart for the one question I had, this looks good to me. Thank you.
crates/red_knot_python_semantic/resources/mdtest/type_properties/is_gradual_equivalent_to.md
Outdated
Show resolved
Hide resolved
26676b7 to
dffee4f
Compare
dhruvmanila
added a commit
that referenced
this pull request
Mar 13, 2025
## Summary This PR adds a new `CallableTypeFromFunction` special form to allow extracting the abstract signature of a function literal i.e., convert a `Type::Function` into a `Type::Callable` (`CallableType::General`). This is done to support testing the `is_gradual_equivalent_to` type relation specifically the case we want to make sure that a function that has parameters with no annotations and does not have a return type annotation is gradual equivalent to `Callable[[Any, Any, ...], Any]` where the number of parameters should match between the function literal and callable type. Refer #16634 (comment) ### Bikeshedding The name `CallableTypeFromFunction` is a bit too verbose. A possibly alternative from Carl is `CallableTypeOf` but that would be similar to `TypeOf` albeit with a limitation that the former only accepts function literal types and errors on other types. Some other alternatives: * `FunctionSignature` * `SignatureOf` (similar issues as `TypeOf`?) * ... ## Test Plan Update `type_api.md` with a new section that tests this special form, both invalid and valid forms.
dffee4f to
9dac249
Compare
Member
Author
|
I found one more case which we need to consider: def foo(*args: Any, **kwargs: Any) -> Any:
pass
static_assert(is_gradual_equivalent_to(CallableTypeFromFunction[foo], Callable[..., Any]))Because, as per the spec:
|
Member
Author
|
Oh, that's simple to fix. I don't need to explicitly check whether both signatures has |
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
Part of #15382
Add support for checking the gradual equivalence between callable types.
A callable type is gradually equivalent to the other callable type:
Noneor the some values are gradually equivalent to each other.Test Plan
Update
is_gradual_equivalent_to.mdwith callable type test cases.Note: I've an explicit goal of updating the property tests with the new callable types once all relations are implemented.