[Python-ideas] Delay evaluation of annotations

2016-09-22 Thread אלעזר
Hi all, Annotations of function parameters and variables are evaluated when encountered. This makes it necessary to use string representation for names that are not yet bound, which affects almost every class definition. It is also easy to forget, and the result might be a (very uninteresting) exc

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread Alexander Belopolsky
On Thu, Sep 22, 2016 at 1:19 PM, אלעזר wrote: > I propose delaying evaluation of annotation-expressions by either keeping > the AST of the annotation, or turning it implicitly from EXP into "lambda: > EXP". Inspection code that is interested in this information can access it > be calling (or eval

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread Steven D'Aprano
On Thu, Sep 22, 2016 at 05:19:12PM +, אלעזר wrote: > Hi all, > > Annotations of function parameters and variables are evaluated when > encountered. Right, like all other Python expressions in general, and specifically like function parameter default arguments. > This makes it necessary to

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread אלעזר
On Thu, Sep 22, 2016 at 9:43 PM Steven D'Aprano wrote: > On Thu, Sep 22, 2016 at 05:19:12PM +, אלעזר wrote: > > Hi all, > > > > Annotations of function parameters and variables are evaluated when > > encountered. > > Right, like all other Python expressions in general, and specifically > like

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread David Mertz
On Thu, Sep 22, 2016 at 11:42 AM, Steven D'Aprano wrote: > > This makes it necessary to use string representation for names > > that are not yet bound, which affects almost every class definition. > > Almost every class? Really? I find that implausible. Still, I can see it > affecting *many* clas

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread אלעזר
On Thu, Sep 22, 2016 at 10:29 PM David Mertz wrote: > On Thu, Sep 22, 2016 at 11:42 AM, Steven D'Aprano > wrote: > >> > This makes it necessary to use string representation for names >> > that are not yet bound, which affects almost every class definition. >> >> Almost every class? Really? I fin

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread David Mertz
On Thu, Sep 22, 2016 at 12:35 PM, אלעזר wrote: > In such a hypothetical future world we might come to allow, e.g. >> `Sequence[#CustomThing]` where some general lazy facility or indirection is >> indicated by the '#' (just a placeholder for this comment, not a >> proposal). But if that comes abo

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread David Mertz
On Thu, Sep 22, 2016 at 12:35 PM, אלעזר 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" h

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread אלעזר
On Thu, Sep 22, 2016 at 10:45 PM David Mertz wrote: > On Thu, Sep 22, 2016 at 12:35 PM, אלעזר wrote: > >> In such a hypothetical future world we might come to allow, e.g. >>> `Sequence[#CustomThing]` where some general lazy facility or indirection is >>> indicated by the '#' (just a placeholder

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread אלעזר
On Thu, Sep 22, 2016 at 10:58 PM David Mertz wrote: > On Thu, Sep 22, 2016 at 12:35 PM, אלעזר 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)

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread David Mertz
On Thu, Sep 22, 2016 at 12:59 PM, אלעזר wrote: > I don't If this feature is "nice, but does not worth the complication", >> then so be it; I can't claim I know better. I only speculate that it does >> not necessarily requires a new custom type. >> > I don't mean a runtime type here (necessarily),

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread Alexander Belopolsky
On Thu, Sep 22, 2016 at 3:58 PM, David Mertz wrote: > It's more verbose, but you can also spell it now as: > > class A: > def __add__(self, other: type(self)) -> type(self): ... > No, you can't: >>> class A: ... def __add__(self, other: type(self)) -> type(self): ... ... Traceback (most

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread אלעזר
On Thu, Sep 22, 2016 at 11:02 PM David Mertz wrote: > On Thu, Sep 22, 2016 at 12:59 PM, אלעזר wrote: > >> I don't If this feature is "nice, but does not worth the complication", >>> then so be it; I can't claim I know better. I only speculate that it does >>> not necessarily requires a new custo

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread David Mertz
Doh! Yes, of course 'self' is only a scoped name within the body of the method, not in the signature. On Thu, Sep 22, 2016 at 1:02 PM, Alexander Belopolsky < [email protected]> wrote: > > On Thu, Sep 22, 2016 at 3:58 PM, David Mertz wrote: > >> It's more verbose, but you can also s

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread אלעזר
On Thu, Sep 22, 2016 at 11:02 PM David Mertz wrote: > On Thu, Sep 22, 2016 at 12:59 PM, אלעזר wrote: > >> I don't If this feature is "nice, but does not worth the complication", >>> then so be it; I can't claim I know better. I only speculate that it does >>> not necessarily requires a new custo

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread Alexander Belopolsky
On Thu, Sep 22, 2016 at 4:29 PM, אלעזר wrote: > Just as a demonstration, the parser can transform `EXP` into `lambda: EXP` > - and that's it. It will not solve everything (e.g. error messages and > .__annotation__ access as Alexander says), but it demonstrates the fact > that the change need not

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread Alexander Belopolsky
On Thu, Sep 22, 2016 at 4:37 PM, Alexander Belopolsky < [email protected]> wrote: > On the second thought, why can't the parser simply replace A with 'A' in > annotations that appear in the body of class A? This will only break > somewhat pathological code that defines A before it is

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread Alexander Belopolsky
On Thu, Sep 22, 2016 at 4:43 PM, Alexander Belopolsky < [email protected]> wrote: > > On Thu, Sep 22, 2016 at 4:37 PM, Alexander Belopolsky < > [email protected]> wrote: > >> On the second thought, why can't the parser simply replace A with 'A' in >> annotations that appe

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread Ivan Levkivskyi
On 22 September 2016 at 22:02, אלעזר wrote: > On Thu, Sep 22, 2016 at 10:58 PM David Mertz wrote: > >> On Thu, Sep 22, 2016 at 12:35 PM, אלעזר 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 ann

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread אלעזר
On Fri, Sep 23, 2016 at 12:05 AM Ivan Levkivskyi wrote: > On 22 September 2016 at 22:02, אלעזר wrote: > >> On Thu, Sep 22, 2016 at 10:58 PM David Mertz wrote: >> >>> On Thu, Sep 22, 2016 at 12:35 PM, אלעזר wrote: >>> I think we're talking about different things here. I just referred to >>

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread Chris Angelico
On Fri, Sep 23, 2016 at 6:48 AM, Alexander Belopolsky wrote: > On the third thought, this entire feature can be implemented in the > metaclass by injecting A = 'A' in the dict in __prepare__. That would be the easiest, and least magical, solution. It simply means that the name of the current clas

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread אלעזר
On Fri, Sep 23, 2016 at 12:18 AM Chris Angelico wrote: > # Recursion in functions > def spam(): > return spam() > I just note that it *is* surprising, for most users, that you can't be sure that this is a recursion, yet. So it if you want a trusted-upon recursion you should write # spam: de

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread Chris Angelico
On Fri, Sep 23, 2016 at 7:33 AM, אלעזר wrote: > On Fri, Sep 23, 2016 at 12:18 AM Chris Angelico wrote: >> >> # Recursion in functions >> def spam(): >> return spam() > > > I just note that it *is* surprising, for most users, that you can't be sure > that this is a recursion, yet. So it if you

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread Nick Coghlan
On 23 September 2016 at 05:58, David Mertz wrote: > On Thu, Sep 22, 2016 at 12:35 PM, אלעזר 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) -

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread אלעזר
On Fri, Sep 23, 2016 at 4:17 AM Nick Coghlan wrote: ... > As others have noted, the general idea of allowing either a > placeholder name or the class name to refer to a suitable type > annotation is fine, though - that would be a matter of implicitly > injecting that name into the class namespace

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread Steven D'Aprano
On Thu, Sep 22, 2016 at 09:33:58PM +, אלעזר wrote: > On Fri, Sep 23, 2016 at 12:18 AM Chris Angelico wrote: > > > # Recursion in functions > > def spam(): > > return spam() > > > > I just note that it *is* surprising, for most users, that you can't be sure > that this is a recursion, yet

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread Chris Angelico
On Fri, Sep 23, 2016 at 12:35 PM, Steven D'Aprano wrote: > The straight-forward and simple way of writing a recursive spam() > function surprises beginners, but they might go years or their entire > career without running into a situation where they are caught by > surprise. After all, it is rare

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread Steven D'Aprano
On Thu, Sep 22, 2016 at 07:21:18PM +, אלעזר wrote: > On Thu, Sep 22, 2016 at 9:43 PM Steven D'Aprano wrote: > > > On Thu, Sep 22, 2016 at 05:19:12PM +, אלעזר wrote: > > > Hi all, > > > > > > Annotations of function parameters and variables are evaluated when > > > encountered. > > > > Rig

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread Terry Reedy
On 9/22/2016 10:54 PM, Chris Angelico wrote: On Fri, Sep 23, 2016 at 12:35 PM, Steven D'Aprano wrote: The straight-forward and simple way of writing a recursive spam() function surprises beginners, but they might go years or their entire career without running into a situation where they are ca

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread Nick Coghlan
On 23 September 2016 at 12:05, אלעזר wrote: > On Fri, Sep 23, 2016 at 4:17 AM Nick Coghlan wrote: > ... >> >> As others have noted, the general idea of allowing either a >> placeholder name or the class name to refer to a suitable type >> annotation is fine, though - that would be a matter of imp

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread Nick Coghlan
On 23 September 2016 at 13:06, Steven D'Aprano wrote: > On Thu, Sep 22, 2016 at 07:21:18PM +, אלעזר wrote: >> "Expression" is something that you need its value right >> now, and "annotation" is something that, well, annotates the code you see >> right now. > > Right. In the case of Python, fun

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread Greg Ewing
אלעזר wrote: it feels like a placeholder for this meaning would be better. E.g.: class A: def __add__(self, other: CLS) -> CLS: ... That's fine for a class that refers to itself, but what about classes that refer to each other? This only addresses a small part of the probl

Re: [Python-ideas] Delay evaluation of annotations

2016-09-22 Thread Nick Coghlan
On 23 September 2016 at 15:50, Greg Ewing wrote: > אלעזר wrote: >> >> it feels like a >> placeholder for this meaning would be better. E.g.: >> >> class A: >> def __add__(self, other: CLS) -> CLS: ... > > > That's fine for a class that refers to itself, but > what about classe