Skip to content

backport contextlib from python 3.12 #43

Closed
@graingert

Description

@graingert

python/cpython@6cb145d

A TypeError is now raised instead of an AttributeError in contextlib.ExitStack.enter_context() and contextlib.AsyncExitStack.enter_async_context() for objects which do not support the context manager or asynchronous context manager protocols correspondingly. (Contributed by Serhiy Storchaka in bpo-44471.)

however this is only to match the change in behaviour of the async with and with statements in 3.11:

A TypeError is now raised instead of an AttributeError in with and async with statements for objects which do not support the context manager or asynchronous context manager protocols correspondingly. (Contributed by Serhiy Storchaka in bpo-12022.)

so would need conditional checking.

perhaps:

if sys.version_info >= (3, 11):
    from contextlib import ExitStack, AsyncExitStack
else:
    class ExitStack(...):
        ...

    class AsyncExitStack(...):
        ...

would be best?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions