[Python-ideas] Trial balloon: adding variable type declarations in support of PEP 484

Guido van Rossum guido at python.org
Thu Aug 4 12:27:34 EDT 2016


On Thu, Aug 4, 2016 at 12:16 AM, Pavol Lisy <pavol.lisy at gmail.com> wrote:
> On 8/4/16, Guido van Rossum <guido at python.org> wrote:
>> On Wed, Aug 3, 2016 at 3:02 PM, Pavol Lisy <pavol.lisy at gmail.com> wrote:
>
>>> def fnc():
>>>   global a: list
>>
>> I'm not proposing to add such syntax, and the right place for the type
>> of a would be at the global level, not on the `global` syatement.
>>
>>> a = 7
>>> fnc()
>>> a = [1, 2, 3]  # and this could be interesting for static type-checker
>>> too
>>
>> Indeed, but that's not what we're debating here.
>
> Sorry but for me is really important where we are going (at least as
> inspiration).

But you are thinking with your runtime hat on. To a static type
checker, the call to fnc() is irrelevant to the type of the global
variable `a`. It's like writing

def foo():
    return int
def bar(a: foo()):
    return a+1

The static type checker rejects `foo()` as an invalid type, even
though you know what it means at runtime. (But what if foo() were to
flip a coin to choose between int and str?)

> As I understand now this changes could end in code which could be pure
> python and on other hand also compilable by static typed compiler too.
> Maybe it is what we wanted (I prefer this one), maybe we doesnt care
> and maybe we want to avoid it.
>
> For example Cython's code:
>   cdef int n
>
> could be writen:
>   n: cdef.int
>
> or
>   n: 'cdef int'
>
> and I think it could be good to see convergence here.

Yes, that should be possible. Just don't run mypy over that source code. :-)

> And maybe this
>     cdef int n, k, i
>
> could be inspiring too allow one to many possibility
>   n, j, k: int   # (1)
>
> I think it is natural and better than
>   n, j, k: int, int, int
>
> But (1)  would be discordant with this (from PEP484)
>   def __init__(self, left: Node, right: Node)

And that's why I really don't want to go there. What if someone wrote

T = Tuple[int, int, str]
a, b, c: T

Do we now have three Tuple[int, int, str] variables, or two ints and a str?

-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-ideas mailing list