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

pathlib.PurePath division raises TypeError instead of returning NotImplemented #78956

Closed
aiudirog mannequin opened this issue Sep 23, 2018 · 6 comments
Closed

pathlib.PurePath division raises TypeError instead of returning NotImplemented #78956

aiudirog mannequin opened this issue Sep 23, 2018 · 6 comments
Labels
3.8 3.9 stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@aiudirog
Copy link
Mannequin

aiudirog mannequin commented Sep 23, 2018

BPO 34775
Nosy @ned-deily, @berkerpeksag, @serhiy-storchaka, @tirkarthi, @aiudirog
PRs
  • bpo-34775: Return NotImplemented in PurePath division. #9509
  • [3.8] bpo-34775: Return NotImplemented in PurePath division. (GH-9509) #15172
  • bpo-36305: Fixes to path handling and parsing in pathlib #12361
  • 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 2019-08-29.05:12:34.289>
    created_at = <Date 2018-09-23.07:02:08.850>
    labels = ['3.8', 'type-bug', 'library', '3.9']
    title = 'pathlib.PurePath division raises TypeError instead of returning NotImplemented'
    updated_at = <Date 2020-01-16.12:08:52.969>
    user = 'https://github.com/aiudirog'

    bugs.python.org fields:

    activity = <Date 2020-01-16.12:08:52.969>
    actor = 'kmaork'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-08-29.05:12:34.289>
    closer = 'ned.deily'
    components = ['Library (Lib)']
    creation = <Date 2018-09-23.07:02:08.850>
    creator = 'Roger Aiudi'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 34775
    keywords = ['patch']
    message_count = 6.0
    messages = ['326140', '326146', '326156', '349217', '350712', '350714']
    nosy_count = 5.0
    nosy_names = ['ned.deily', 'berker.peksag', 'serhiy.storchaka', 'xtreak', 'Roger Aiudi']
    pr_nums = ['9509', '15172', '12361']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue34775'
    versions = ['Python 3.8', 'Python 3.9']

    @aiudirog
    Copy link
    Mannequin Author

    aiudirog mannequin commented Sep 23, 2018

    PurePath.__truediv__ and __rtruediv__ raise a TypeError when passed something which is not an instance of string or PurePath. This prevents creating any sort of compatible class that doesn't inherit from the previously mentioned types.

    @aiudirog aiudirog mannequin added 3.7 3.8 stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Sep 23, 2018
    @berkerpeksag
    Copy link
    Member

    berkerpeksag commented Sep 23, 2018

    Thanks for the report and for the PR. Could you give us a little bit more information about your use case? Couldn't you make the class you want to use implement the __fspath__ protocol?

        import pathlib as p
    
        class Spam:
            def __fspath__(self):
                return 'pathlib.py'

    And we can use it like:

        >>> p.Path('Lib') / Spam()
        PosixPath('Lib/pathlib.py')
        >>> (p.Path('Lib') / Spam()).exists()
        True

    (3.4 and 3.5 are in security-fix-only mode, so I removed them from the versions field.)

    @aiudirog
    Copy link
    Mannequin Author

    aiudirog mannequin commented Sep 23, 2018

    Using your above example, my use case is returning an instance of Spam instead of PurePath from the division operation. The Spam class would have extra properties and methods for dealing with a substructure of our file system that can exist in different places, so being able to use a normal Path to locate it later with the division operation would be useful. (I know I could have a method that would do that, but I personally think reading the division left to right is clearer.)

    The current implementation makes this impossible and I found raising a TypeError to be inconsistent with how Python handles operator overloading in the standard library.

    @serhiy-storchaka
    Copy link
    Member

    serhiy-storchaka commented Aug 8, 2019

    New changeset 4c69be2 by Serhiy Storchaka (aiudirog) in branch 'master':
    bpo-34775: Return NotImplemented in PurePath division. (GH-9509)
    4c69be2

    @ned-deily
    Copy link
    Member

    ned-deily commented Aug 29, 2019

    New changeset 4adcaf8 by Ned Deily (Miss Islington (bot)) in branch '3.8':
    bpo-34775: Return NotImplemented in PurePath division. (GH-9509) (GH-15172)
    4adcaf8

    @ned-deily
    Copy link
    Member

    ned-deily commented Aug 29, 2019

    The change was pushed for release in 3.8.0b4. I don't think we should backport it to 3.7 at this stage in its lifecycle as the old behavior has been around for a long time. Thanks, everyone!

    @ned-deily ned-deily added 3.9 and removed 3.7 labels Aug 29, 2019
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 3.9 stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants