<div dir="ltr"><div><div><div><div>I need to cut this subthread short, but basically, for a successful type check, the expressions used in annotations *cannot* be arbitrary expressions. The type checker has a special understanding of things defined int 'typing', and it knows e.g. what typing.Union means. It supports simple aliases through assignments, so that you can say e.g.<br><br></div><div>import typing<br></div>U = typing.Union<br></div>def foo(U[int, str]) -> str: ...<br><br></div>but it does not understand arbitrary call chaines, so that e.g. this does *not* work:<br><br></div>import typing<br>def u(a, b): return typing.Union[a, b]<br>def foo(u(int, str)) -> str: ...<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jan 21, 2015 at 10:27 AM, Matt <span dir="ltr"><<a href="mailto:boehm.matthew@gmail.com" target="_blank">boehm.matthew@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><span class=""><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote">I think that since type hints have to be analyzed at compile time,<br>
that execution of arbitrary Python code is a non-starter.</blockquote><div><br></div></span><div>From the way the abstract is worded, it sounds like arbitrary expressions could be used: <br></div> </div><span class=""><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote">Abstract base classes, types available in the ``types`` module, and<br>user-defined classes may be used as type hints as well. Annotations<br>must be valid expressions that evaluate without raising exceptions at<br>the time the function is defined</blockquote><div><br></div></span><div>I had assumed that this means that the annotation is any arbitrary python expression that evaluates to one of these things. Even if the syntax is more limited (as I guess it must be), I assume that Union and generics work not because the static analyzers know anything special about Union, but that typing.Union is a python class and could have just as easily been defined outside of the standard library. Even if function calls do not work, I imagine people can somehow abuse generics to capture documentation or other things inside type hints.<br></div><div><br>As an example, let's say a function expects a positive integer. A
programmer could achieve this with an assert statement near the top of
the function, a decorator, or by making users pass in a PositiveInt
object (subclass of int). However, they may prefer to annotate the
function as Type[int, Positive]. Perhaps a better example would be
something like Type[FilePath, exists, readable]<br><br>I like the idea of keeping annotation syntax very simple and primarily
about static type checking, but if it's true that people can experiment
with other uses for function annotations without violating the current
syntax, then it sounds like everybody wins.<br></div><div><br></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jan 21, 2015 at 11:45 AM, Skip Montanaro <span dir="ltr"><<a href="mailto:skip.montanaro@gmail.com" target="_blank">skip.montanaro@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span>On Wed, Jan 21, 2015 at 10:38 AM, Matt <<a href="mailto:boehm.matthew@gmail.com" target="_blank">boehm.matthew@gmail.com</a>> wrote:<br>
> What do people think about allowing the annotation to be a function that<br>
> returns the type? Is this too complicated to understand/implement?<br>
<br>
</span>I think that since type hints have to be analyzed at compile time,<br>
that execution of arbitrary Python code is a non-starter. Smarter<br>
folks than me may well see a way this could happen though.<br>
<span><font color="#888888"><br>
Skip<br>
</font></span></blockquote></div><br></div>
</div></div><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></blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido">python.org/~guido</a>)</div>
</div>