Why Any and None appear in a Generator type hint parameters alongside the actual type of the members?

2258 views python

Writing the following code

def a(n: int):
    for i in range(n):
        yield i

b = a(3)

then asking PyCharm to add type hint to variable b turns the variable declaration in

b: Generator[int, Any, None] = a(3)

Why not just the following?

b: Generator[int] = a(3)

answered question

1 Answer


Because Generator[int] isn't a valid use of typing.Generator. Generator takes 3 type parameters. You can't give it just one.

posted this

Have an answer?


Please login first before posting an answer.