[Python-ideas] Delay evaluation of annotations

אלעזר elazarg at gmail.com
Thu Sep 22 16:02:17 EDT 2016


On Thu, Sep 22, 2016 at 10:58 PM David Mertz <mertz at gnosis.cx> wrote:

> On Thu, Sep 22, 2016 at 12:35 PM, אלעזר <elazarg at gmail.com> wrote:
>
>> I think we're talking about different things here. I just referred to the
>> common need to use the name of the current class in type annotation
>>
>> class A:
>>     def add(self, other: A) -> A: ...
>>
>
> Yeah, I find the need for using the string "A" here a wart. Rather than
> change the entire semantics of annotations, it feels like a placeholder for
> this meaning would be better.  E.g.:
>
> class A:
>     def __add__(self, other: CLS) -> CLS: ...
>
> A static checker could do the magic of recognizing that special name
> easily enough (no harder than recognizing the quoted string).  At runtime
> 'CLS' could either just be a singleton with no other behavior... or perhaps
> it could be some sort of magic introspection object.  It's more verbose,
> but you can also spell it now as:
>
> class A:
>     def __add__(self, other: type(self)) -> type(self): ...
>
> That's a little ugly, but it expresses the semantics we want.
>
> Mypy fails on this, and version 4.4 fails very hard and ugly, with errors
for each line to the end of the file + 1. It can be fixed of course, but
it's worth noting: mypy does not expect arbitrary expression - because it
is an annotation.

Elazar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160922/6820d681/attachment.html>


More information about the Python-ideas mailing list