what is correct way of type hint a function that return only a specific set of values?
<p>I have a function that can only return <code>a</code>, <code>b</code> or <code>c</code> all of them are of type <code>T</code> but I want to make part of its signature this fact because of the special meaning they carry in the context of the function, how I do that?</p>

<p>currently I use this</p>

<pre><code>def fun(...) -&gt; "a or b or c":
    #briefly explain the meaning of a, b and c in its docstring
</code></pre>

<p>is that the correct one?</p>

<p>I know that I can do this</p>

<pre><code>def fun(...) -&gt; T:
    #briefly explain the meaning of a, b and c in its docstring
</code></pre>

<p>but as I said I want to express in the signature that the function only return those specific values</p>

<p>You can't specify that your function returns only a subset of a type's values using type hinting alone. As the name implies, type hinting is all about <em>types</em> not values.</p>

<p>However, you can create a new <code>enum.Enum</code> subtype that only has the values you're going to return and use it in the function. Then you can type hint that you're returning the enum type.</p>

<pre><code>import enum

class cmp_results(enum.IntEnum):
    less = -1
    equal = 0
    greater = 1

def my_cmp_function(x, y) -&gt; cmp_results:
    if x &lt; y: return cmp_results.less
    elif x == y: return cmp_results.equal
    else: return cmp_results.greater
</code></pre>

<p>This may be overkill. Just hinting <code>int</code> as the return type (and documenting the specific values) is probably good enough.</p>

python python-3.x type-hinting