How to make a type hint forward reference

Viewed 3199

I am playing around with python's 3.5 type hints. I was wondering how can I type hint the return type of a class method.

This is what I had in mind:

>>> class A():
    @classmethod
    def a(cls) -> A:
        pass

Traceback (most recent call last):
  File "<pyshell#24>", line 1, in <module>
    class A():
  File "<pyshell#24>", line 3, in A
    def a(cls) -> A:
NameError: name 'A' is not defined

Clearly it does not work. I guess one way would be to do some sort of "forward declaration" like so:

>>> class A():
    pass

>>> class A():
    @classmethod
    def a(cls) -> A:
        pass

But that does not feel "pythonic". How do people normally solve this?

1 Answers
Related