14

How can I type a static method that returns an instance object?

import typing

class foo(object):

  @staticmethod
  def getOne() -> foo:
    return FooRegister().get()

FooRegister contains all the instances of foo. If however I type it as shown, python complains since foo hasn't been defined yet.

What's the correct way to type this?

Paulo Matos
  • 1,614
  • 17
  • 23
  • Well, technically your return value's type is `type` or a subclass thereof if you are returning class objects. – Mad Physicist Aug 11 '17 at 13:49
  • 5
    Using a string is very common: `def getOne() -> 'foo':`. That's interpreted as ["forward referencing"](https://www.python.org/dev/peps/pep-0484/#forward-references) with typehints. – MSeifert Aug 11 '17 at 13:49
  • @MSeifert that works. Thanks. Apologies for the duplicate. – Paulo Matos Aug 11 '17 at 15:43

0 Answers0