[issue37743] How should contextmanager/ContextDecorator work with generators?

Nick Coghlan report at bugs.python.org
Tue Sep 3 04:25:36 EDT 2019


Nick Coghlan <ncoghlan at gmail.com> added the comment:

This is a documentation bug, as the current behaviour is as intended, but the documented equivalence doesn't hold for generator functions: for a generator function, the CM will only be applied when the generator is instantiated, whereas the inline context manager version will be held open until the generator is closed or destroyed.

That said, an approach similar to the one discussed in #37398 could also be applied, here, with a separate "ContextDecorator.generator()" class method added to give the "wrapped yield from" behaviour. If anyone is interested in pursuing that, it can be filed as a separate enhancement issue (leaving this bug to cover the fact that the existing documentation is only accurate for regular synchronous functions)

----------
assignee: ncoghlan -> 
components: +Documentation -Library (Lib)

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37743>
_______________________________________


More information about the Python-bugs-list mailing list