How can I ensure that arguments have same type without listing the types explicitly?
<p>Let us assume that we need a function that accepts two arguments of any type as long as both arguments have the same type. How would you check it statically with mypy?</p>

<p>If we only need the function to accept some finite amount of already known types, it is easy:</p>

<pre class="lang-py prettyprint-override"><code>from typing import TypeVar, List, Callable

T = TypeVar('T', int, str, List[int], Callable[[], int])

def f(a: T, b: T) -&gt; None:
   pass

f(1, 2)
f("1", "2")
f([1], [2])
f(lambda: 1, lambda: 2)
f(1, "2") # mypy will print an error message
</code></pre>

<p>For this code, mypy can ensure that the arguments to <code>f</code> are either two <code>int</code>s or two <code>str</code>s or two lists of <code>int</code>s or two functions of zero arguments that return <code>int</code>.</p>

<p>But what if we don't know the types in advance? What if we need something similar to <code>let f (a:'t) (b:'t) = ()</code> from F# and OCaml? Simply writing <code>T = TypeVar('T')</code> would make things like <code>f(1, "2")</code> valid, and this is not what we want.</p>