Issue 35498: Parents objects in pathlib.Path don't support slices as getitem arguments (original) (raw)

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 "", line 1, in 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 . 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.
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) https://github.com/python/cpython/commit/452058448335b39c784af9a047f9c4a1767c0b00
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) https://github.com/python/cpython/commit/79d2e62c008446fbbc6f264bb8a30e2d38b6ff58
History
Date User Action Args
2022-04-11 14:59:09 admin set github: 79679
2020-11-23 20:06:38 p-ganssle set messages: +
2020-11-20 15:46:16 p-ganssle set status: open -> closeddependencies: - pathlib.PurePath.parents rejects negative indexesresolution: fixedstage: patch review -> resolved
2020-11-20 15:40:47 p-ganssle set messages: +
2020-11-19 18:59:32 p-ganssle set nosy: + p-gansslemessages: + versions: + Python 3.10, - Python 3.8
2019-09-13 13:43:13 thejcannon set messages: +
2019-09-13 10:32:10 mdk set nosy: + mdkmessages: +
2018-12-17 21:52:04 brett.cannon set nosy: + brett.cannon
2018-12-16 11:27:04 serhiy.storchaka set versions: + Python 3.8, - Python 3.6nosy: + serhiy.storchakamessages: + dependencies: + pathlib.PurePath.parents rejects negative indexes
2018-12-14 19:22:09 thejcannon set messages: +
2018-12-14 19:13:25 thejcannon set keywords: + patchstage: patch reviewpull_requests: + <pull%5Frequest10401>
2018-12-14 16:55:17 thejcannon create