<div dir="ltr"><div><div><div><div><div><div><div>What do people think about allowing the annotation to be a function that returns the type? Is this too complicated to understand/implement?<br></div><div><br>     import typing<br></div>    def Doc(param_doc, param_type=typing.Any):<br></div>        #do something with the documentation for the parameter...<br></div>        return param_type<br></div><div><br></div>    def print_twice(message: Doc("something to print")):<br></div>        print(message)<br></div>        print(message)<br><br><div>    def Prime():<br></div>        return Doc("A prime number", int)<br><br></div>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.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jan 21, 2015 at 2:31 AM, Stefan Behnel <span dir="ltr"><<a href="mailto:stefan_ml@behnel.de" target="_blank">stefan_ml@behnel.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Guido van Rossum schrieb am 19.01.2015 um 04:40:<br>
<span class="">> On Sun, Jan 18, 2015 at 12:36 PM, Stefan Behnel wrote:<br>
>> would be nice to at least be able to explicitly express somehow that<br>
>> subtypes should be disallowed.<br>
><br>
> Can you come up with a specific proposal? Maybe we're not as far apart as<br>
> we think. :-)<br>
<br>
</span>Normally, "x: List[int]" would mean that x is either a list or a subtype of<br>
list, where each item is an int or a subtype. To disallow subtypes, you<br>
could have something like "exactly(List)[int]" or "List[exactly(int)]" or<br>
"exactly(List[int])", which would then make it clear that the exact type<br>
matters, only for the List in the first case, only for the item type in the<br>
second, and (this is not necessarily required) for all types inside of the<br>
"exactly()" call in the last case. The return value of "exactly()" could be<br>
some kind of type wrapper object which marks the wrapped type construct as<br>
allowing "no subtypes".<br>
<br>
This would keep the normal case of allowing subtypes everywhere normal and<br>
simple, but would allow restricting the type to disallow subtypes in the<br>
cases where it seems appropriate.<br>
<div class="HOEnZb"><div class="h5"><br>
Stefan<br>
<br>
<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</div></div></blockquote></div><br></div>