Python type checking not working as expected
<p>I'm sure I'm missing something obvious here, but why does the following script actually work?</p>

<pre><code>import enum
import typing

class States(enum.Enum):
    a = 1
    b = 2

states = typing.NewType('states', States)

def f(x: states) -&gt; states:
    return x

print(
    f(States.b),
    f(3)
)
</code></pre>

<p>As far I understand it, it should fail on the call <code>f(3)</code>, however it doesn't. Can someone shed some light on this behaviour?</p>

<p>No checking is performed by Python itself. This is specified in the <a href="https://www.python.org/dev/peps/pep-0484/#non-goals" rel="nofollow">"Non- Goals" section</a> of PEP 484. When executed (i.e during run-time), Python completely ignores the annotations you provided and evaluates your statements as it usually does, dynamically.</p>

<p>If you need type checking, you should perform it yourself. This can currently be performed by static type checking tools like <a href="http://mypy.readthedocs.io/en/latest/" rel="nofollow"><code>mypy</code></a>.</p>

python python-3.x types type-hinting