classification
Title: Parents objects in pathlib.Path don't support slices as __getitem__ arguments
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.8
process
Status: open Resolution:
Dependencies: 21041 Superseder:
Assigned To: Nosy List: brett.cannon, mdk, serhiy.storchaka, thejcannon
Priority: normal Keywords: patch

Created on 2018-12-14 16:55 by thejcannon, last changed 2019-09-13 13:43 by thejcannon.

Pull Requests
URL Status Linked Edit
PR 11165 open thejcannon, 2018-12-14 19:13
Messages (5)
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\pathlib.py", 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: https://bugs.python.org/issue21041#msg352281 

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.
History
Date User Action Args
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