Skip to content

pathlib ABCs: Require one or more initialiser arguments #113885

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 2 commits into from
Jan 10, 2024
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
10 changes: 2 additions & 8 deletions Lib/pathlib/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,7 @@ def with_segments(self, *pathsegments):
def __str__(self):
"""Return the string representation of the path, suitable for
passing to system calls."""
paths = self._raw_paths
if len(paths) == 1:
return paths[0]
elif paths:
return self.pathmod.join(*paths)
else:
return ''
return self.pathmod.join(*self._raw_paths)

def as_posix(self):
"""Return the string representation of the path with forward (/)
Expand Down Expand Up @@ -838,7 +832,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
Expand Down
48 changes: 24 additions & 24 deletions Lib/test/test_pathlib/test_pathlib_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ def test_match_common(self):
self.assertFalse(P('c:/a/B.Py').match('C:/A/*.pY', case_sensitive=True))
self.assertTrue(P('/a/b/c.py').match('/A/*/*.Py', case_sensitive=False))
# Matching against empty path
self.assertFalse(P().match('*'))
self.assertTrue(P().match('**'))
self.assertFalse(P().match('**/*'))
self.assertFalse(P('').match('*'))
self.assertTrue(P('').match('**'))
self.assertFalse(P('').match('**/*'))

def test_parts_common(self):
# `parts` returns a tuple.
Expand All @@ -249,8 +249,8 @@ def test_parent_common(self):
p = P('a/b/c')
self.assertEqual(p.parent, P('a/b'))
self.assertEqual(p.parent.parent, P('a'))
self.assertEqual(p.parent.parent.parent, P())
self.assertEqual(p.parent.parent.parent.parent, P())
self.assertEqual(p.parent.parent.parent, P(''))
self.assertEqual(p.parent.parent.parent.parent, P(''))
# Anchored
p = P('/a/b/c')
self.assertEqual(p.parent, P('/a/b'))
Expand Down Expand Up @@ -478,20 +478,20 @@ def test_relative_to_common(self):
p = P('a/b')
self.assertRaises(TypeError, p.relative_to)
self.assertRaises(TypeError, p.relative_to, b'a')
self.assertEqual(p.relative_to(P()), P('a/b'))
self.assertEqual(p.relative_to(P('')), P('a/b'))
self.assertEqual(p.relative_to(''), P('a/b'))
self.assertEqual(p.relative_to(P('a')), P('b'))
self.assertEqual(p.relative_to('a'), P('b'))
self.assertEqual(p.relative_to('a/'), P('b'))
self.assertEqual(p.relative_to(P('a/b')), P())
self.assertEqual(p.relative_to('a/b'), P())
self.assertEqual(p.relative_to(P(), walk_up=True), P('a/b'))
self.assertEqual(p.relative_to(P('a/b')), P(''))
self.assertEqual(p.relative_to('a/b'), P(''))
self.assertEqual(p.relative_to(P(''), walk_up=True), P('a/b'))
self.assertEqual(p.relative_to('', walk_up=True), P('a/b'))
self.assertEqual(p.relative_to(P('a'), walk_up=True), P('b'))
self.assertEqual(p.relative_to('a', walk_up=True), P('b'))
self.assertEqual(p.relative_to('a/', walk_up=True), P('b'))
self.assertEqual(p.relative_to(P('a/b'), walk_up=True), P())
self.assertEqual(p.relative_to('a/b', walk_up=True), P())
self.assertEqual(p.relative_to(P('a/b'), walk_up=True), P(''))
self.assertEqual(p.relative_to('a/b', walk_up=True), P(''))
self.assertEqual(p.relative_to(P('a/c'), walk_up=True), P('../b'))
self.assertEqual(p.relative_to('a/c', walk_up=True), P('../b'))
self.assertEqual(p.relative_to(P('a/b/c'), walk_up=True), P('..'))
Expand All @@ -517,15 +517,15 @@ def test_relative_to_common(self):
self.assertEqual(p.relative_to(P('/a')), P('b'))
self.assertEqual(p.relative_to('/a'), P('b'))
self.assertEqual(p.relative_to('/a/'), P('b'))
self.assertEqual(p.relative_to(P('/a/b')), P())
self.assertEqual(p.relative_to('/a/b'), P())
self.assertEqual(p.relative_to(P('/a/b')), P(''))
self.assertEqual(p.relative_to('/a/b'), P(''))
self.assertEqual(p.relative_to(P('/'), walk_up=True), P('a/b'))
self.assertEqual(p.relative_to('/', walk_up=True), P('a/b'))
self.assertEqual(p.relative_to(P('/a'), walk_up=True), P('b'))
self.assertEqual(p.relative_to('/a', walk_up=True), P('b'))
self.assertEqual(p.relative_to('/a/', walk_up=True), P('b'))
self.assertEqual(p.relative_to(P('/a/b'), walk_up=True), P())
self.assertEqual(p.relative_to('/a/b', walk_up=True), P())
self.assertEqual(p.relative_to(P('/a/b'), walk_up=True), P(''))
self.assertEqual(p.relative_to('/a/b', walk_up=True), P(''))
self.assertEqual(p.relative_to(P('/a/c'), walk_up=True), P('../b'))
self.assertEqual(p.relative_to('/a/c', walk_up=True), P('../b'))
self.assertEqual(p.relative_to(P('/a/b/c'), walk_up=True), P('..'))
Expand All @@ -536,7 +536,7 @@ def test_relative_to_common(self):
self.assertRaises(ValueError, p.relative_to, P('/c'))
self.assertRaises(ValueError, p.relative_to, P('/a/b/c'))
self.assertRaises(ValueError, p.relative_to, P('/a/c'))
self.assertRaises(ValueError, p.relative_to, P())
self.assertRaises(ValueError, p.relative_to, P(''))
self.assertRaises(ValueError, p.relative_to, '')
self.assertRaises(ValueError, p.relative_to, P('a'))
self.assertRaises(ValueError, p.relative_to, P("../a"))
Expand All @@ -553,7 +553,7 @@ def test_is_relative_to_common(self):
p = P('a/b')
self.assertRaises(TypeError, p.is_relative_to)
self.assertRaises(TypeError, p.is_relative_to, b'a')
self.assertTrue(p.is_relative_to(P()))
self.assertTrue(p.is_relative_to(P('')))
self.assertTrue(p.is_relative_to(''))
self.assertTrue(p.is_relative_to(P('a')))
self.assertTrue(p.is_relative_to('a/'))
Expand All @@ -576,7 +576,7 @@ def test_is_relative_to_common(self):
self.assertFalse(p.is_relative_to(P('/c')))
self.assertFalse(p.is_relative_to(P('/a/b/c')))
self.assertFalse(p.is_relative_to(P('/a/c')))
self.assertFalse(p.is_relative_to(P()))
self.assertFalse(p.is_relative_to(P('')))
self.assertFalse(p.is_relative_to(''))
self.assertFalse(p.is_relative_to(P('a')))

Expand All @@ -590,7 +590,7 @@ class PathBaseTest(PurePathBaseTest):

def test_unsupported_operation(self):
P = self.cls
p = self.cls()
p = self.cls('')
e = UnsupportedOperation
self.assertRaises(e, p.stat)
self.assertRaises(e, p.lstat)
Expand Down Expand Up @@ -634,13 +634,13 @@ def test_unsupported_operation(self):

def test_as_uri_common(self):
e = UnsupportedOperation
self.assertRaises(e, self.cls().as_uri)
self.assertRaises(e, self.cls('').as_uri)

def test_fspath_common(self):
self.assertRaises(TypeError, os.fspath, self.cls())
self.assertRaises(TypeError, os.fspath, self.cls(''))

def test_as_bytes_common(self):
self.assertRaises(TypeError, bytes, self.cls())
self.assertRaises(TypeError, bytes, self.cls(''))


class DummyPathIO(io.BytesIO):
Expand Down Expand Up @@ -993,7 +993,7 @@ def _check(glob, expected):
_check(p.glob("*/"), ["dirA/", "dirB/", "dirC/", "dirE/", "linkB/"])

def test_glob_empty_pattern(self):
p = self.cls()
p = self.cls('')
with self.assertRaisesRegex(ValueError, 'Unacceptable pattern'):
list(p.glob(''))

Expand Down Expand Up @@ -1554,7 +1554,7 @@ def _check_complex_symlinks(self, link0_target):

# Resolve relative paths.
try:
self.cls().absolute()
self.cls('').absolute()
except UnsupportedOperation:
return
old_path = os.getcwd()
Expand Down