What exactly are the proposed semantics of Map?
I was recently trying to write the type for a decorator which is the moral
equivalent of the following:
def preserve_none(fn: Callable[..., T]) -> Callable[..., Optional[T]]:
def wrapper(*args: Any) -> Optional[T]:
if any(arg is None for arg in args):
return None
return fn(*args)
return wrapper
Would I be able to write this as:
def preserve_none(fn: Callable[*Ts, T]) -> Callable[[Map[Optional,
Ts]], Optional[T]]:
def wrapper(*args: *Map[Optional, Ts]) -> Optional[T]:
...
What is the type of Map[Dict, Ts]? Is it an error?
Is there any way to write Map[Dict, Zip[Ks, Vs]]?
— Caleb
On Fri, May 21, 2021 at 12:08 PM Guido van Rossum
Given that much of my *own* excitement for variadics comes from the prospect of typing zip and gather, and that we have some time before this goes into 3.11, I'd be in favor of adding Map[] to PEP 646.
On Fri, May 21, 2021 at 11:46 AM S Pradeep Kumar
wrote: Somehow my brain wants to use a loop construct
This would use the Map operator:
``` def tuples(*possibilities: *Map[Possibility, Ts]) -> Possibility[Tuple[*Ts]]: ...
p1: Possibility[int] p2: Possibility[str] p = tuples(p1, p2) ```
+ Ts will be bound to Tuple[int, str] + That way, Map[Possibility, Ts] === Tuple[Possibility[int], Possibility[str]] + Return type becomes Possibility[Tuple[int, str]]
Or was it put off till a later PEP? (That would be a shame, since this is the first variadic signature I've encountered in the wild in a long time.)
Yeah, this was part of earlier drafts but we deferred this to a later PEP. There were objections since this would require checking that the type argument to Map was a higher-order type like `Possibility` or a generic alias.
We would need this for functions like zip and asyncio.gather as well (but not really for tensor typing). Let me know if it would be better to incorporate this within PEP 646 itself instead of a follow-up PEP.
On Fri, May 21, 2021 at 11:24 AM Guido van Rossum
wrote: I came across a case where I think PEP 646 would provide relief, but I don't know how to spell it.
Suppose we have a class Property[T]: ``` class Property(Generic[T]): ... ``` Now we have a function tuples that takes several Property arguments with different type parameters and returns a Property whose type parameter is a Tuple: ``` def tuples(*possibilities: Possibility[?]) -> Possibility[?]: ``` The idea is that you call ``` p1: Possibility[int] p2: Possibility[str] p = tuples(p1, p2) reveal_type(p) # Possibility[Tuple[int, str]] ``` Somehow I can't figure out how we're supposed to write that with PEP 646. The best I can come up with is ``` def tuples(*possibilities: *Possibility[*Ts]) -> Possibility[Tuple[*Ts]]:` ``` but the first `*Ts` feels wrong -- it looks as if Possibility takes multiple type parameters, but that's not the case. Somehow my brain wants to use a loop construct, e.g. ``` def tuples(*possibilities: *Possibility[T] for T in Ts) -> Possibility[Tuple[*Ts]]: ``` Does anyone recall how this is supposed to be handled? Or was it put off till a later PEP? (That would be a shame, since this is the first variadic signature I've encountered in the wild in a long time.)
PS. If you really want to know: https://github.com/DRMacIver/minithesis/pull/9
-- --Guido van Rossum (python.org/~guido) *Pronouns: he/him **(why is my pronoun here?)* http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-c... _______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: gohanpra@gmail.com
-- S Pradeep Kumar
-- --Guido van Rossum (python.org/~guido) *Pronouns: he/him **(why is my pronoun here?)* http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-c... _______________________________________________ Typing-sig mailing list -- typing-sig@python.org To unsubscribe send an email to typing-sig-leave@python.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: donovick@cs.stanford.edu