1

I'm trying to make use of type hints in Python 3.5.1 with following code:

>>> class A:                         
...     def a(self, i: int, b: A):   
...         pass                     
...                                  
Traceback (most recent call last):   
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in A       
NameError: name 'A' is not defined   

I guess it's problem with scoping that type A is not yet fully constructed while parsing type A, but I fail to understand why such a syntax is not OK. Is there some way to express it otherwise or it's simply illegal construct?

I wanted to use this syntax hint in base class that could compose derived classes in tree-like hierarchy.

vaultah
  • 44,105
  • 12
  • 114
  • 143
Yakuza
  • 3,237
  • 2
  • 21
  • 18

1 Answers1

3

Try this:

class A:

    def a(self, i: int, b: 'A'):
        pass

For more information take a look here

BPL
  • 9,632
  • 9
  • 59
  • 117