Tooltip generator type in python 3.6

In accordance with PEP-484, we can introduce allusions to the function of the generator 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, understanding the list gives the following error in PyCharm.

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

Any idea why PyCharm treats this as an error? Thanks.


A few clarifications after reading some answers. I am using PyCharm Community Edition 2016.3.2 (latest version) and imported typing.Generator (updated in code). The above code works very well, but PyCharm considers this an error:

enter image description here

So, I am wondering if this is really a bug or an unsupported function in PyCharm.

+8
python pycharm
source share
2 answers

As Alexander Dashkov commented, I tried the same code with Pycharm 2017.1 EAP, which handles this annotation correctly. I assume that this feature will be integrated into the next official version of PyCharm. Thanks to everyone.

+2
source share

You need to import the typing module. According to the docs:

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

Try instead:

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

The above result will have the desired result:

 l = [i for i in generate()] 

Output:

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


As stated in the comments, you cannot use the latest version of PyCharm. Try upgrading to version 2016.3.2 and everything may be in order. Unfortunately, this is a known bug, according to a comment by @AshwiniChaudhary.

In more detail, the reported problem (for the latest version of PyCharm) was introduced in December last year. They probably fixed it and introduced modifications to the same version.

+3
source share

All Articles