
On Wed, 1 Jun 2022 at 23:55, Serhiy Storchaka <storchaka@gmail.com> wrote:
31.05.22 16:21, Chris Angelico пише:
On Tue, 31 May 2022 at 23:00, Aaron L via Python-ideas <python-ideas@python.org> wrote:
After getting used to writing async functions, I’ve been wanting use a similar syntax to declare generator functions.
What's the advantage? You can just use normal function syntax to define them, and it works correctly. Do you need the ability to declare that it's a generator even without any yields in it?
The advantage is that you cannot accidentally turn a function into a generator by adding "yield". If the result of the call is ignored (it is expected to be None), this bug can live a long time. It is a common issue: test containing yield always passes. Since 3.11 the unittest module emits a warning if a test method returns not None, but it will not solve all problems: the test can call helpers, and if they are generators, the call is virtually no-op. This error can also occur in non-test code.
That might be nice, but without a massive backward compatibility break (or another keyword for non-generator functions), it can't happen. Is there any advantage to being able to declare that it must be a generator (as opposed to simply returning a generator object)? Maybe I just don't work on the right sorts of codebases. ChrisA