This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Pathlib parents doesn't support slicing with negative indexes
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.10
Status: closed Resolution: duplicate
Dependencies: Superseder: pathlib.PurePath.parents rejects negative indexes
View: 21041
Assigned To: Nosy List: python-dev, remi.lapeyre, xtreak, ypank
Priority: normal Keywords: patch

Created on 2020-08-09 17:43 by ypank, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 21799 open python-dev, 2020-08-09 17:54
Messages (5)
msg375076 - (view) Author: Yaroslav Pankovych (ypank) * Date: 2020-08-09 17:43
As I can see, pathlib path parents don't support slicing with negative indexes: 

>>> import pathlib
>>> path = pathlib.PosixPath("some/very/long/path/here")
>>> path.parents[-1]
    raise IndexError(idx)
IndexError: -1

That's kinda weird for python. I mean, in regular list/etc if I need the last element, I'd normally do list[-1], but here to get the last parent, I need to actually know how many parents do I have. 

So now, I can do something like this:

>>> parents_count = len(path.parents) - 1
>>> path.parents[parents_count]

So that's how I can get the last parent. But is it pythonic? No.

So, I decided to fix this, and now we can do negative slicing:

>>> path.parents[-1] == path.parents[parents_count]
>>> path.parents[-2] == path.parents[parents_count - 1]

So what do you guys think about this?
msg375077 - (view) Author: Yaroslav Pankovych (ypank) * Date: 2020-08-09 17:56
Here's opened PR:
msg375082 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2020-08-09 21:12
This is a duplicate of 21041, it would be better to change the title of your PR so that it points to this bug report and to continue the discussion there.
msg375083 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2020-08-09 21:13
*This is a duplicate of issue 21041
msg375093 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020-08-10 04:13
Closing it as duplicate of issue21041. Thanks Remi. Yaroslav, feel free to discuss it in the other issue.
Date User Action Args
2022-04-11 14:59:34adminsetgithub: 85683
2020-08-10 04:13:11xtreaksetstatus: open -> closed

superseder: pathlib.PurePath.parents rejects negative indexes

nosy: + xtreak
messages: + msg375093
resolution: duplicate
stage: patch review -> resolved
2020-08-09 21:13:09remi.lapeyresetmessages: + msg375083
2020-08-09 21:12:54remi.lapeyresetnosy: + remi.lapeyre

messages: + msg375082
versions: - Python 3.5, Python 3.6, Python 3.7, Python 3.8, Python 3.9
2020-08-09 17:56:02ypanksetmessages: + msg375077
2020-08-09 17:54:09python-devsetkeywords: + patch
nosy: + python-dev

pull_requests: + pull_request20934
stage: patch review
2020-08-09 17:43:16ypankcreate