Added support for negative indexes to PurePath.parents (GH-21799)
This commit also fixes up some of the overlapping documentation changed in bpo-35498, which added support for indexing with slices. Fixes bpo-21041. https://bugs.python.org/issue21041 Co-authored-by: Paul Ganssle <p.ganssle@gmail.com> Co-authored-by: Rémi Lapeyre <remi.lapeyre@henki.fr>
This commit is contained in:
parent
ffae93248a
commit
79d2e62c00
|
@ -337,7 +337,7 @@ Pure paths provide the following methods and properties:
|
||||||
PureWindowsPath('c:/')
|
PureWindowsPath('c:/')
|
||||||
|
|
||||||
.. versionchanged:: 3.10
|
.. versionchanged:: 3.10
|
||||||
Slice support was added.
|
The parents sequence now supports :term:`slices <slice>` and negative index values.
|
||||||
|
|
||||||
.. data:: PurePath.parent
|
.. data:: PurePath.parent
|
||||||
|
|
||||||
|
|
|
@ -247,9 +247,13 @@ pipe. (Contributed by Pablo Galindo in :issue:`41625`.)
|
||||||
pathlib
|
pathlib
|
||||||
-------
|
-------
|
||||||
|
|
||||||
Added slice support to :meth:`~pathlib.Path.parents`.
|
Added slice support to :attr:`PurePath.parents <pathlib.PurePath.parents>`.
|
||||||
(Contributed by Joshua Cannon in :issue:`35498`)
|
(Contributed by Joshua Cannon in :issue:`35498`)
|
||||||
|
|
||||||
|
Added negative indexing support to :attr:`PurePath.parents
|
||||||
|
<pathlib.PurePath.parents>`.
|
||||||
|
(Contributed by Yaroslav Pankovych in :issue:`21041`)
|
||||||
|
|
||||||
py_compile
|
py_compile
|
||||||
----------
|
----------
|
||||||
|
|
||||||
|
|
|
@ -632,7 +632,8 @@ class _PathParents(Sequence):
|
||||||
def __getitem__(self, idx):
|
def __getitem__(self, idx):
|
||||||
if isinstance(idx, slice):
|
if isinstance(idx, slice):
|
||||||
return tuple(self[i] for i in range(*idx.indices(len(self))))
|
return tuple(self[i] for i in range(*idx.indices(len(self))))
|
||||||
if idx < 0 or idx >= len(self):
|
|
||||||
|
if idx >= len(self) or idx < -len(self):
|
||||||
raise IndexError(idx)
|
raise IndexError(idx)
|
||||||
return self._pathcls._from_parsed_parts(self._drv, self._root,
|
return self._pathcls._from_parsed_parts(self._drv, self._root,
|
||||||
self._parts[:-idx - 1])
|
self._parts[:-idx - 1])
|
||||||
|
|
|
@ -440,6 +440,9 @@ class _BasePurePathTest(object):
|
||||||
self.assertEqual(par[0], P('a/b'))
|
self.assertEqual(par[0], P('a/b'))
|
||||||
self.assertEqual(par[1], P('a'))
|
self.assertEqual(par[1], P('a'))
|
||||||
self.assertEqual(par[2], P('.'))
|
self.assertEqual(par[2], P('.'))
|
||||||
|
self.assertEqual(par[-1], P('.'))
|
||||||
|
self.assertEqual(par[-2], P('a'))
|
||||||
|
self.assertEqual(par[-3], P('a/b'))
|
||||||
self.assertEqual(par[0:1], (P('a/b'),))
|
self.assertEqual(par[0:1], (P('a/b'),))
|
||||||
self.assertEqual(par[:2], (P('a/b'), P('a')))
|
self.assertEqual(par[:2], (P('a/b'), P('a')))
|
||||||
self.assertEqual(par[:-1], (P('a/b'), P('a')))
|
self.assertEqual(par[:-1], (P('a/b'), P('a')))
|
||||||
|
@ -448,7 +451,7 @@ class _BasePurePathTest(object):
|
||||||
self.assertEqual(par[::-1], (P('.'), P('a'), P('a/b')))
|
self.assertEqual(par[::-1], (P('.'), P('a'), P('a/b')))
|
||||||
self.assertEqual(list(par), [P('a/b'), P('a'), P('.')])
|
self.assertEqual(list(par), [P('a/b'), P('a'), P('.')])
|
||||||
with self.assertRaises(IndexError):
|
with self.assertRaises(IndexError):
|
||||||
par[-1]
|
par[-4]
|
||||||
with self.assertRaises(IndexError):
|
with self.assertRaises(IndexError):
|
||||||
par[3]
|
par[3]
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
|
|
|
@ -1283,6 +1283,7 @@ Michael Otteneder
|
||||||
Richard Oudkerk
|
Richard Oudkerk
|
||||||
Russel Owen
|
Russel Owen
|
||||||
Joonas Paalasmaa
|
Joonas Paalasmaa
|
||||||
|
Yaroslav Pankovych
|
||||||
Martin Packman
|
Martin Packman
|
||||||
Elisha Paine
|
Elisha Paine
|
||||||
Shriphani Palakodety
|
Shriphani Palakodety
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Add slice support to :meth:`~pathlib.Path.parents`.
|
Add slice support to :attr:`pathlib.PurePath.parents`.
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
:attr:`pathlib.PurePath.parents` now supports negative indexing. Patch contributed by Yaroslav Pankovych.
|
Loading…
Reference in New Issue