You're right, this violates the standard scoping rules. I suspect it is a mistake by the PEP author. I'm adding Lukasz to the discussion, hopefully he remembers why the PEP says this.

On Sat, Jun 19, 2021 at 12:10 AM Sergei Lebedev <sergei.a.lebedev@gmail.com> wrote:
Hi typing-sig,

PEP-563 has a "Backward compatibility" section which contains examples of how type annotations work for nested classes. In particular, the PEP says that

class C:
  class D:
    def method(self) -> D: ...

should be accepted by the type checker. This does not contradict

> Annotations using nested classes and their respective state are still valid. They can use local names or the fully qualified name.

but is still surprising, because if we apply the usual scoping rules, the name D is not visible from the body of D. The name C is visible, so is C.D, but D is not.

Does this look right to you? If yes, could you help me understand the motivation for having this special-case in the PEP?

As an aside, I have  tried this example in mypy (0.800), PyCharm (2020.3), pyright (in pylance 2021.6.2) and pytype (2021.05.25), and none of them seem to accept it. The same goes for typing.get_type_hints, which fails with a NameError

Python 3.9.2 (default, Feb 28 2021, 17:03:44)
>>> from __future__ import annotations
>>> import typing
>>> class C:
...   class D:
...     def method(self) -> D: ...
...
>>> typing.get_type_hints(C.D.method)
Traceback (most recent call last):
  ...
NameError: name 'D' is not defined

Cheers,
Sergei
_______________________________________________
Typing-sig mailing list -- typing-sig@python.org
To unsubscribe send an email to typing-sig-leave@python.org
https://mail.python.org/mailman3/lists/typing-sig.python.org/
Member address: guido@python.org


--
--Guido van Rossum (python.org/~guido)
Pronouns: he/him (why is my pronoun here?)