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
Comments
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. |
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.) |
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. |
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! |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: