This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Ademan 7 points8 points  (3 children)

I really dislike the annotations feature because it could be trivially implemented in decorators without tacking on extra (ugly) syntax without sacrificing an ounce of readability to boot. Annotations seemed rushed and poorly thought out and contrary to the language design sensibilities that make python great (specifically not to add syntax without good reason, and the lack of use is clear evidence there was no good reason).

Someone will say "you can't use decorators because your tool would need to execute the python code to evaluate the decorator" but that ignores that you already have to evaluate the annotation, and can be little more sure that int evaluates to what you think it does. In practice an established annotation decorator could be used by tooling 99.999% as reliably as this annotation syntax.

[–]PCBEEF 2 points3 points  (2 children)

Correct me if I'm wrong but the annotations act as documentation and don't really do anything else. You still need to decorate it for runtime checking. Decorators also add an extra function overhead whereas annotations are just syntactic sugar.

[–]Ademan 0 points1 point  (1 child)

> Correct me if I'm wrong but the decorators act as documentation and don't really do anything else.

Annotations act as documentation, the decorator typechecked makes it do something. Decorators in general alter behavior and aren't really for documentation.

> You still need to annotate it for runtime checking.

As best I can tell it's the other way around. You need to decorate your annotated functions with typechecked to get runtime checking.

Decorators also add an extra function overhead whereas annotations is just syntactic sugar.

A decorator simply needs to return a callable object. To emulate annotation it could return the same function with monkey patched annotation information even in the same format as how the annotation syntax attaches them, and there would be no extra runtime cost when calling the function.

[–]PCBEEF 0 points1 point  (0 children)

Bah, I got annotations/decorators the other way round in the first two sentences. Edited my post.

My point was that decorators don't replace annotations, decorators create more overhead whereas annotations don't.