[Python-Dev] PEP 526 ready for review: Syntax for Variable and Attribute Annotations (original) (raw)
Koos Zevenhoven k7hoven at gmail.com
Thu Sep 1 13:01:23 EDT 2016
- Previous message (by thread): [Python-Dev] PEP 526 ready for review: Syntax for Variable and Attribute Annotations
- Next message (by thread): [Python-Dev] PEP 526 ready for review: Syntax for Variable and Attribute Annotations
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Thu, Sep 1, 2016 at 5:46 PM, Guido van Rossum <guido at python.org> wrote:
On Thu, Sep 1, 2016 at 6:11 AM, Koos Zevenhoven <k7hoven at gmail.com> wrote:
While a large amount of Python programmers may not be interested in type hinting local variables inside functions, I can see other potential benefits in this. IOW, PEP 3157 is not dead yet. Indeed.
PEP 3157? Is that a typo or is there such a thing somewhere?
[...]
Also, when reading code, it may be hard to tell which (instance) attributes the class implements. To have these listed in the beginning of the class could therefore improve the readability. Right. That has been my observation using PEP 484's type comments extensively for annotating instance variables at the class level. E.g. much of mypy's own code is written this way, and it really is a huge help. But foo = None # type: List[int] while it gives me the info I'm looking for, is not great notation-wise, and that's why I started thinking about an alternative: foo: List[int] (in either case, the init contains something like
self.foo = []
).In this light, I'm not sure it's a good idea to allow attribute type hints inside methods. Those are meant for the coding style where all attributes are initialized in the method and people just want to add annotations there. This is already in heavy use in some PEP-484-annotated code bases I know of, using # type comments, and I think it will be easier to get people to switch to syntactic annotations if they can mechanically translate those uses. (In fact we are planning an automatic translator.)
I suppose the translator would be somewhat more complicated if it were to move the type hints to the beginning of the class suite. Anyway, I hope there will at least be a recommendation somewhere (PEP 8?) to not mix the two styles of attribute annotation (beginning of class / in method). The whole readability benefit turns against itself if there are some non-ClassVar variables annotated outside init and then the rest somewhere in init and in whatever initialization helper methods init happens to call.
[...]
Note that we could then also have this:
def NAME Which would, again for readability (see above), be a way to express that "there is an instance variable called X, but no type hint for now". I can't think of a good way to do this with the keyword-free version for people that don't use type hints. And then there could also be a simple decorator like @slottedattributes that automatically generates "slots" from the annotations. This I like, or something like it. It can be a follow-up design. (I.e. a separate PEP, once we have experiece with PEP 526.)
I think there should be a syntax for this that does not involve type hints, but I can't seem to come up with anything that works with the keyword-free version :(.
-- Koos
-- --Guido van Rossum (python.org/~guido)
--
- Koos Zevenhoven + http://twitter.com/k7hoven +
- Previous message (by thread): [Python-Dev] PEP 526 ready for review: Syntax for Variable and Attribute Annotations
- Next message (by thread): [Python-Dev] PEP 526 ready for review: Syntax for Variable and Attribute Annotations
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]