FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

GH-110109: Fix misleading `pathlib._abc.PurePathBase` repr by barneygale · Pull Request #113376 · python/cpython · GitHub

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

Filter by extension

Filter by extension .py  (4) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
3 changes: 3 additions & 0 deletions Lib/pathlib/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ def __reduce__(self):
# when pickling related paths.
return (self.__class__, self.parts)

def __repr__(self):
return "{}({!r})".format(self.__class__.__name__, self.as_posix())

def __fspath__(self):
return str(self)

Expand Down
3 changes: 0 additions & 3 deletions Lib/pathlib/_abc.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,6 @@ def as_posix(self):
slashes."""
return str(self).replace(self.pathmod.sep, '/')

def __repr__(self):
return "{}({!r})".format(self.__class__.__name__, self.as_posix())

@property
def drive(self):
"""The drive prefix (letter or UNC path), if any."""
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_pathlib/test_pathlib.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ def test_pickling_common(self):
self.assertEqual(hash(pp), hash(p))
self.assertEqual(str(pp), str(p))

def test_repr_common(self):
for pathstr in ('a', 'a/b', 'a/b/c', '/', '/a/b', '/a/b/c'):
with self.subTest(pathstr=pathstr):
p = self.cls(pathstr)
clsname = p.__class__.__name__
r = repr(p)
# The repr() is in the form ClassName("forward-slashes path").
self.assertTrue(r.startswith(clsname + '('), r)
self.assertTrue(r.endswith(')'), r)
inner = r[len(clsname) + 1 : -1]
self.assertEqual(eval(inner), p.as_posix())

def test_fspath_common(self):
P = self.cls
p = P('a/b')
Expand Down
13 changes: 1 addition & 12 deletions Lib/test/test_pathlib/test_pathlib_abc.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def test_magic_methods(self):
self.assertFalse(hasattr(P, '__fspath__'))
self.assertFalse(hasattr(P, '__bytes__'))
self.assertIs(P.__reduce__, object.__reduce__)
self.assertIs(P.__repr__, object.__repr__)
self.assertIs(P.__hash__, object.__hash__)
self.assertIs(P.__eq__, object.__eq__)
self.assertIs(P.__lt__, object.__lt__)
Expand Down Expand Up @@ -227,18 +228,6 @@ def test_as_posix_common(self):
self.assertEqual(P(pathstr).as_posix(), pathstr)
# Other tests for as_posix() are in test_equivalences().

def test_repr_common(self):
for pathstr in ('a', 'a/b', 'a/b/c', '/', '/a/b', '/a/b/c'):
with self.subTest(pathstr=pathstr):
p = self.cls(pathstr)
clsname = p.__class__.__name__
r = repr(p)
# The repr() is in the form ClassName("forward-slashes path").
self.assertTrue(r.startswith(clsname + '('), r)
self.assertTrue(r.endswith(')'), r)
inner = r[len(clsname) + 1 : -1]
self.assertEqual(eval(inner), p.as_posix())

def test_eq_common(self):
P = self.cls
self.assertEqual(P('a/b'), P('a/b'))
Expand Down

Back | FazBrowse Home | New Git URL