You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Apologies if this has already been asked/solved - I can't figure out exactly what this is called so it's very difficult to search for.
I'm writing a simple strongly typed dependency system, based on function closures. I have a decorator function that has this typing:
# This is a stand-in for a full class implementation that has a __call__ method matching# Callable[P, Awaitable[R]] - I'm just omitting it for brevitytypeCallableObject[**P, R] =Callable[P, Awaitable[R]]
defdependency[**P, R](*args: P.args, **kwargs: P.kwargs) ->Callable[
[Callable[P, Awaitable[R]] |Callable[P, R]], CallableObject
]:
...
This works to a degree, but, I want to transform the arguments in P to also accept functions that return the arguments type or an Awaitable of that type. For example:
defone() ->int:
return1deftwo() ->int:
return2@dependency()defthree(one: int, two: int) ->int:
returnone+two# I want the decorated three to have this signaturedefdecorated_three(one: int|Callable[..., Awaitable[int] |int], two: int|Callable[..., Awaitable[int] |int]) ->int:
...
Essentially, I want to apply a mapping to every arg and kwarg in P that transforms it into P | Callable[..., Awaitable[P] | P]. Is this possible with the current grammar?
As a reference, I wrote an example that does what I'm attempting to do in typescript, where you can unpack what they call "type tuples" using the keyof syntax to mutate their values:
functiondependency<Pextendsunknown[],R>(wrap: (...args: P)=>R): (...args: {[VinkeyofP]: P[V]|((...args: any)=>P[V]|Promise<P[V]>)})=>R{returnwrapasany}functionone(): number{return1}functiontwo(): number{return2}functionthree(one: number,two: number): number{returnone+two}// Has type: const decorated_three: (one: number | ((...args: any) => number | Promise<number>), two: number | ((...args: any) => number | Promise<number>)) => number;constdecorated_three=dependency(three)
(Playground where you can see / verify the typing works as expected)
Apologies for dragging in another type system/language - I'm just looking for the python equivalent (if it exists).
Appreciate the help in advance. If this is a duplicate, please close it and mark it with the correct issue - I just couldn't find it 😅
The text was updated successfully, but these errors were encountered:
No, there's currently no equivalent of mapped types in the Python type system. There has been some discussion about adding them, but nothing has been formally spec'ed yet.
Thanks, that's very helpful (albeit a little disappointing). Can you point me at where the discussion is happening (if it's online) so I can keep an eye on that?
It has come up a few times... Early versions of PEP 646 proposed a Map operator that worked only on TypeVarTuple. More recently, this discussion thread talked about it in terms of TypedDict. I don't think anyone has thought about how it would apply to ParamSpecs. Function signatures in Python are significantly more complex than they are in TypeScript (e.g. parameters can be positional-only, keyword-only, or a combination of the two), so simply copying TypeScript's behavior won't be possible here.
Apologies if this has already been asked/solved - I can't figure out exactly what this is called so it's very difficult to search for.
I'm writing a simple strongly typed dependency system, based on function closures. I have a decorator function that has this typing:
This works to a degree, but, I want to transform the arguments in
P
to also accept functions that return the arguments type or anAwaitable
of that type. For example:Essentially, I want to apply a mapping to every arg and kwarg in
P
that transforms it intoP | Callable[..., Awaitable[P] | P]
. Is this possible with the current grammar?As a reference, I wrote an example that does what I'm attempting to do in typescript, where you can unpack what they call "type tuples" using the
keyof
syntax to mutate their values:(Playground where you can see / verify the typing works as expected)
Apologies for dragging in another type system/language - I'm just looking for the python equivalent (if it exists).
Appreciate the help in advance. If this is a duplicate, please close it and mark it with the correct issue - I just couldn't find it 😅
The text was updated successfully, but these errors were encountered: