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
Priority: normal Keywords:

Created on 2017-10-05 08:35 by srittau, last changed 2017-11-02 23:55 by srittau. This issue is now closed.

Messages (4)
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.
History
Date User Action Args
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