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

Guido van Rossum guido at python.org
Mon Mar 23 20:55:37 CET 2015


On Sun, Mar 22, 2015 at 11:02 PM, David Foster <davidfstr at gmail.com> wrote:

> * It's a bit vague how local variables should be typed in the presence of
>   multiple assignments. Consider:
>
>   ```
>   if x > 0:
>       root = sqrt(x)  # type: int
>   else:
>       root = None
>   ```
>
>   It seems that the type of `root` should be inferred as `Union[int, None]`
>   (AKA `Optional[int]`), but there's no discussion about this type of
>   inference in the PEP.
>
>   Cases like this are the only way that the proposed blank `Undefined`
>   value makes sense to me, using an example like:
>
>   ```
>   root = Undefined    # type: Optional[int]
>   if x > 0:
>       root = sqrt(x)  # type: int
>   else:
>       root = None
>   ```
>

Right. Another use case for Undefined is class variables (serving as
instance variable initialization/declaration).

If you feel strongly that this needs to be mentioned in the PEP, can you
send a PR to github.com/ambv/typehinting ?


> * +1 for only allowing .pyi as the extension for stub files (and not also
> .py).
>   Makes it very clear that they should only be interpreted as stubs.
>

Perhaps. Though there are also downsides.


> * -1 that `Optional[T1]` is unnecessary. It's conceptually more clean and
>   directly corresponds conceptually to types like `int?` and `?int` from
>   other languages.
>

Indeed, Optional[t] is much clearer to the human reader than Union[t, None].


> * +1 for the argument order cast(x, T). This is consistent with (x: T)
> elsewhere.
>

I disagree on this. It goes against the argument order of casts in other
languages, e.g. C, C++, Java, and even in Python -- you write int(x), not
x(int).

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150323/d7a5b334/attachment.html>


More information about the Python-ideas mailing list