
01.06.22 16:59, Chris Angelico пише:
On Wed, 1 Jun 2022 at 23:55, Serhiy Storchaka <storchaka@gmail.com> wrote:
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)?
The advantage is not in the ability to declare that it must be a generator, but in the ability to declare that it must not be a generator (and this should be the default). Of course it is a massive backward compatibility break, and the transition should be very slow and careful. First introduce a new optional syntax for generator functions, after 4 or 5 years (when all maintained Python versions support the new syntax) start to emit a quiet warning for a generator function with old syntax, after few more years make it more loud, and finally, after yet more years, an error. In between, third-party linters can start complaining about old syntax, first these warnings will be disable by default. It all will take 8-10 years or more. Or we can just break the world in Python 4.0 (but this is not the plan).