[Python-checkins] bpo-46556: emit `DeprecationWarning` from `pathlib.Path.__enter__()` (GH-30971)

brettcannon webhook-mailer at python.org
Tue Feb 8 16:01:46 EST 2022


https://github.com/python/cpython/commit/06e1701ad3956352bc0f42b8f51c2f8cc85bf378
commit: 06e1701ad3956352bc0f42b8f51c2f8cc85bf378
branch: main
author: Barney Gale <barney.gale at gmail.com>
committer: brettcannon <brett at python.org>
date: 2022-02-08T13:01:37-08:00
summary:

bpo-46556: emit `DeprecationWarning` from `pathlib.Path.__enter__()` (GH-30971)

In Python 3.9, Path.__exit__() was made a no-op and has never been documented.

Co-authored-by: Brett Cannon <brett at python.org>

files:
A Misc/NEWS.d/next/Library/2022-01-27-23-20-30.bpo-46556.tlpAgS.rst
M Lib/pathlib.py
M Lib/test/test_pathlib.py

diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index 7f4210e2b80c9..4763ab54f6ba8 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -877,17 +877,20 @@ def _make_child_relpath(self, part):
         return self._from_parsed_parts(self._drv, self._root, parts)
 
     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):
-        # https://bugs.python.org/issue39682
-        # In previous versions of pathlib, this method 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 the
-        # _closed attribute was removed, and this method made a no-op.
-        # This method and __enter__()/__exit__() should be deprecated and
-        # removed in the future.
         pass
 
     # Public API
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
index ec2baca18fd81..f03fcbef1b0a0 100644
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -1850,8 +1850,10 @@ def test_with(self):
         it = p.iterdir()
         it2 = p.iterdir()
         next(it2)
-        with p:
-            pass
+        # bpo-46556: path context managers are deprecated in Python 3.11.
+        with self.assertWarns(DeprecationWarning):
+            with p:
+                pass
         # Using a path as a context manager is a no-op, thus the following
         # operations should still succeed after the context manage exits.
         next(it)
@@ -1859,8 +1861,9 @@ def test_with(self):
         p.exists()
         p.resolve()
         p.absolute()
-        with p:
-            pass
+        with self.assertWarns(DeprecationWarning):
+            with p:
+                pass
 
     def test_chmod(self):
         p = self.cls(BASE) / 'fileA'
diff --git a/Misc/NEWS.d/next/Library/2022-01-27-23-20-30.bpo-46556.tlpAgS.rst b/Misc/NEWS.d/next/Library/2022-01-27-23-20-30.bpo-46556.tlpAgS.rst
new file mode 100644
index 0000000000000..1209e0e2bd8b0
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-01-27-23-20-30.bpo-46556.tlpAgS.rst
@@ -0,0 +1,2 @@
+Deprecate undocumented support for using a :class:`pathlib.Path` object as a
+context manager.



More information about the Python-checkins mailing list