Hi Simon,

On 2023-01-07 13:24, Simon McVittie wrote:
On Sat, 07 Jan 2023 at 10:23:19 +0200, Andrius Merkys wrote:
If I may, I would as well be grateful if someone could give a look at:

#1023972 [src:python-ase] FTBFS with Python 3.11 due to
pathlib.Path.__enter__() deprecation

I have no idea how to fix this and the upstream is silent.
My first thought on seeing this was: why were Path objects a context
manager in the first place? What would that mean?

Looking at the code in python3.10 and python3.11 pathlib, it seems the
reason this is deprecated is indeed that it didn't make sense:

     def __enter__(self):
         # In previous versions of pathlib, __exit__() marked this path as
         # closed; subsequent attempts to perform I/O would raise an IOError.
         # This functionality was never documented, and had the effect of
         # making Path objects mutable, contrary to PEP 428.
         # In Python 3.9 __exit__() was made a no-op.
         # In Python 3.11 __enter__() began emitting DeprecationWarning.
         # In Python 3.13 __enter__() and __exit__() should be removed.
         warnings.warn("pathlib.Path.__enter__() is deprecated and scheduled "
                       "for removal in Python 3.13; Path objects as a context "
                       "manager is a no-op",
                       DeprecationWarning, stacklevel=2)
         return self

     def __exit__(self, t, v, tb):
         pass

So the solution seems to be that if your package contains this:

     with some_path_object:
         do_stuff(some_path_object)

replace it with:

     do_stuff(some_path_object)

and if it contains:

     with Path(...) as my_path:
         do_stuff(my_path)

replace with:

     my_path = Path(...)
     do_stuff(my_path)

I hope that should be a relatively straightforward change.

Thanks a lot for the hint, this indeed worked. Failure in __enter__() threw me off tracks, but now I recall how it is related to 'with' construction.

Best wishes,
Andrius

Reply via email to