PEP 526 - var annotations and the spirit of python

Gregory Ewing greg.ewing at canterbury.ac.nz
Thu Jul 5 01:34:55 EDT 2018


Steven D'Aprano wrote:

> but the type checker should infer that if you assign None to a variable 
> which is declared int, you must have meant Optional[int] rather than just 
> int.

This seems to be equivalent to saying that *all* types are
Optional, in which case what point is there in having Optional
at all?

> Indeed, that's often the best way, except for the redundant type hint, 
> which makes you That Guy:
> 
>     x: int = 0  # set x to the int 0

But you've shown in an earlier example that such a hint is
*not* always redundant, e.g.

    x = 0
    x = 2.5

is legal and useful, whereas

    x: int = 0
    x = 2.5

ought to be a type error.

-- 
Greg



More information about the Python-list mailing list