classification
Title: pathlib.PurePath division raises TypeError instead of returning NotImplemented
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.9, Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Roger Aiudi, berker.peksag, ned.deily, serhiy.storchaka, xtreak
Priority: normal Keywords: patch

Created on 2018-09-23 07:02 by Roger Aiudi, last changed 2019-08-29 05:12 by ned.deily. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 9509 merged Roger Aiudi, 2018-09-23 07:02
PR 15172 merged miss-islington, 2019-08-08 05:44
Messages (6)
msg326140 - (view) Author: Roger Aiudi (Roger Aiudi) * Date: 2018-09-23 07:02
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.
msg326146 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2018-09-23 07:58
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.)
msg326156 - (view) Author: Roger Aiudi (Roger Aiudi) * Date: 2018-09-23 14:41
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.
msg349217 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-08-08 05:41
New changeset 4c69be22df3852f17873a74d015528d9a8ae92d6 by Serhiy Storchaka (aiudirog) in branch 'master':
bpo-34775: Return NotImplemented in PurePath division. (GH-9509)
https://github.com/python/cpython/commit/4c69be22df3852f17873a74d015528d9a8ae92d6
msg350712 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2019-08-29 05:06
New changeset 4adcaf81513e57a2a4e278073a89efb1375af8df by Ned Deily (Miss Islington (bot)) in branch '3.8':
bpo-34775: Return NotImplemented in PurePath division. (GH-9509) (GH-15172)
https://github.com/python/cpython/commit/4adcaf81513e57a2a4e278073a89efb1375af8df
msg350714 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2019-08-29 05:12
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!
History
Date User Action Args
2019-08-29 05:12:34ned.deilysetstatus: open -> closed
versions: + Python 3.9, - Python 3.6, Python 3.7
messages: + msg350714

resolution: fixed
stage: patch review -> resolved
2019-08-29 05:06:03ned.deilysetnosy: + ned.deily
messages: + msg350712
2019-08-08 05:44:33miss-islingtonsetkeywords: + patch
stage: test needed -> patch review
pull_requests: + pull_request14904
2019-08-08 05:41:18serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg349217
2018-09-23 14:41:44Roger Aiudisetmessages: + msg326156
2018-09-23 08:20:43xtreaksetnosy: + xtreak
2018-09-23 07:58:01berker.peksagsetversions: - Python 3.4, Python 3.5
nosy: + berker.peksag

messages: + msg326146

stage: test needed
2018-09-23 07:02:08Roger Aiudicreate