Replies: 1 comment 2 replies
-
You are looking for a Map operator on types. An older version of typevartuple pep had map operator and it would have let last approach work ignoring keyword argument support. Something like, NewT = Union[T, ObjectRef[T]] Callable[[Unpack[Ts]], R] -> Callable[[Unpack[Map[NewT, Ts]]], R] Map takes a generic type/alias as first argument and then applies it over each type in typevartuple. A Map type operator that works on paramspecs in theory could also be proposed. At moment though the current type system does not support this. My guess is eventually TypeVarTuple Map will be proposed as reason it was dropped was to simplify pep 646 and leave it as a future follow up. I’m unaware of any plan to support Map on paramspec keyword arguments. |
Beta Was this translation helpful? Give feedback.
-
I want to be able to annotate something that takes a function and creates a new one that alters the argument types it accepts.
A quick example, using Ray:
So,
do_things()
anddo_more_things()
should keep their signature. But theirremote
counterparts should have as signature that allows the original types or a wrapperObjectRef[OriginalType]
for each argument:Now, I know type annotating this in this particular API design is probably difficult, and possibly not easily supported. I was thinking of an alternative API that could take advantage of
ParamSpec
,TypeVar
s withoverload
s and/orTypeVarTuple
. But I still can't find a way to get all the desired features together.Wanted features
remote
) counterpart, for keyword arguments.remote
) version to accept keyword arguments and validate its types, with the extended/modified type acceptingObjectRef[X]
.Alternatives
I have been playing around with alternative code interfaces/APIs to achieve this.
ParamSpec
For example with
ParamSpec
and a wrapper function that doesn't create a new attribute:And with this approach it could be inlined, which is what I would expect would be the common usage pattern, this allows type checks (inline errors) and autocompletion in editors:
But this approach doesn't support extending the types of the arguments with
ObjectRef[X]
.TypeVar
s andoverload
sAnother alternative is with
TypeVar
s andoverload
, this enables type checks, extending the types, but it no longer supports keyword arguments (nor autocompletion for them in editors):TypeVarTuple
I also tried experimenting with
TypeVarTuple
:But the end result is both of the two problems above combined 😔 , I can't update the new type arguments in the resulting function and it doesn't support keyword arguments.
Questions
I would like to have both of these main features combined:
ObjectRef
versionBoth of the two first alternatives achieve almost all of the things I would want. I also tried using overloads combining both ideas, but only one of the signatures would be taken into account. So there's always one of these two main features missing.
I'm not even sure the title of the discussion is right or what is the right term for this. I've been trying to find the solution for a couple of days in the discussions and issues here, the typing mailing list, the Pyright discussions, etc. But I didn't find any previous discussions around it.
Is there any way to achieve annotating types in some way that solve this problem? Am I missing something else?
Edit 2022-04-29
Assuming this is currently not possible, what would be needed to make it possible? Would some way of achieving this be acceptable?
And if so, what would be the best approach to make it possible?
I'm not sure what's the process, but maybe there could be a way to sponsor someone with the right expertise here to tackle it, I imagine it would require a PEP, work on mypy, not sure what else, but maybe I'm being naive in some way and it would require a different approach.
Beta Was this translation helpful? Give feedback.
All reactions