Recursive type annotations

Ian Kelly ian.g.kelly at gmail.com
Wed Jun 8 10:58:22 EDT 2016


On Wed, Jun 8, 2016 at 7:31 AM, Nagy László Zsolt <gandalf at shopzeus.com> wrote:
>
>>>         pass
>>>
>>> NameError: name 'Test' is not defined
>> I think you can fix this by using a string annotation as follows:
>>
>>     class Test:
>>         def test(self, child: "Test"):
>>             pass
> Yes, you are right. It is not directly written in the official
> documentation ( https://docs.python.org/3/library/typing.html ), but it
> is in the PEP 0484, section "Forward references".

That link specifically documents the typing module. Forward references
don't really have anything to do with the typing module and are best
covered by the documentation of the static checker in use. Here's
where the MyPy documentation covers it:

http://mypy.readthedocs.io/en/latest/kinds_of_types.html#class-name-forward-references



More information about the Python-list mailing list