ChatGPT解决这个技术问题 Extra ChatGPT

How to type hint a generator in Python 3?

According to PEP-484, we should be able to type hinting a generator function as follows:

from typing import Generator

def generate() -> Generator[int, None, None]:
    for i in range(10):
        yield i

for i in generate():
    print(i)

However, the list comprehension gives the following error in PyCharm.

Expected 'collections.Iterable', got 'Generator[int, None, None]' instead less... (⌘F1)

Any idea why PyCharm is considering this as error?

A few clarification after reading some answers. I am using PyCharm Community Edition 2016.3.2 (the latest version) and have imported the typing.Generator (updated in the code). The above code runs just fine, but PyCharm considers this an error:

https://i.stack.imgur.com/PGSwC.png

So, I'm wondering if this is actually an error or an unsupported feature in PyCharm.

What happens when you change your code to this: ideone.com/IwHbT0 ?
I use Pycharm 2017.1 EAP and it seems to be fixed.
The EAP indeed solved this issue; thank you very much.
@MikeWilliamson yes, it was an edit: stackoverflow.com/revisions/42531143/2. It looks like the real question is PyCharm, not Python. The top answer has been updated to mention that, too. I dunno what should be done with this confusing Q/A per SO standards.

佚名

You need to import the typing module. As per docs:

The return type of generator functions can be annotated by the generic type Generator[yield_type, send_type, return_type] provided by typing.py module

Try this way instead:

from typing import Generator


def generate() -> Generator[int, None, None]:
    for i in range(10):
        yield i

The above will have the desired result:

l = [i for i in generate()]

Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

As pointed out in the comments, you might not use the last version of PyCharm. Try switching to version 2016.3.2 and you might be fine. Unfortunately this is a well-known bug, as per @AshwiniChaudhary comment.

More, the reported issue (for the last version of PyCharm) was submitted on December, last year. They probably fixed it and pushed the modifications into the same version.


Shouldn't that result in NameError for Generator for OP?
@AshwiniChaudhary why would it ? I just tested it and it works exactly as is.
Issue is not with output, issue is with PyCharm complaining about return type. Have you tested it in PyCharm?
@AshwiniChaudhary is correct, the OP has already made the import (even though it's not shown in their code snippet) otherwise they'd get a NameError
what's a send_type?
n
neves

This isn't a direct answer to the question, but I think it is a better solution.

I'm using the typing specification below, using Iterator[int] instead of Generator. The validation is OK. I think it is a lot clearer. It better describes the code intention and is recommended by Python docs.

from typing import Iterator

def generate() -> Iterator[int]:
    for i in range(10):
        yield i

It would also allow future refactorings if you change your Generator for a list or other iterable.

I'm using Visual Studio Code with PyLance for typing validation. PyCharm mypy should have the same behavior.


I am not sure why this got downvoted. The python doc itself states, that is it possible to use Iterator[YieldType] or Iterable[YieldType] for generators, which yield only. See docs.python.org/3.6/library/typing.html#typing.Generator
from collections.abc import Iterator is the new preferable way. See docs.python.org/3.10/library/typing.html#typing.Iterable