Skip to content

Commit 79d2e62

Browse files
ypankovychpganssleRémi Lapeyre
authored
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>
1 parent ffae932 commit 79d2e62

File tree

7 files changed

+15
-5
lines changed

7 files changed

+15
-5
lines changed

Doc/library/pathlib.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ Pure paths provide the following methods and properties:
337337
PureWindowsPath('c:/')
338338

339339
.. versionchanged:: 3.10
340-
Slice support was added.
340+
The parents sequence now supports :term:`slices <slice>` and negative index values.
341341

342342
.. data:: PurePath.parent
343343

Doc/whatsnew/3.10.rst

+5-1
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,13 @@ pipe. (Contributed by Pablo Galindo in :issue:`41625`.)
247247
pathlib
248248
-------
249249

250-
Added slice support to :meth:`~pathlib.Path.parents`.
250+
Added slice support to :attr:`PurePath.parents <pathlib.PurePath.parents>`.
251251
(Contributed by Joshua Cannon in :issue:`35498`)
252252

253+
Added negative indexing support to :attr:`PurePath.parents
254+
<pathlib.PurePath.parents>`.
255+
(Contributed by Yaroslav Pankovych in :issue:`21041`)
256+
253257
py_compile
254258
----------
255259

Lib/pathlib.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,8 @@ def __len__(self):
632632
def __getitem__(self, idx):
633633
if isinstance(idx, slice):
634634
return tuple(self[i] for i in range(*idx.indices(len(self))))
635-
if idx < 0 or idx >= len(self):
635+
636+
if idx >= len(self) or idx < -len(self):
636637
raise IndexError(idx)
637638
return self._pathcls._from_parsed_parts(self._drv, self._root,
638639
self._parts[:-idx - 1])

Lib/test/test_pathlib.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,9 @@ def test_parents_common(self):
440440
self.assertEqual(par[0], P('a/b'))
441441
self.assertEqual(par[1], P('a'))
442442
self.assertEqual(par[2], P('.'))
443+
self.assertEqual(par[-1], P('.'))
444+
self.assertEqual(par[-2], P('a'))
445+
self.assertEqual(par[-3], P('a/b'))
443446
self.assertEqual(par[0:1], (P('a/b'),))
444447
self.assertEqual(par[:2], (P('a/b'), P('a')))
445448
self.assertEqual(par[:-1], (P('a/b'), P('a')))
@@ -448,7 +451,7 @@ def test_parents_common(self):
448451
self.assertEqual(par[::-1], (P('.'), P('a'), P('a/b')))
449452
self.assertEqual(list(par), [P('a/b'), P('a'), P('.')])
450453
with self.assertRaises(IndexError):
451-
par[-1]
454+
par[-4]
452455
with self.assertRaises(IndexError):
453456
par[3]
454457
with self.assertRaises(TypeError):

Misc/ACKS

+1
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,7 @@ Michael Otteneder
12831283
Richard Oudkerk
12841284
Russel Owen
12851285
Joonas Paalasmaa
1286+
Yaroslav Pankovych
12861287
Martin Packman
12871288
Elisha Paine
12881289
Shriphani Palakodety
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Add slice support to :meth:`~pathlib.Path.parents`.
1+
Add slice support to :attr:`pathlib.PurePath.parents`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:attr:`pathlib.PurePath.parents` now supports negative indexing. Patch contributed by Yaroslav Pankovych.

0 commit comments

Comments
 (0)