0

When I annotate a method argument with the enclosing class as its type in python, I get an error that the type is not (yet) defined. E.g. the code

class Foobar:
    def foo(self, bar: Foobar):
        ...

produces NameError: name 'Foobar' is not defined. What's the correct approach to this?

Henning Koehler
  • 2,456
  • 1
  • 16
  • 20
  • 1
    This might provide some insight: https://stackoverflow.com/questions/33533148/how-do-i-specify-that-the-return-type-of-a-method-is-the-same-as-the-class-itsel – theberzi Apr 29 '19 at 07:42

1 Answers1

1

Using a future import:

from __future__ import annotations

class Foobar:
    def foo(self, bar: Foobar):
        ...
L3viathan
  • 26,748
  • 2
  • 58
  • 81