[Python-Dev] Please reject or postpone PEP 526

Koos Zevenhoven k7hoven at gmail.com
Sun Sep 4 10:34:30 EDT 2016


On Sun, Sep 4, 2016 at 3:43 PM, Steven D'Aprano <steve at pearwood.info> wrote:
[...]
> [steve at ando ~]$ cat test.py
> from typing import Optional
>
> def foo(x:Optional[int])->int:
>     if x is None:
>         return -1
>     return x + 1
>
> def bar(x:Optional[int])->int:
>     y = x  # the type of y must be inferred
>     if y is None:
>         return y + 1
>     return len(y)
>
> [steve at ando ~]$ mypy --strict-optional test.py
> test.py: note: In function "bar":
> test.py:11: error: Unsupported operand types for + (None and "int")
> test.py:12: error: Argument 1 to "len" has incompatible type "int"; expected "Sized"
>
>
> foo passes the type check; bar fails.
>

That's great. While mypy has nice features, these examples have little
to do with PEP 526 as they don't have variable annotations, not even
using comments.

For some reason, pip install --upgrade mypy fails for me at the
moment, but at least mypy version 0.4.1 does not allow this:

    from typing import Callable

    def foo(cond: bool, bar : Callable, baz : Callable) -> float:
        if cond:
            x = bar() # type: int
        else:
            x = baz() # type: float
        return x / 2

and complains that

test.py:7: error: Name 'x' already defined". Maybe someone can confirm
this with a newer version.

Here,

    def foo(cond: bool) -> float:
        if cond:
            x = 1
        else:
            x = 1.5
        return x / 2

you get a different error:

test.py:5: error: Incompatible types in assignment (expression has
type "float", variable has type "int")

Maybe someone can confirm this with a newer version, but IIUC this is
still the case.

>> I want a checker to check my code and, with minimal annotations, give me
>> confidence that my code is correct
>
> Don't we all.
>

I would add *with minimal restrictions on how the code is supposed to
be written* for type checking to work. It's not at all obvious that
everyone thinks that way. Hence, the "Semantics for type checking"
thread on python-ideas.

-- Koos

>
>
> --
> Steve
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: https://mail.python.org/mailman/options/python-dev/k7hoven%40gmail.com



-- 
+ Koos Zevenhoven + http://twitter.com/k7hoven +


More information about the Python-Dev mailing list