[Python-ideas] Adding Type[C] support to PEP 484 and typing.py
Pavol Lisy
pavol.lisy at gmail.com
Thu May 12 17:51:53 EDT 2016
2016-05-12 20:49 GMT+02:00, Guido van Rossum <guido at python.org>:
> There are some subtleties, e.g. in the above example we would actually like
> to know that the return type varies with the argument type:
>
> joe = new_user(ProUser) # Really, joe is a ProUser, not just a User
>
> This can be done using a type variable, e.g.
>
> U = TypeVar('U', bound=User)
> def new_user(user_class: Type[U]) -> U: ...
> joe = new_user(ProUser)
It is very probably I dont understand. Is U here same type (=ProUser)
for parameter and for return?
Because in PEP484 is written:
-> CT = TypeVar('CT', bound=Comparable)
->
-> def min(x: CT, y: CT) -> CT:
-> if x < y:
-> return x
-> else:
-> return y
->
-> min(1, 2) # ok, return type int
-> min('x', 'y') # ok, return type str
->
->(Note that this is not ideal -- for example ``min('x', 1)`` is invalid
->at runtime but a type checker would simply infer the return type
->``Comparable``. Unfortunately, addressing this would require
->introducing a much more powerful and also much more complicated
->vconcept, F-bounded polymorphism. We may revisit this in the future.)
which I understand that CT could be different type for x (=str) and y (=int).
More information about the Python-ideas
mailing list