Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parents objects in pathlib.Path don't support slices as __getitem__ arguments #79679

Closed
thejcannon mannequin opened this issue Dec 14, 2018 · 8 comments
Closed

Parents objects in pathlib.Path don't support slices as __getitem__ arguments #79679

thejcannon mannequin opened this issue Dec 14, 2018 · 8 comments
Labels
3.10 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@thejcannon
Copy link
Mannequin

thejcannon mannequin commented Dec 14, 2018

BPO 35498
Nosy @brettcannon, @serhiy-storchaka, @JulienPalard, @pganssle, @thejcannon
PRs
  • bpo-35498: Added slice support to PathLib parents attribute. #11165
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-11-20.15:46:16.796>
    created_at = <Date 2018-12-14.16:55:17.375>
    labels = ['type-feature', 'library', '3.10']
    title = "Parents objects in pathlib.Path don't support slices as __getitem__ arguments"
    updated_at = <Date 2020-11-23.20:06:38.266>
    user = 'https://github.com/thejcannon'

    bugs.python.org fields:

    activity = <Date 2020-11-23.20:06:38.266>
    actor = 'p-ganssle'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-11-20.15:46:16.796>
    closer = 'p-ganssle'
    components = ['Library (Lib)']
    creation = <Date 2018-12-14.16:55:17.375>
    creator = 'thejcannon'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 35498
    keywords = ['patch']
    message_count = 8.0
    messages = ['331841', '331854', '331923', '352283', '352331', '381453', '381485', '381695']
    nosy_count = 5.0
    nosy_names = ['brett.cannon', 'serhiy.storchaka', 'mdk', 'p-ganssle', 'thejcannon']
    pr_nums = ['11165']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue35498'
    versions = ['Python 3.10']

    @thejcannon
    Copy link
    Mannequin Author

    thejcannon mannequin commented Dec 14, 2018

    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.

    @thejcannon thejcannon mannequin added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Dec 14, 2018
    @thejcannon
    Copy link
    Mannequin Author

    thejcannon mannequin commented Dec 14, 2018

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

    @serhiy-storchaka
    Copy link
    Member

    See also bpo-21041. 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.

    @serhiy-storchaka serhiy-storchaka added the 3.8 only security fixes label Dec 16, 2018
    @JulienPalard
    Copy link
    Member

    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.

    @thejcannon
    Copy link
    Mannequin Author

    thejcannon mannequin commented Sep 13, 2019

    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.

    @pganssle
    Copy link
    Member

    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.

    @pganssle pganssle added 3.10 only security fixes and removed 3.8 only security fixes labels Nov 19, 2020
    @pganssle
    Copy link
    Member

    New changeset 4520584 by Joshua Cannon in branch 'master':
    bpo-35498: Added slice support to PathLib parents attribute. (GH-11165)
    4520584

    @pganssle
    Copy link
    Member

    New changeset 79d2e62 by Yaroslav Pankovych in branch 'master':
    Added support for negative indexes to PurePath.parents (GH-21799)
    79d2e62

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.10 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants