Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -968,17 +968,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
Expand Down
11 changes: 7 additions & 4 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1830,17 +1830,20 @@ 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)
next(it2)
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'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Deprecate undocumented support for using a :class:`pathlib.Path` object as a
context manager.