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: Parents objects in pathlib.Path don't support slices as __getitem__ arguments
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.10
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, mdk, p-ganssle, serhiy.storchaka, thejcannon
Priority: normal Keywords: patch

Created on 2018-12-14 16:55 by thejcannon, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 11165 merged thejcannon, 2018-12-14 19:13
Messages (8)
msg331841 - (view) Author: Joshua Cannon (thejcannon) * Date: 2018-12-14 16:55
I would expect the following to work:
>>> import pathlib
>>> pathlib.Path.cwd().parents[0:1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "...\Python36\lib\", line 593, in __getitem__
    if idx < 0 or idx >= len(self):
TypeError: '<' not supported between instances of 'slice' and 'int'

Since pathlib documents `parents` as a sequence-type, and slicing a sequence is pretty standard behavior.
msg331854 - (view) Author: Joshua Cannon (thejcannon) * Date: 2018-12-14 19:22
If it is deemed a bug which needs to be fixed, I've gone ahead and attached the PR to fix it.

CLA signage is pending approval at the company I work for, with most people out for the holidays (so it might be a day or two turnaround).
msg331923 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-12-16 11:27
See also issue21041. First than add support for slices, we should make a decision about negative indices.

In any case this is a new feature, which can be only added in the future 3.8 release.
msg352283 - (view) Author: Julien Palard (mdk) * (Python committer) Date: 2019-09-13 10:32
Just commented on the related issue: 

My question is: Why would you need to slice parents? parents already looks like the result of some slice.
msg352331 - (view) Author: Joshua Cannon (thejcannon) * Date: 2019-09-13 13:43
Pretty much the same reason you would want to slice any other sequence. You want some range of values.

top_most_3_dirs = myPath.parents[-3:]
grandparents_and_beyond = myPath.parents[1:]

The same goes for negative indexes.
msg381453 - (view) Author: Paul Ganssle (p-ganssle) * (Python committer) Date: 2020-11-19 18:59
One question I would have about this is that `.parents` is a lazily-calculated sequence, not a list or a tuple, so it's not immediately obvious what the return type of a slice would be. I don't think it makes sense to return, e.g. a `_PathParents` object with fewer parts, but I don't know if there's any traditional choice here, other than that the result of slicing Sequence is another Sequence.

The PR returns a list, but I'm inclined to say we should return a tuple.
msg381485 - (view) Author: Paul Ganssle (p-ganssle) * (Python committer) Date: 2020-11-20 15:40
New changeset 452058448335b39c784af9a047f9c4a1767c0b00 by Joshua Cannon in branch 'master':
bpo-35498: Added slice support to PathLib parents attribute. (GH-11165)
msg381695 - (view) Author: Paul Ganssle (p-ganssle) * (Python committer) Date: 2020-11-23 20:06
New changeset 79d2e62c008446fbbc6f264bb8a30e2d38b6ff58 by Yaroslav Pankovych in branch 'master':
Added support for negative indexes to PurePath.parents (GH-21799)
Date User Action Args
2022-04-11 14:59:09adminsetgithub: 79679
2020-11-23 20:06:38p-gansslesetmessages: + msg381695
2020-11-20 15:46:16p-gansslesetstatus: open -> closed
dependencies: - pathlib.PurePath.parents rejects negative indexes
resolution: fixed
stage: patch review -> resolved
2020-11-20 15:40:47p-gansslesetmessages: + msg381485
2020-11-19 18:59:32p-gansslesetnosy: + p-ganssle

messages: + msg381453
versions: + Python 3.10, - Python 3.8
2019-09-13 13:43:13thejcannonsetmessages: + msg352331
2019-09-13 10:32:10mdksetnosy: + mdk
messages: + msg352283
2018-12-17 21:52:04brett.cannonsetnosy: + brett.cannon
2018-12-16 11:27:04serhiy.storchakasetversions: + Python 3.8, - Python 3.6
nosy: + serhiy.storchaka

messages: + msg331923

dependencies: + pathlib.PurePath.parents rejects negative indexes
2018-12-14 19:22:09thejcannonsetmessages: + msg331854
2018-12-14 19:13:25thejcannonsetkeywords: + patch
stage: patch review
pull_requests: + pull_request10401
2018-12-14 16:55:17thejcannoncreate