[Python-Dev] Adding Type[C] to PEP 484 (original) (raw)
Guido van Rossum gvanrossum at gmail.com
Sun May 15 13:30:05 EDT 2016
- Previous message (by thread): [Python-Dev] Adding Type[C] to PEP 484
- Next message (by thread): [Python-Dev] Adding Type[C] to PEP 484
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Right. I still have no good intuition for what Type[BasicUser, ProUser] would mean so I think you should be required to use the Union, which is clear. Type[] should only allow one parameter.
Most real uses of Type[] use a type variable anyway.
I really recommend that you read through https://github.com/python/typing/issues/107 even though it's long and meanders a bit; it comes to the right conclusion in the end.
On Sun, May 15, 2016 at 10:24 AM, Peter Ludemann <pludemann at google.com> wrote:
On 14 May 2016 at 13:28, Guido van Rossum <gvanrossum at gmail.com> wrote: On Sat, May 14, 2016 at 11:47 AM, Peter Ludemann <pludemann at google.com> wrote:
I think that Type[C,D] would mean Union[Type[C], Type[D]], but I'm not sure ... I should probably talk to a typing expert about this. (Sorry for thinking out loud; but if we decide that Type[C,D] doesn't make sense, we need to prohibit it)
It sounds iffy enough to prohibit. I suppose I'm asking: do we allow newuser(Type[BasicUser, ProUser])? That looks like a call, but Type[] is something to use in the definition. Here's a trivial example that off the top of my head makes sense: BasicOrProUser = TypeVar('BasicOrProUser', BasicUser, ProUser) def newuser(name: str, userfactory: Type[BasicOrProuser]) -> BasicOrProUser: return userfactory(name) That looks reasonable and only has one parameter to Type[]. So, should we allow this (by analogy to TypeVar allowing multiple types): def newuser(name: str, userfactory: Type[BasicUser, ProUser]) -> Union[BasicUser, ProUser]: return userfactory(name) ? ... or should it be: def newuser(name: str, userfactory: Type[ Union[ BasicUser, ProUser ] ]) -> Union[BasicUser, ProUser]: return userfactory(name) ? Although both of these lose the information that the output type is related to the input type; and hopefully the verbosity would encourage people to use TypeVar. (From these examples, my preference is for Type to be limited to a single choice and anything more complicated should be done with TypeVar, Union, etc.)
Or must the Type[...] item have been defined with a TypeVar(..., bound=...), in which case multiple types aren't allowed with Type[...]? You can use whatever you want. If you want to write Type[int] go right ahead. Even Type[Union[int, str]] should be allowed. There is usually a good reason to use a type variable though -- without one the return type would be the base class. (But then again maybe that could be what you want, or you might not even have a return type, e.g. when the thing just gets thrown into the database.) --Guido
On 14 May 2016 at 11:30, Guido van Rossum <gvanrossum at gmail.com> wrote: What would Type[C, D] mean? --Guido (mobile) On May 14, 2016 11:21 AM, "Peter Ludemann via Python-Dev" <_ _python-dev at python.org> wrote: Is Type[C,D] allowed? Or should multiple types be restricted to TypeVar?
Python-Dev mailing list Python-Dev at python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/guido%40python.org
-- --Guido van Rossum (python.org/~guido)
-- --Guido van Rossum (python.org/~guido) -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20160515/9c8c75ea/attachment.html>
- Previous message (by thread): [Python-Dev] Adding Type[C] to PEP 484
- Next message (by thread): [Python-Dev] Adding Type[C] to PEP 484
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]