On 02/09/16 19:19, Steven D'Aprano wrote:
On Fri, Sep 02, 2016 at 10:47:41AM -0700, Steve Dower wrote:
"I'm not seeing what distinction you think you are making here. What
distinction do you see between:

    x: int = func(value)

and

    x = func(value)  #type: int"

Not sure whether I agree with Mark on this particular point, but the
difference I see here is that the first describes what types x may
ever contain, while the latter describes what type of being assigned
to x right here. So one is a variable annotation while the other is an
expression annotation.

Ultimately Python is a dynamically typed language, and that's not
changing. This means types are fundamentally associated with *values*,
not *variables* (names). But in practice, you can go a long way by
pretending that it is the variable that carries the type. That's the
point of the static type checker: if you see that x holds an int here,
then assume (unless told differently) that x should always be an int.
Because in practice, most exceptions to that are due to bugs, or at
least sloppy code.

Of course, it is up to the type checker to decide how strict it wants to
be, whether to treat violations as a warning or a error, whether to
offer the user a flag to set the behaviour, etc. None of this is
relevant to the PEP. The PEP only specifies the syntax, leaving
enforcement or non-enforcement to the checker, and it says:

    PEP 484 introduced type hints, a.k.a. type annotations. While its
    main focus was function annotations, it also introduced the notion
    of type comments to annotate VARIABLES [emphasis added]

If I recall, Guido and I agreed to differ on that point. We still do, it seems. We did manage to agree on the syntax though.


not expressions. And:

    This PEP aims at adding syntax to Python for annotating the types
    of variables and attributes, instead of expressing them through
    comments

which to me obviously implies that the two ways (type comment, and
variable type hint) are intended to be absolutely identical in
semantics, at least as far as the type-checker is concerned.

The key difference is in placement.
PEP 484 style
variable = value # annotation

Which reads to me as if the annotation refers to the value.
PEP 526
variable: annotation = value

Which reads very much as if the annotation refers to the variable.
That is a change in terms of semantics and a change for the worse, in terms of expressibility.

Cheers,
Mark.

_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to