From 0138810223716995e19bceef2036845ca3645b07 Mon Sep 17 00:00:00 2001 From: yaroslavpankovych Date: Sun, 9 Aug 2020 20:44:44 +0300 Subject: [PATCH] Added support for negative indexes to PurePath.parents MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Co-authored-by: Rémi Lapeyre --- Doc/library/pathlib.rst | 2 +- Doc/whatsnew/3.10.rst | 6 +++++- Lib/pathlib.py | 3 ++- Lib/test/test_pathlib.py | 5 ++++- Misc/ACKS | 1 + .../next/Library/2018-12-14-13-29-17.bpo-35498.LEJHl7.rst | 2 +- .../next/Library/2020-08-10-15-06-55.bpo-21041.cYz1eL.rst | 1 + 7 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2020-08-10-15-06-55.bpo-21041.cYz1eL.rst diff --git a/Doc/library/pathlib.rst b/Doc/library/pathlib.rst index 2071e7ed5f4bfe..2bbf3aad619884 100644 --- a/Doc/library/pathlib.rst +++ b/Doc/library/pathlib.rst @@ -337,7 +337,7 @@ Pure paths provide the following methods and properties: PureWindowsPath('c:/') .. versionchanged:: 3.10 - Slice support was added. + The parents sequence now supports :term:`slices ` and negative index values. .. data:: PurePath.parent diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst index ce66b1de1b7f20..f3e433abf08283 100644 --- a/Doc/whatsnew/3.10.rst +++ b/Doc/whatsnew/3.10.rst @@ -247,9 +247,13 @@ pipe. (Contributed by Pablo Galindo in :issue:`41625`.) pathlib ------- -Added slice support to :meth:`~pathlib.Path.parents`. +Added slice support to :attr:`PurePath.parents `. (Contributed by Joshua Cannon in :issue:`35498`) +Added negative indexing support to :attr:`PurePath.parents +`. +(Contributed by Yaroslav Pankovych in :issue:`21041`) + py_compile ---------- diff --git a/Lib/pathlib.py b/Lib/pathlib.py index af310393c3e40e..531a699a40df49 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -632,7 +632,8 @@ def __len__(self): def __getitem__(self, idx): if isinstance(idx, slice): 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) return self._pathcls._from_parsed_parts(self._drv, self._root, self._parts[:-idx - 1]) diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py index f1451796b6427d..5e5e065b988aaf 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -440,6 +440,9 @@ def test_parents_common(self): self.assertEqual(par[0], P('a/b')) self.assertEqual(par[1], P('a')) 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[:2], (P('a/b'), P('a'))) self.assertEqual(par[:-1], (P('a/b'), P('a'))) @@ -448,7 +451,7 @@ def test_parents_common(self): self.assertEqual(par[::-1], (P('.'), P('a'), P('a/b'))) self.assertEqual(list(par), [P('a/b'), P('a'), P('.')]) with self.assertRaises(IndexError): - par[-1] + par[-4] with self.assertRaises(IndexError): par[3] with self.assertRaises(TypeError): diff --git a/Misc/ACKS b/Misc/ACKS index 43030caae669d4..3e0916bf7c2f61 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -1282,6 +1282,7 @@ Michael Otteneder Richard Oudkerk Russel Owen Joonas Paalasmaa +Yaroslav Pankovych Martin Packman Elisha Paine Shriphani Palakodety diff --git a/Misc/NEWS.d/next/Library/2018-12-14-13-29-17.bpo-35498.LEJHl7.rst b/Misc/NEWS.d/next/Library/2018-12-14-13-29-17.bpo-35498.LEJHl7.rst index fb24ce027c2186..1ab0093fcde041 100644 --- a/Misc/NEWS.d/next/Library/2018-12-14-13-29-17.bpo-35498.LEJHl7.rst +++ b/Misc/NEWS.d/next/Library/2018-12-14-13-29-17.bpo-35498.LEJHl7.rst @@ -1 +1 @@ -Add slice support to :meth:`~pathlib.Path.parents`. +Add slice support to :attr:`pathlib.PurePath.parents`. diff --git a/Misc/NEWS.d/next/Library/2020-08-10-15-06-55.bpo-21041.cYz1eL.rst b/Misc/NEWS.d/next/Library/2020-08-10-15-06-55.bpo-21041.cYz1eL.rst new file mode 100644 index 00000000000000..4f14fd39d8827f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-08-10-15-06-55.bpo-21041.cYz1eL.rst @@ -0,0 +1 @@ +:attr:`pathlib.PurePath.parents` now supports negative indexing. Patch contributed by Yaroslav Pankovych.