4

I'm trying to write out type annotation for proper autocomplete in PyCharm. See this refined code example:

class TestClass:
    a = 1

    @staticmethod
    def make_new_with_a(a: int) -> TestClass:
        t = TestClass()
        t.a = a
        return t

print(TestClass.make_new_with_a(7).a)

Code works and prints 7 to output. But PyCharm underlines -> TestClass with red and shows Unresolved reference 'TestClass' on mouse over hint. Autocompletion for fields of method return type (...make_new_with_a(7). + <ctrl+space>) doesn't work.

Similar questions:

d9k
  • 1,476
  • 3
  • 15
  • 28
  • 1
    check it with python3.5 and pycharm 5.0 rc, should work – user996142 Oct 31 '15 at 03:59
  • 4
    The solution is given in the linked _Similar questions_: put `-> 'TestClass'` (as a string) instead `-> TestClass`. PyCharm/IntelliJ IDEA recognises this and gives you autocompletion and all them goodies. ([Type Hints: forward references](https://www.python.org/dev/peps/pep-0484/#forward-references)) – Bloke Oct 12 '16 at 17:55
  • Oh wow that really blows – Dagrooms Nov 28 '17 at 16:03
  • Possible duplicate of [putting current class as return type annotation](https://stackoverflow.com/questions/15853469/putting-current-class-as-return-type-annotation) – Dagrooms Nov 28 '17 at 18:42

0 Answers0