Description
Documentation
Documentation for typing.Generator
currently says:
If your generator will only yield values, set the
SendType
andReturnType
toNone
While not obviously wrong, using None
as SendType
looks imprecise: we want a typechecker to warn about .send(t)
calls for any t
, right? Using that method likely indicates that the generator is used incorrectly.
Now type checkers will reject .send(t)
calls for all t
types except None
and Any
.
Since python 3.11 we have a Never
type - effectively a "please don't" type. So setting SendType
to Never
instead of None
would be more useful from the interface declaration perspective: literally saying "please don't send anything here".
I do understand that None
is there to match runtime behaviour closer since calling it.send(None)
is equivalent to calling next(it)
for the next time, so None
is formally correct. However, there's no benefit ever from doing it.send(None)
if the generator does not use sent values - calling next(it)
is clearly preferable.
My suggested solution is to recommend typing.Never
as SendType
in such cases in documentation. I'm ready to write a PR with this change.