Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #109675 > unrolled thread

Recursive type annotations

Started byNagy László Zsolt <gandalf@shopzeus.com>
First post2016-06-08 15:02 +0200
Last post2016-06-08 08:58 -0600
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Recursive type annotations Nagy László Zsolt <gandalf@shopzeus.com> - 2016-06-08 15:02 +0200
    Re: Recursive type annotations Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2016-06-08 13:11 +0000
      Re: Recursive type annotations Nagy László Zsolt <gandalf@shopzeus.com> - 2016-06-08 15:31 +0200
      Re: Recursive type annotations Ian Kelly <ian.g.kelly@gmail.com> - 2016-06-08 08:58 -0600

#109675 — Recursive type annotations

FromNagy László Zsolt <gandalf@shopzeus.com>
Date2016-06-08 15:02 +0200
SubjectRecursive type annotations
Message-ID<mailman.77.1465390948.2306.python-list@python.org>
class Test:
    def test(self, child : Test):
        pass

NameError: name 'Test' is not defined

I understand that the class "Test" is not defined until the class
definition is executed. But it is very very common to build recursive
data structures, and I have concrete use case where the IDE did not
recognize the type of the argument, and as a result it forgot to rename
some method calls when I auto-refactored the name of the method.

I'm not an expert, but I believe that these annotations are not used by
the byte compiler for anything. This is just pure syntax introduced for
the person who reads the code.

Is there a known obsticle that would prevent us from detecting recursive
type annotations? (E.g. bind the annotation to the class that is being
defined.)

Thanks,

   Laszlo

[toc] | [next] | [standalone]


#109676

FromJon Ribbens <jon+usenet@unequivocal.co.uk>
Date2016-06-08 13:11 +0000
Message-ID<slrnnlg6bs.6f8.jon+usenet@sable.unequivocal.co.uk>
In reply to#109675
On 2016-06-08, Nagy László Zsolt <gandalf@shopzeus.com> wrote:
> class Test:
>     def test(self, child : Test):
>         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

[toc] | [prev] | [next] | [standalone]


#109678

FromNagy László Zsolt <gandalf@shopzeus.com>
Date2016-06-08 15:31 +0200
Message-ID<mailman.79.1465392677.2306.python-list@python.org>
In reply to#109676
>>         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".

Thanks!

   Laszlo

[toc] | [prev] | [next] | [standalone]


#109680

FromIan Kelly <ian.g.kelly@gmail.com>
Date2016-06-08 08:58 -0600
Message-ID<mailman.81.1465397951.2306.python-list@python.org>
In reply to#109676
On Wed, Jun 8, 2016 at 7:31 AM, Nagy László Zsolt <gandalf@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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web