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
_______________________________________________