[issue29302] add contextlib.AsyncExitStack

Yury Selivanov report at bugs.python.org
Sat Aug 19 12:44:25 EDT 2017


Yury Selivanov added the comment:

> I'm not sure about type() to get a class object and calling __aenter__, __aexit__ through it: that makes it hard to mock these classes as Mock's spec= relies on __class__ and type() seem to ignore it (learned it a hard way.

Looking up __dunder__ methods on the class is how it should be done as that's how the rest of Python works.  And that's how ExitStack is currently implemented for synchronous "with" blocks.  We won't be able to change this behaviour even if we wanted to, so it stays.

Here's an example:

    class Foo:
        def __init__(self):
            self.__aenter__ = ...
            self.__aexit__ = ...

If we implement AsyncExitStack to lookup __anter__ directly on the object, then the above Foo class would be supported by it, but at the same time rejected by the 'async with' statement.

> Yury, I could take a second look and try to change this into a patch if that's OK.

By all means you can submit a PR!

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29302>
_______________________________________


More information about the Python-bugs-list mailing list