[red-knot] Add CallableTypeFromFunction special form#16683
Conversation
|
|
More of a high-level comment. I'm not completely convinced that we need a special form in def signature_of[*Ts, R](c: Callable[[*Ts], R]) -> Callable[[*Ts], R]:
return cwhich could be used like this. |
This approach can still only express callable types that can be spelled with I think a more promising way to (partially?) obviate the need for this special form is "callable Protocols" (that is, a Protocol that defines only a Given the simplicity of this special form, and the fact that we can use it today to help us test callable type relations more thoroughly, I'm still in favor of landing it. We can always consider removing it later (I guess barring the possibility that it sees wide external adoption, but this is unlikely since it doesn't exist at runtime, and if it does see wide adoption despite that, it suggests that it is useful and shouldn't be removed.) |
|
Additionally, this will also be useful to test out assignability because |
Summary
This PR adds a new
CallableTypeFromFunctionspecial form to allow extracting the abstract signature of a function literal i.e., convert aType::Functioninto aType::Callable(CallableType::General).This is done to support testing the
is_gradual_equivalent_totype 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 toCallable[[Any, Any, ...], Any]where the number of parameters should match between the function literal and callable type.Refer #16634 (comment)
Bikeshedding
The name
CallableTypeFromFunctionis a bit too verbose. A possibly alternative from Carl isCallableTypeOfbut that would be similar toTypeOfalbeit with a limitation that the former only accepts function literal types and errors on other types.Some other alternatives:
FunctionSignatureSignatureOf(similar issues asTypeOf?)Test Plan
Update
type_api.mdwith a new section that tests this special form, both invalid and valid forms.