[Python-ideas] PEP 484 (Type Hints) -- first draft round

Matt boehm.matthew at gmail.com
Wed Jan 21 17:38:28 CET 2015


What do people think about allowing the annotation to be a function that
returns the type? Is this too complicated to understand/implement?

     import typing
    def Doc(param_doc, param_type=typing.Any):
        #do something with the documentation for the parameter...
        return param_type

    def print_twice(message: Doc("something to print")):
        print(message)
        print(message)

    def Prime():
        return Doc("A prime number", int)

I've done my best to read through the thread/PEP before replying, but
please excuse me if I've missed something and this is a dumb question. If
this is out of the question, perhaps people wishing to encode more
information into function annotations could (ab)use subscripting.

On Wed, Jan 21, 2015 at 2:31 AM, Stefan Behnel <stefan_ml at behnel.de> wrote:

> Guido van Rossum schrieb am 19.01.2015 um 04:40:
> > On Sun, Jan 18, 2015 at 12:36 PM, Stefan Behnel wrote:
> >> would be nice to at least be able to explicitly express somehow that
> >> subtypes should be disallowed.
> >
> > Can you come up with a specific proposal? Maybe we're not as far apart as
> > we think. :-)
>
> Normally, "x: List[int]" would mean that x is either a list or a subtype of
> list, where each item is an int or a subtype. To disallow subtypes, you
> could have something like "exactly(List)[int]" or "List[exactly(int)]" or
> "exactly(List[int])", which would then make it clear that the exact type
> matters, only for the List in the first case, only for the item type in the
> second, and (this is not necessarily required) for all types inside of the
> "exactly()" call in the last case. The return value of "exactly()" could be
> some kind of type wrapper object which marks the wrapped type construct as
> allowing "no subtypes".
>
> This would keep the normal case of allowing subtypes everywhere normal and
> simple, but would allow restricting the type to disallow subtypes in the
> cases where it seems appropriate.
>
> Stefan
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150121/3d46e1fc/attachment.html>


More information about the Python-ideas mailing list