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

Created on 2017-10-05 08:35 by srittau, last changed 2017-10-06 17:06 by levkivskyi.

Messages (2)
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) * 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.
History
Date User Action Args
2017-10-06 17:06:28levkivskyisetnosy: + levkivskyi
messages: + msg303839
2017-10-05 08:35:17srittaucreate