contextlib2
contextlib2 copied to clipboard
backport contextlib from python 3.11
https://github.com/python/cpython/commit/6cb145d23f5cf69b6d7414877d142747cd3d134c
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?
this will need to wait for a 3.11 to be available on github actions at least
also support for contextlib.chdir will need the changes in typeshed first: https://github.com/python/typeshed/pull/6191
Hello, is there any progress on this? I think Python 3.11 is now available on GitHub Actions.
Fedora is now updating to 3.11 so it would be great if this could be fixed. Thanks.
https://github.com/python/typeshed/pull/6191 is fixed, and Python 3.11 is available from GitHub Actions
For the TypeError/AttributeError change, I'll likely just add a small snippet of code to set an _IncompatibilityError module global at import time. Conditionally importing the stdlib versions doesn't work since one of goals of contextlib2 is that its behaviour should mostly be based on the package version rather than the Python version (while in this specific case raising the same error as the underlying statements would raise feels like the right approach, pulling in all future changes in any 3.x release feels like overkill).
Since it has taken me so long to do anything about it, I've also updated the issue title to cover syncing with 3.12 rather than with 3.11 (there haven't actually been any changes to the stdlib module since 3.12.1, so this is effectively syncing with 3.13, but I think describing it as syncing with 3.12 will be clearer)
Noting the other changes since 3.10:
- 3.11: new
chdirCM - 3.12: improved handling of
BaseExceptionGroupinsuppress - potentially some other smaller bugfixes (3.12 changelog doesn't make the timing clear, but the diff on the update will)
#58 fixed the 3.11+ test suite compatibility issue noted here and in #51
#59 pulled in the latest typeshed stubs as part of getting mypy stubcheck running again in CI, so it will just be a matter of uncommenting the chdir stub once the library itself has been updated.
Saving the old patch files proved worthwhile, since they mostly applied cleanly to get https://github.com/jazzband/contextlib2/pull/60 started.
The updated patch files are included in the sync PR (they're a convenient way to review what has changed from upstream and make sure it all looks intentional)
The dependency on the data and ziptestdata folders has also been clarified (the chdir tests don't care about their contents, just their existence as easily located targets to switch into)
The way the AttributeError handling worked out, the structure of the code meant that the runtime change is that the exception type to catch and replace with TypeError is set at import time. On the older versions, it gets set to a custom Exception subclass, so it will never catch anything, while on 3.11+ it gets set to AttributeError, and hence makes the same replacement the 3.11+ standard library version does.