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.

Title: pathlib.Path to support the "in" operator (x in y)
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.10
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') 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:

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'))
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.

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