Issue 41511: Pathlib parents doesn't support slicing with negative indexes
Created on 2020-08-09 17:43 by ypank, last changed 2020-08-10 04:13 by xtreak. 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]
PosixPath('.')
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]
True
>>> path.parents[-2] == path.parents[parents_count - 1]
True
So what do you guys think about this?
|
|||
| msg375077 - (view) | Author: Yaroslav Pankovych (ypank) * | Date: 2020-08-09 17:56 | |
Here's opened PR: https://github.com/python/cpython/pull/21799 |
|||
| 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) * ![]() |
Date: 2020-08-10 04:13 | |
Closing it as duplicate of issue21041. Thanks Remi. Yaroslav, feel free to discuss it in the other issue. |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2020-08-10 04:13:11 | xtreak | set | status: open -> closed superseder: pathlib.PurePath.parents rejects negative indexes nosy:
+ xtreak |
| 2020-08-09 21:13:09 | remi.lapeyre | set | messages: + msg375083 |
| 2020-08-09 21:12:54 | remi.lapeyre | set | nosy:
+ remi.lapeyre messages:
+ msg375082 |
| 2020-08-09 17:56:02 | ypank | set | messages: + msg375077 |
| 2020-08-09 17:54:09 | python-dev | set | keywords:
+ patch nosy: + python-dev pull_requests:
+ pull_request20934 |
| 2020-08-09 17:43:16 | ypank | create | |
