Issue 33301: Add contains to pathlib (original) (raw)

Created on 2018-04-17 21:23 by Alok Singh, last changed 2022-04-11 14:58 by admin. 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
2022-04-11 14:58:59 admin set github: 77482
2019-01-14 10:05:28 serhiy.storchaka set status: open -> closedresolution: rejectedstage: resolved
2018-06-20 03:57:27 serhiy.storchaka set nosy: + serhiy.storchakamessages: +
2018-06-19 23:34:13 aberger5b set messages: +
2018-06-19 19:00:48 pitrou set nosy: + pitroumessages: +
2018-06-18 18:06:08 aberger5b set nosy: + aberger5bmessages: +
2018-04-17 21:35:32 barry set nosy: + barry
2018-04-17 21:23:21 Alok Singh create