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) *  |
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) *  |
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) *  |
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) *  |
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) *  |
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 |
|
|