Skip to content
Advertisement

Subtype Generator with fixed type variables

Say I have a program with several generator functions that all have the return type Generator[<type>, <type2>, None] and I want to make an alias SimpleGenerator[<type>, <type2>] that is then expanded to the previous.

So for example

JavaScript

could be written

JavaScript

I imagine the code might look something like (obviously incorrect python code)

JavaScript

But I’m not finding an easy way of writing it, and if going all the way of defining a class that inherits from Generator:

JavaScript

Then both mypy and pyright will not accept that as a return type for generators, saying

The return type of a generator function should be “Generator” or one of its supertypes

and

Return type of generator function must be “Generator” or “Iterable”

respectively. Is this possible to do?

(Originally I wanted Generator[<type>, None, None] and the pyright error message helped me realize I could then just use Iterable[<type>] as the return type. But my curiousity remains so I modified the question.)

Advertisement

Answer

Your generator is not a class, but a method, and you annotate it with your type. So you should do something like this:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement