You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2017-11-02.22:56:04.702>created_at=<Date2017-10-05.08:35:17.837>labels= ['3.7', 'invalid']
title='one-argument version for Generator.typing'updated_at=<Date2021-12-01.01:34:39.694>user='https://github.com/srittau'
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.)
You can use Iterator type, for example this works in mypy (didn't test in other type checkers):
deff() ->Iterator[int]:
yield42
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:
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.
zeth commented on Dec 1, 2021
zethmannequin
Mannequin
More actions
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.
Activity
srittau commentedon Oct 5, 2017
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.)
ilevkivskyi commentedon Oct 6, 2017
You can use Iterator type, for example this works in mypy (didn't test in other type checkers):
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:
this is supported by mypy as well.
ilevkivskyi commentedon Nov 2, 2017
Since there is no response for few weeks I assume this works for you.
srittau commentedon Nov 2, 2017
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.
zeth commentedon Dec 1, 2021
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.