https://github.com/python/cpython/commit/a55dffd66dbddfd50c8f3de195218d041d26bd3c
commit: a55dffd66dbddfd50c8f3de195218d041d26bd3c
branch: main
author: Barney Gale <[email protected]>
committer: barneygale <[email protected]>
date: 2025-03-01T21:24:19Z
summary:
GH-127381: pathlib ABCs: remove `ReadablePath.exists()` and `is_*()` (#130520)
Remove `ReadablePath` methods duplicated by `ReadablePath.info`. To be
specific, we remove `exists()`, `is_dir()`, `is_file()` and `is_symlink()`.
The public `Path` class retains these methods.
files:
M Lib/pathlib/_abc.py
M Lib/test/test_pathlib/test_pathlib.py
M Lib/test/test_pathlib/test_pathlib_abc.py
diff --git a/Lib/pathlib/_abc.py b/Lib/pathlib/_abc.py
index 930701d4789f5c..cb5af3230bfd67 100644
--- a/Lib/pathlib/_abc.py
+++ b/Lib/pathlib/_abc.py
@@ -219,38 +219,6 @@ def info(self):
"""
raise NotImplementedError
- def exists(self, *, follow_symlinks=True):
- """
- Whether this path exists.
-
- This method normally follows symlinks; to check whether a symlink
exists,
- add the argument follow_symlinks=False.
- """
- info = self.joinpath().info
- return info.exists(follow_symlinks=follow_symlinks)
-
- def is_dir(self, *, follow_symlinks=True):
- """
- Whether this path is a directory.
- """
- info = self.joinpath().info
- return info.is_dir(follow_symlinks=follow_symlinks)
-
- def is_file(self, *, follow_symlinks=True):
- """
- Whether this path is a regular file (also True for symlinks pointing
- to regular files).
- """
- info = self.joinpath().info
- return info.is_file(follow_symlinks=follow_symlinks)
-
- def is_symlink(self):
- """
- Whether this path is a symbolic link.
- """
- info = self.joinpath().info
- return info.is_symlink()
-
@abstractmethod
def __open_rb__(self, buffering=-1):
"""
diff --git a/Lib/test/test_pathlib/test_pathlib.py
b/Lib/test/test_pathlib/test_pathlib.py
index 182d054de9d234..4bb62daecb6356 100644
--- a/Lib/test/test_pathlib/test_pathlib.py
+++ b/Lib/test/test_pathlib/test_pathlib.py
@@ -2453,6 +2453,93 @@ def test_lstat_nosymlink(self):
st = p.stat()
self.assertEqual(st, p.lstat())
+ def test_exists(self):
+ P = self.cls
+ p = P(self.base)
+ self.assertIs(True, p.exists())
+ self.assertIs(True, (p / 'dirA').exists())
+ self.assertIs(True, (p / 'fileA').exists())
+ self.assertIs(False, (p / 'fileA' / 'bah').exists())
+ if self.can_symlink:
+ self.assertIs(True, (p / 'linkA').exists())
+ self.assertIs(True, (p / 'linkB').exists())
+ self.assertIs(True, (p / 'linkB' / 'fileB').exists())
+ self.assertIs(False, (p / 'linkA' / 'bah').exists())
+ self.assertIs(False, (p / 'brokenLink').exists())
+ self.assertIs(True, (p /
'brokenLink').exists(follow_symlinks=False))
+ self.assertIs(False, (p / 'foo').exists())
+ self.assertIs(False, P('/xyzzy').exists())
+ self.assertIs(False, P(self.base + '\udfff').exists())
+ self.assertIs(False, P(self.base + '\x00').exists())
+
+ def test_is_dir(self):
+ P = self.cls(self.base)
+ self.assertTrue((P / 'dirA').is_dir())
+ self.assertFalse((P / 'fileA').is_dir())
+ self.assertFalse((P / 'non-existing').is_dir())
+ self.assertFalse((P / 'fileA' / 'bah').is_dir())
+ if self.can_symlink:
+ self.assertFalse((P / 'linkA').is_dir())
+ self.assertTrue((P / 'linkB').is_dir())
+ self.assertFalse((P/ 'brokenLink').is_dir())
+ self.assertFalse((P / 'dirA\udfff').is_dir())
+ self.assertFalse((P / 'dirA\x00').is_dir())
+
+ def test_is_dir_no_follow_symlinks(self):
+ P = self.cls(self.base)
+ self.assertTrue((P / 'dirA').is_dir(follow_symlinks=False))
+ self.assertFalse((P / 'fileA').is_dir(follow_symlinks=False))
+ self.assertFalse((P / 'non-existing').is_dir(follow_symlinks=False))
+ self.assertFalse((P / 'fileA' / 'bah').is_dir(follow_symlinks=False))
+ if self.can_symlink:
+ self.assertFalse((P / 'linkA').is_dir(follow_symlinks=False))
+ self.assertFalse((P / 'linkB').is_dir(follow_symlinks=False))
+ self.assertFalse((P/ 'brokenLink').is_dir(follow_symlinks=False))
+ self.assertFalse((P / 'dirA\udfff').is_dir(follow_symlinks=False))
+ self.assertFalse((P / 'dirA\x00').is_dir(follow_symlinks=False))
+
+ def test_is_file(self):
+ P = self.cls(self.base)
+ self.assertTrue((P / 'fileA').is_file())
+ self.assertFalse((P / 'dirA').is_file())
+ self.assertFalse((P / 'non-existing').is_file())
+ self.assertFalse((P / 'fileA' / 'bah').is_file())
+ if self.can_symlink:
+ self.assertTrue((P / 'linkA').is_file())
+ self.assertFalse((P / 'linkB').is_file())
+ self.assertFalse((P/ 'brokenLink').is_file())
+ self.assertFalse((P / 'fileA\udfff').is_file())
+ self.assertFalse((P / 'fileA\x00').is_file())
+
+ def test_is_file_no_follow_symlinks(self):
+ P = self.cls(self.base)
+ self.assertTrue((P / 'fileA').is_file(follow_symlinks=False))
+ self.assertFalse((P / 'dirA').is_file(follow_symlinks=False))
+ self.assertFalse((P / 'non-existing').is_file(follow_symlinks=False))
+ self.assertFalse((P / 'fileA' / 'bah').is_file(follow_symlinks=False))
+ if self.can_symlink:
+ self.assertFalse((P / 'linkA').is_file(follow_symlinks=False))
+ self.assertFalse((P / 'linkB').is_file(follow_symlinks=False))
+ self.assertFalse((P/ 'brokenLink').is_file(follow_symlinks=False))
+ self.assertFalse((P / 'fileA\udfff').is_file(follow_symlinks=False))
+ self.assertFalse((P / 'fileA\x00').is_file(follow_symlinks=False))
+
+ def test_is_symlink(self):
+ P = self.cls(self.base)
+ self.assertFalse((P / 'fileA').is_symlink())
+ self.assertFalse((P / 'dirA').is_symlink())
+ self.assertFalse((P / 'non-existing').is_symlink())
+ self.assertFalse((P / 'fileA' / 'bah').is_symlink())
+ if self.can_symlink:
+ self.assertTrue((P / 'linkA').is_symlink())
+ self.assertTrue((P / 'linkB').is_symlink())
+ self.assertTrue((P/ 'brokenLink').is_symlink())
+ self.assertIs((P / 'fileA\udfff').is_file(), False)
+ self.assertIs((P / 'fileA\x00').is_file(), False)
+ if self.can_symlink:
+ self.assertIs((P / 'linkA\udfff').is_file(), False)
+ self.assertIs((P / 'linkA\x00').is_file(), False)
+
def test_is_junction_false(self):
P = self.cls(self.base)
self.assertFalse((P / 'fileA').is_junction())
diff --git a/Lib/test/test_pathlib/test_pathlib_abc.py
b/Lib/test/test_pathlib/test_pathlib_abc.py
index b64924d06d420b..efc6c17962e261 100644
--- a/Lib/test/test_pathlib/test_pathlib_abc.py
+++ b/Lib/test/test_pathlib/test_pathlib_abc.py
@@ -1007,25 +1007,6 @@ def test_is_readable(self):
p = self.cls(self.base)
self.assertIsInstance(p, ReadablePath)
- def test_exists(self):
- P = self.cls
- p = P(self.base)
- self.assertIs(True, p.exists())
- self.assertIs(True, (p / 'dirA').exists())
- self.assertIs(True, (p / 'fileA').exists())
- self.assertIs(False, (p / 'fileA' / 'bah').exists())
- if self.can_symlink:
- self.assertIs(True, (p / 'linkA').exists())
- self.assertIs(True, (p / 'linkB').exists())
- self.assertIs(True, (p / 'linkB' / 'fileB').exists())
- self.assertIs(False, (p / 'linkA' / 'bah').exists())
- self.assertIs(False, (p / 'brokenLink').exists())
- self.assertIs(True, (p /
'brokenLink').exists(follow_symlinks=False))
- self.assertIs(False, (p / 'foo').exists())
- self.assertIs(False, P('/xyzzy').exists())
- self.assertIs(False, P(self.base + '\udfff').exists())
- self.assertIs(False, P(self.base + '\x00').exists())
-
def test_magic_open(self):
p = self.cls(self.base)
with magic_open(p / 'fileA', 'r') as f:
@@ -1058,17 +1039,8 @@ def test_iterdir_nodir(self):
def test_iterdir_info(self):
p = self.cls(self.base)
for child in p.iterdir():
- info = child.info
- self.assertIsInstance(info, PathInfo)
- self.assertEqual(info.exists(), child.exists())
- self.assertEqual(info.is_dir(), child.is_dir())
- self.assertEqual(info.is_file(), child.is_file())
- self.assertEqual(info.is_symlink(), child.is_symlink())
- self.assertTrue(info.exists(follow_symlinks=False))
- self.assertEqual(info.is_dir(follow_symlinks=False),
- child.is_dir(follow_symlinks=False))
- self.assertEqual(info.is_file(follow_symlinks=False),
- child.is_file(follow_symlinks=False))
+ self.assertIsInstance(child.info, PathInfo)
+ self.assertTrue(child.info.exists(follow_symlinks=False))
def test_glob_common(self):
def _check(glob, expected):
@@ -1248,74 +1220,6 @@ def test_info_is_symlink(self):
self.assertFalse((p / 'fileA\udfff').info.is_symlink())
self.assertFalse((p / 'fileA\x00').info.is_symlink())
- def test_is_dir(self):
- P = self.cls(self.base)
- self.assertTrue((P / 'dirA').is_dir())
- self.assertFalse((P / 'fileA').is_dir())
- self.assertFalse((P / 'non-existing').is_dir())
- self.assertFalse((P / 'fileA' / 'bah').is_dir())
- if self.can_symlink:
- self.assertFalse((P / 'linkA').is_dir())
- self.assertTrue((P / 'linkB').is_dir())
- self.assertFalse((P/ 'brokenLink').is_dir())
- self.assertFalse((P / 'dirA\udfff').is_dir())
- self.assertFalse((P / 'dirA\x00').is_dir())
-
- def test_is_dir_no_follow_symlinks(self):
- P = self.cls(self.base)
- self.assertTrue((P / 'dirA').is_dir(follow_symlinks=False))
- self.assertFalse((P / 'fileA').is_dir(follow_symlinks=False))
- self.assertFalse((P / 'non-existing').is_dir(follow_symlinks=False))
- self.assertFalse((P / 'fileA' / 'bah').is_dir(follow_symlinks=False))
- if self.can_symlink:
- self.assertFalse((P / 'linkA').is_dir(follow_symlinks=False))
- self.assertFalse((P / 'linkB').is_dir(follow_symlinks=False))
- self.assertFalse((P/ 'brokenLink').is_dir(follow_symlinks=False))
- self.assertFalse((P / 'dirA\udfff').is_dir(follow_symlinks=False))
- self.assertFalse((P / 'dirA\x00').is_dir(follow_symlinks=False))
-
- def test_is_file(self):
- P = self.cls(self.base)
- self.assertTrue((P / 'fileA').is_file())
- self.assertFalse((P / 'dirA').is_file())
- self.assertFalse((P / 'non-existing').is_file())
- self.assertFalse((P / 'fileA' / 'bah').is_file())
- if self.can_symlink:
- self.assertTrue((P / 'linkA').is_file())
- self.assertFalse((P / 'linkB').is_file())
- self.assertFalse((P/ 'brokenLink').is_file())
- self.assertFalse((P / 'fileA\udfff').is_file())
- self.assertFalse((P / 'fileA\x00').is_file())
-
- def test_is_file_no_follow_symlinks(self):
- P = self.cls(self.base)
- self.assertTrue((P / 'fileA').is_file(follow_symlinks=False))
- self.assertFalse((P / 'dirA').is_file(follow_symlinks=False))
- self.assertFalse((P / 'non-existing').is_file(follow_symlinks=False))
- self.assertFalse((P / 'fileA' / 'bah').is_file(follow_symlinks=False))
- if self.can_symlink:
- self.assertFalse((P / 'linkA').is_file(follow_symlinks=False))
- self.assertFalse((P / 'linkB').is_file(follow_symlinks=False))
- self.assertFalse((P/ 'brokenLink').is_file(follow_symlinks=False))
- self.assertFalse((P / 'fileA\udfff').is_file(follow_symlinks=False))
- self.assertFalse((P / 'fileA\x00').is_file(follow_symlinks=False))
-
- def test_is_symlink(self):
- P = self.cls(self.base)
- self.assertFalse((P / 'fileA').is_symlink())
- self.assertFalse((P / 'dirA').is_symlink())
- self.assertFalse((P / 'non-existing').is_symlink())
- self.assertFalse((P / 'fileA' / 'bah').is_symlink())
- if self.can_symlink:
- self.assertTrue((P / 'linkA').is_symlink())
- self.assertTrue((P / 'linkB').is_symlink())
- self.assertTrue((P/ 'brokenLink').is_symlink())
- self.assertIs((P / 'fileA\udfff').is_file(), False)
- self.assertIs((P / 'fileA\x00').is_file(), False)
- if self.can_symlink:
- self.assertIs((P / 'linkA\udfff').is_file(), False)
- self.assertIs((P / 'linkA\x00').is_file(), False)
-
class WritablePathTest(JoinablePathTest):
cls = DummyWritablePath
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]