-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[red-knot] Check gradual equivalence between callable types #16634
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
Conversation
5e2ccec to
26676b7
Compare
|
sharkdp
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! I guess this (and the other similar PRs) will probably need some adjustment rebased on top of @dcreager's overload support.
26676b7 to
dffee4f
Compare
carljm
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great!
## 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
|
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:
|
|
Oh, that's simple to fix. I don't need to explicitly check whether both signatures has |
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.