contextlib2 icon indicating copy to clipboard operation
contextlib2 copied to clipboard

backport contextlib from python 3.11

Open graingert opened this issue 4 years ago • 5 comments

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?

graingert avatar Jul 01 '21 21:07 graingert

this will need to wait for a 3.11 to be available on github actions at least

graingert avatar Jul 01 '21 21:07 graingert

also support for contextlib.chdir will need the changes in typeshed first: https://github.com/python/typeshed/pull/6191

graingert avatar Oct 23 '21 09:10 graingert

Hello, is there any progress on this? I think Python 3.11 is now available on GitHub Actions.

hrnciar avatar Apr 25 '22 06:04 hrnciar

Fedora is now updating to 3.11 so it would be great if this could be fixed. Thanks.

opoplawski avatar Jun 14 '22 02:06 opoplawski

https://github.com/python/typeshed/pull/6191 is fixed, and Python 3.11 is available from GitHub Actions

mr-c avatar Dec 23 '22 10:12 mr-c

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)

ncoghlan avatar May 22 '24 14:05 ncoghlan

Noting the other changes since 3.10:

  • 3.11: new chdir CM
  • 3.12: improved handling of BaseExceptionGroup in suppress
  • potentially some other smaller bugfixes (3.12 changelog doesn't make the timing clear, but the diff on the update will)

ncoghlan avatar May 22 '24 14:05 ncoghlan

#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.

ncoghlan avatar May 22 '24 14:05 ncoghlan

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)

ncoghlan avatar May 23 '24 06:05 ncoghlan

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)

ncoghlan avatar May 23 '24 06:05 ncoghlan

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.

ncoghlan avatar May 23 '24 07:05 ncoghlan