https://github.com/python/cpython/commit/0f47a3199c51ba7c49e72b4c645dbf599aa17be4
commit: 0f47a3199c51ba7c49e72b4c645dbf599aa17be4
branch: main
author: Barney Gale <[email protected]>
committer: barneygale <[email protected]>
date: 2024-11-09T18:21:53Z
summary:

pathlib ABCs: support initializing paths with no arguments (#126608)

In the past I've equivocated about whether to require at least one argument
in the `PurePathBase` (and `PathBase`) initializer, and what the default
should be if we make it optional. I now have a local use case that has
persuaded me to make it optional and default to the empty string (a
`zipp.Path`-like class that treats relative and absolute paths similarly.)
Happily this brings the base class more in line with `PurePath` and `Path`.

files:
M Lib/pathlib/_abc.py
M Lib/test/test_pathlib/test_pathlib_abc.py

diff --git a/Lib/pathlib/_abc.py b/Lib/pathlib/_abc.py
index 43e6624934b045..e9e46e511bddf1 100644
--- a/Lib/pathlib/_abc.py
+++ b/Lib/pathlib/_abc.py
@@ -124,14 +124,12 @@ class PurePathBase:
     parser = ParserBase()
     _globber = PathGlobber
 
-    def __init__(self, arg, *args):
-        paths = [arg]
-        paths.extend(args)
-        for path in paths:
-            if not isinstance(path, str):
+    def __init__(self, *args):
+        for arg in args:
+            if not isinstance(arg, str):
                 raise TypeError(
-                    f"path should be a str, not {type(path).__name__!r}")
-        self._raw_paths = paths
+                    f"argument should be a str, not {type(arg).__name__!r}")
+        self._raw_paths = list(args)
         self._resolving = False
 
     def with_segments(self, *pathsegments):
@@ -270,7 +268,7 @@ def relative_to(self, other, *, walk_up=False):
                 raise ValueError(f"'..' segment in {str(other)!r} cannot be 
walked")
             else:
                 parts0.append('..')
-        return self.with_segments('', *reversed(parts0))
+        return self.with_segments(*reversed(parts0))
 
     def is_relative_to(self, other):
         """Return True if the path is relative to another path or False.
@@ -746,7 +744,7 @@ def cwd(cls):
         # enable users to replace the implementation of 'absolute()' in a
         # subclass and benefit from the new behaviour here. This works because
         # os.path.abspath('.') == os.getcwd().
-        return cls('').absolute()
+        return cls().absolute()
 
     def expanduser(self):
         """ Return a new path with expanded ~ and ~user constructs
diff --git a/Lib/test/test_pathlib/test_pathlib_abc.py 
b/Lib/test/test_pathlib/test_pathlib_abc.py
index d155e7c5bb9935..bb2e4187ef9574 100644
--- a/Lib/test/test_pathlib/test_pathlib_abc.py
+++ b/Lib/test/test_pathlib/test_pathlib_abc.py
@@ -148,6 +148,7 @@ def test_constructor_common(self):
         P = self.cls
         p = P('a')
         self.assertIsInstance(p, P)
+        P()
         P('a', 'b', 'c')
         P('/a', 'b', 'c')
         P('a/b/c')

_______________________________________________
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]

Reply via email to