syntax difference

Cameron Simpson cs at cskk.id.au
Sun Jun 17 18:45:40 EDT 2018


On 17Jun2018 11:10, Rick Johnson <rantingrickjohnson at gmail.com> wrote:
>Steven D'Aprano wrote:
>> But the human reader, linters, IDEs and editors can
>> associate it with the name it annotates, and use it as a
>> hint as to what is intended to happen, and flag any
>> discrepancies.
>
>And each of these could have done the same with a "type-hint
>comment". But oh no, that would be too easy! And the whole
>point here is to cause a big fat ruckus? Isn't it, Mr.
>D'Aprano?

No, it is so that there can be an agreed syntax for this stuff rather than a 
billion funky special comments, and also so that _compiled_ python can be 
inspected for type hints because they survive that step. This makes for machine 
inspectable hints very easily, and that is a boon for linters.

Another advantage of type hints being part of the syntax is that 
invalid/mistyped type hints can be caught by the parser. If they're just 
special comments then linters _might_ see them and complain, or equally they 
might say "this comment isn't a valid type hint, so it is an ordinary comment", 
silently ignoring it. Thus weakening the lint.

As for the runtime ignoring them: they can't accomodate all situations in a 
dynamic language, and type checking would impose a performance penalty for no 
semantic effect.

Cheers,
Cameron Simpson <cs at cskk.id.au>



More information about the Python-list mailing list