Optional return type annotations?
<p>I have a function that will either return a generator (<code>types.GeneratorType</code>) or <code>list</code>, depending upon the value of an argument. For example:</p>

<pre><code>def foo(switch: bool) -&gt; list: # return type of list || types.GeneratorType
    ... # create some generator object named f1
    if switch:
        return list(f1)
    else:
        return f1
</code></pre>

<p>How do I tell it that I can optionally return a list or another type? Or, for that matter, what about a bunch of different types?</p>

<p>I've looked over <a href="https://docs.python.org/3/library/typing.html" rel="nofollow">documentation on a module <code>typing</code></a> but haven't as of yet found any methods there that allow for this. I haven't been able to find any examples by Googling either.</p>