[Python-Dev] Please reject or postpone PEP 526
Steven D'Aprano
steve at pearwood.info
Sun Sep 4 09:07:43 EDT 2016
Referring to the alternative syntax forms:
# Proposed
x: int = func(value)
# Already accepted
x = func(value) #type: int
On Sun, Sep 04, 2016 at 11:52:24AM +0100, Mark Shannon wrote:
> The key difference is in placement.
> PEP 484 style
> variable = value # annotation
>
> Which reads to me as if the annotation refers to the value.
Both Guido and the PEP have stated that it doesn't refer to the value,
but to the variable.
But what does it even mean to say that it refers to the value in the
context of *static type-checking*? I know what it means in the context
of dynamic type-checking, but I don't see how that has any relevance to
a static checker.
I have seen a number of people commenting that the comment annotation
"applies to the expression", but I don't understand what this is
supposed to mean. How is that different from applying it to the
variable? (That's not a rhetorical question.) Suppose I write this:
mylist = []
x = False or None or (mylist + [1]) #type: List[int]
pass # stand-in for arbitrary code
x.append("definitely not an int")
Should the type-checker flag the call to x.append as an error? I hope we
all agree that it should.
But it can only do that if it knows the type of the variable `x`. This
is a *static* type-checker, it doesn't know what value x *actually* has
at run-time because it isn't running at run-time. As far as the static
checker is concerned, it can only flag that append as an error if it
knows that `x` must be a list of ints.
If you distinguish the two cases:
"the expression `False or None or (mylist + [1])` is List[int]"
versus:
"the variable `x` is List[int]"
I don't even see what the first case could possible mean. But whatever
it means, if it is different from the second case, then the type-checker
is pretty limited in what it can do.
> PEP 526
> variable: annotation = value
>
> Which reads very much as if the annotation refers to the variable.
Since the PEP makes it clear that the two forms are to be treated the
same, I think that whatever difference you think they have is not
relevant. They are *defined* to mean the same thing.
--
Steve
More information about the Python-Dev
mailing list