This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: pathlib.Path to support the "in" operator (x in y)
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.10
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Torxed, eric.smith
Priority: normal Keywords:

Created on 2020-12-27 18:54 by Torxed, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg383857 - (view) Author: Anton Hvornum (Torxed) Date: 2020-12-27 18:54
I would like to propose that the `pathlib.Path()` gets a `in` operator, much like that ipaddress has IP in Subnet, it would be nice if we could be able to do:

```
import pathlib
pathlib.Path('/home/Torxed/machine.qcow2')
pathlib.Path('/home/Torxed/machine.qcow2') in pathlib.Path('/home/Torxed')
```

Currently that would generate:
```
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument of type 'PosixPath' is not iterable
```

This would avoid "complicated" implementations such as:
 * https://stackoverflow.com/questions/21411904/python-how-to-check-if-path-is-a-subpath
 * https://stackoverflow.com/questions/3812849/how-to-check-whether-a-directory-is-a-sub-directory-of-another-directory

Which tend to be half-complete truths and would result in potential security issues. pathlib.Path() could help prevent some of those.
msg383879 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-12-28 00:52
Can you describe what this would do? How is it different from path.is_relative_to(other_path)?

>>> import pathlib
>>> pathlib.Path('/home/Torxed/machine.qcow2').is_relative_to(pathlib.Path('/home/Torxed'))
True
msg383886 - (view) Author: Anton Hvornum (Torxed) Date: 2020-12-28 08:23
Missed that function, but they would behave the same without explicitly use the function call.
msg383887 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-12-28 09:11
I don't think there's any chance we'd add "in" as an alias for "is_relative_to()", so I'm going to close this.

If you disagree, you could try and get support on python-ideas, and then we can re-open this.

Thanks!
History
Date User Action Args
2022-04-11 14:59:39adminsetgithub: 86924
2020-12-28 09:11:56eric.smithsetstatus: open -> closed
resolution: rejected
messages: + msg383887

stage: resolved
2020-12-28 08:23:34Torxedsetmessages: + msg383886
2020-12-28 00:59:25eric.smithsetversions: - Python 3.9
2020-12-28 00:52:11eric.smithsetnosy: + eric.smith
messages: + msg383879
2020-12-27 18:54:36Torxedcreate