[Python-ideas] ideas for type hints for variable: beyond comments

Chris Angelico rosuav at gmail.com
Tue Sep 1 15:13:51 CEST 2015


On Tue, Sep 1, 2015 at 11:03 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Tue, Sep 01, 2015 at 09:19:29PM +1000, Chris Angelico wrote:
>
>> On Tue, Sep 1, 2015 at 8:24 PM, Ian <ian.team.python at gmail.com> wrote:
>> >
>> > mypy currently inspects the comment on the line of first assignment for the
>> > variables to be type hinted.
>> >
>> > It is logical that at some time python language will add support to allow
>> > these type hints to move from comments to the code as has happened for 'def'
>> > signatures.
>>
>> Potential problem: Function annotations are supported all the way back
>> to Python 3.0, but any new syntax would be 3.6+ only. That's going to
>> severely limit its value for quite some time. That doesn't mean new
>> syntax can't be added (otherwise none ever would), but the bar is that
>> much higher - you'll need an extremely compelling justification.
>
> PEP 484 says:
>
> "No first-class syntax support for explicitly marking variables as being
> of a specific type is added by this PEP. To help with type inference in
> complex cases, a comment of the following format may be used: ..."
>
> https://www.python.org/dev/peps/pep-0484/
>
> I recall that in the discussions prior to the PEP, I got the strong
> impression that Guido was open to the concept of annotating variables in
> principle, but didn't think it was very important (for the most part,
> the type checker should be able to infer the variable type), and he
> didn't want to delay the PEP for the sake of agreement on a variable
> declaration syntax when a simple comment will do the job.
>
> So in principle, if we agree that type declarations for variables should
> look like (let's say) `str s = some_function(arg)` then the syntax may
> be added in the future, but it's a low priority.

Right, it's low priority, and a non-backward-compatible one.
Backporting typing.py to any 3.x Python will make all the annotations
"succeed" (given that success, at run time, doesn't require any sort
of actual checking); it's not possible to backport a syntax change.
It's like using 'yield from' for coroutines - it instantly stops you
from running on anything older than 3.3. Maybe that'll be worthwhile,
but the complaint that "comments are ugly" isn't enough justification
IMO.

If there were some serious run-time value for these annotations, then
I could see more reason for adding them. At the moment, though, I'm
distinctly -1.

ChrisA


More information about the Python-ideas mailing list