<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On 1 November 2017 at 23:48, Lukasz Langa <span dir="ltr"><<a href="mailto:lukasz@langa.pl" target="_blank">lukasz@langa.pl</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Runtime annotation resolution and class decorators<br>
------------------------------<wbr>--------------------<br>
<br>
Metaclasses and class decorators that need to resolve annotations for<br>
the current class will fail for annotations that use the name of the<br>
current class.  Example::<br>
<br>
    def class_decorator(cls):<br>
        annotations = get_type_hints(cls)  # raises NameError on 'C'<br>
        print(f'Annotations for {cls}: {annotations}')<br>
        return cls<br>
<br>
    @class_decorator<br>
    class C:<br>
        singleton: 'C' = None<br>
<br>
This was already true before this PEP.  The class decorator acts on<br>
the class before it's assigned a name in the current definition scope.<br>
<br></blockquote><div> </div></div></div><div class="gmail_extra">Just a random idea: maybe this can be resolved by just updating the localns before calling get_type_hints() like this:</div><div class="gmail_extra"><br></div><div class="gmail_extra">  localns = locals().copy()</div><div class="gmail_extra">  localns.update({cls.__name__: cls})</div><div class="gmail_extra"><br></div><div class="gmail_extra"><br></div><div class="gmail_extra">In general I like how the PEP is written now. I maybe would add examples for this<br></div><div class="gmail_extra"><br></div><div class="gmail_extra">> the cases listed above might be
worked around by placing the usage</div><div class="gmail_extra">> in a <tt class="gmail-docutils gmail-literal">if TYPE_CHECKING:</tt> block.</div><div class="gmail_extra">> ...</div><div class="gmail_extra">> For named tuples, using the new
class definition syntax introduced in Python 3.6 solves the issue.</div><div class="gmail_extra"><br></div><div class="gmail_extra">actually showing something like <br></div><div class="gmail_extra"><br></div><div class="gmail_extra">  if TYPE_CHECKING:</div><div class="gmail_extra">      Alias = List[Tuple[int, SomeClass]]<br></div><div class="gmail_extra"><br></div><div class="gmail_extra">  class NT(NamedTuple):</div><div class="gmail_extra">      one: SomeClass</div><div class="gmail_extra">      other: Alias<br></div><div class="gmail_extra"><br></div><div class="gmail_extra">  class SomeClass:</div><div class="gmail_extra">      ...<br></div><div class="gmail_extra"><br></div><div class="gmail_extra">--</div><div class="gmail_extra">Ivan</div><div class="gmail_extra"><br></div><div class="gmail_extra"><br></div></div>