(Python type hints) How to specify type being currently defined as the returned type from a method?
<p>I would like to specify (as a type hint) the currently defined type as the return type from a method.</p>

<p>Here's an example:</p>

<pre class="lang-py prettyprint-override"><code>class Action(Enum):
    ignore = 0
    replace = 1
    delete = 2

    @classmethod
    # I would like something like
    # def idToObj(cls, elmId: int)-&gt;Action:
    # but I cannot specify Action as the return type
    # since it would generate the error
    # NameError: name 'Action' is not defined
    def idToObj(cls, elmId: int):
        if not hasattr(cls, '_idToObjDict'):
            cls._idToObjDict = {}
            for elm in list(cls):
                cls._idToObjDict[elm.value] = elm

        return cls._idToObjDict[elmId]
</code></pre>

<p>Ideally I would have liked to specify something like</p>

<pre class="lang-py prettyprint-override"><code>def idToObj(cls, elmId: int)-&gt;Action:
</code></pre>

<p>Thank you.</p>

<p>This case is mentioned in official <a href="https://www.python.org/dev/peps/pep-0484/#id27" rel="nofollow">type hints PEP</a>:</p>

<blockquote>
  <p>When a type hint contains names that have not been defined yet, that
  definition may be expressed as a string literal, to be resolved later.</p>
</blockquote>

<pre><code>class Tree:
    def __init__(self, left: Tree, right: Tree):
        self.left = left
        self.right = right
</code></pre>

<p>To address this, we write:</p>

<pre><code>class Tree:
    def __init__(self, left: 'Tree', right: 'Tree'):
        self.left = left
        self.right = right
</code></pre>

<p>In your case It would be:</p>

<pre><code>def idToObj(cls, elmId: int)-&gt;'Action':
    pass  # classmethod body
</code></pre>

python type-hinting