I have a function that will either return a generator (
def foo(switch: bool) -> list: # return type of list || types.GeneratorType
... # create some generator object named f1
The two solutions that immediately spring to mind are:
def foo(switch: bool) -> Union[list, GeneratorType]:
Type based on the behaviour you can expect from either return value, e.g. that they are
def foo(switch: bool) -> Iterable:
In both cases you could also provide more detail: list or generator/iterable of what?
That said, something that returns either a list or a generator sounds like an odd interface, and it would be helpful to provide a bit more context around what consumers of your function should expect to do with whatever they receive. It may be that:
listyou could use the more general
Sequence, to permit e.g.
tuples to be used instead.