mirror of
https://github.com/python/cpython.git
synced 2025-11-01 06:01:29 +00:00
GH-110109: Fix misleading pathlib._abc.PurePathBase repr (#113376)
`PurePathBase.__repr__()` produces a string like `MyPath('/foo')`. This
repr is incorrect/misleading when a subclass's `__init__()` method is
customized, which I expect to be the very common.
This commit moves the `__repr__()` method to `PurePath`, leaving
`PurePathBase` with the default `object` repr.
No user-facing changes because the `pathlib._abc` module remains private.
This commit is contained in:
parent
45e09f921b
commit
237e2cff00
4 changed files with 16 additions and 15 deletions
|
|
@ -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__)
|
||||
|
|
@ -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'))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue