This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: one-argument version for Generator.typing
Type: Stage: resolved
Components: Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: levkivskyi, srittau, zeth
Priority: normal Keywords:

Created on 2017-10-05 08:35 by srittau, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg303748 - (view) Author: Sebastian Rittau (srittau) * Date: 2017-10-05 08:35
Currently typing.Generator requires three arguments: Generator[YieldType, SendType, ReturnType].

At least for me, passing values to a generator is a very rare case. I suggest to allow only one argument to be passed to Generator: Generator[YieldType], where the other arguments default to None. This makes the common case more readable and is less error-prone. (I always forget the second and third argument, since that use case is so uncommon for me.)
msg303839 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2017-10-06 17:06
You can use Iterator type, for example this works in mypy (didn't test in other type checkers):

  def f() -> Iterator[int]:
      yield 42

In case you want annotate something specifically as Generator[int, None, None] (for example to use its .close() method), then you can create a generic alias:

  T = TypeVar('T')
  Gen = Generator[T, None, None]

  def f() -> Gen[int]:
      ...

this is supported by mypy as well.
msg305456 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2017-11-02 22:56
Since there is no response for few weeks I assume this works for you.
msg305459 - (view) Author: Sebastian Rittau (srittau) * Date: 2017-11-02 23:55
Sorry for not responding, but I didn't know what I could have added that I didn't already say in the opening post. Of course, you can use workaround like using the three-argument version or creating aliases. Using Iterator is of course not a real solution for the general case.

I still believe that Generator should be usable with just one argument or typing should provide such an alias.
msg407421 - (view) Author: Zeth (zeth) * Date: 2021-12-01 01:34
I don't get why this is closed. It is a bug. The Generator type annotation works differently than the others, it shouldn't.

Sebastian Rittau is correct here, the closer doesn't seem to have understood. 

As for using Iterator instead as the documentation suggests, that makes a mockery of the whole process. You might as well set everything to Any.
History
Date User Action Args
2022-04-11 14:58:53adminsetgithub: 75881
2021-12-01 01:34:39zethsetnosy: + zeth
messages: + msg407421
2017-11-02 23:55:09srittausetmessages: + msg305459
2017-11-02 22:56:04levkivskyisetstatus: open -> closed
resolution: not a bug
messages: + msg305456

stage: resolved
2017-10-06 17:06:28levkivskyisetnosy: + levkivskyi
messages: + msg303839
2017-10-05 08:35:17srittaucreate