Requiring the type of a parameter of an objects method to be the class of this same object
<p>The code I have included below throws the following error:</p>

<pre><code>NameError: name 'Vector2' is not defined 
</code></pre>

<p>at this line: </p>

<pre><code>def Translate (self, pos: Vector2):
</code></pre>

<p>Why does Python not recognize my <code>Vector2</code> class in the <code>Translate</code> method?</p>

<pre><code>class Vector2:

    def __init__(self, x: float, y: float):

        self.x = x
        self.y = y

    def Translate(self, pos: Vector2):

        self.x += pos.x
        self.y += pos.y
</code></pre>

<p>Because when it encounters <code>Translate</code> (while compiling the class body), <code>Vector2</code> hasn't been defined yet (it is currently compiling, name binding hasn't been performed); Python naturally complains. </p>

<p>Since this is such a common scenario (type-hinting a class in the body of that class), you should use a <em><a href="https://www.python.org/dev/peps/pep-0484/#forward-references" rel="nofollow">forward reference</a></em> to it by enclosing it in quotes:</p>

<pre><code>class Vector2:    
    # __init__ as defined

    def Translate(self, pos: 'Vector2'):    
        self.x += pos.x
        self.y += pos.y
</code></pre>

<p>Python (and any checkers complying to <code>PEP 484</code>) will understand your hint and register it appropriately. Python does recognize this when <code>__annotations__</code> are accessed through <a href="https://docs.python.org/3/library/typing.html#typing.get_type_hints" rel="nofollow"><code>typing.get_type_hints</code></a>:</p>

<pre><code>from typing import get_type_hints

get_type_hints(Vector2(1,2).Translate)
{'pos': __main__.Vector2}
</code></pre>

python python-3.x oop type-hinting