classification
Title: Add __contains__ to pathlib
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Alok Singh, aberger5b, barry, pitrou, serhiy.storchaka
Priority: normal Keywords:

Created on 2018-04-17 21:23 by Alok Singh, last changed 2019-01-14 10:05 by serhiy.storchaka. This issue is now closed.

Messages (5)
msg315422 - (view) Author: Alok Singh (Alok Singh) Date: 2018-04-17 21:23
I was thinking today that it would be natural for paths to support __contains__ since then you could write statements like `if file in dir` or `if subdir in dir` cleanly.

The library plumbum appears to already have this, but I think it could be useful in the standard library:

https://plumbum.readthedocs.io/en/latest/_modules/plumbum/path/base.html#Path.__contains__
msg319902 - (view) Author: Andrew Berger (aberger5b) Date: 2018-06-18 18:06
I can make these changes. Would probably add a .exists method to PurePath, using the _normal_accessor.stats staticmethod, then call that in __contains__
msg319992 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2018-06-19 19:00
Not a good idea IMHO.  Why would containment mean the existence of a file in a directory?  It could just as well mean that a certain path component is part of the path.

Also I don't understand what would e.g. `Path('/usr/bar') in Path('/etc/foo')` mean.

As for adding a .exists method to PurePath, I think you're missing the point of PurePath here (i.e. its operations *don't* do any IO whatsoever).
msg320007 - (view) Author: Andrew Berger (aberger5b) Date: 2018-06-19 23:34
I think the idea is that either a subdir or file could be valid inputs. 

So `Path('/usr/bar') in Path('/etc/foo')` return True if `Path('/etc/foo/usr/bar')` is either a dir or file.

As for PurePath, I did overlook that accessing an inode via a call to stat would be considered filesystem IO. So putting that method in Path (if this turns out to be a good idea) is the better option. Thanks
msg320016 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-06-20 03:57
There was a similar (closed) issue about making Path an iterable. The problem with both these related ideas is that they are ambiguous. Does it means that a directory contains a specified file, or that a path contains a specified path component, or that a specified path is a prefix of other path? There are third-party pathlib-like implementations that make Path a str subclass. They have both __iter__ and __contains__ inherited from str, but with different meaning.

I'm -1. It is better to write explicitly what you mean than allow the computer to guess.
History
Date User Action Args
2019-01-14 10:05:28serhiy.storchakasetstatus: open -> closed
resolution: rejected
stage: resolved
2018-06-20 03:57:27serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg320016
2018-06-19 23:34:13aberger5bsetmessages: + msg320007
2018-06-19 19:00:48pitrousetnosy: + pitrou
messages: + msg319992
2018-06-18 18:06:08aberger5bsetnosy: + aberger5b
messages: + msg319902
2018-04-17 21:35:32barrysetnosy: + barry
2018-04-17 21:23:21Alok Singhcreate