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: Shortcut for checking if PurePath object contains str
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.10
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: serhiy.storchaka, tomplast, veky
Priority: normal Keywords: patch

Created on 2021-02-13 04:44 by tomplast, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
pure_path_contains.patch tomplast, 2021-02-13 04:47
Messages (6)
msg386904 - (view) Author: Tomas Gustavsson (tomplast) * Date: 2021-02-13 04:44
While using pathlib I realised there are situations where I easily want to check if part of a returned path object contains a particular given path (as a string).

Today the following will give an error:
...
path = PosixPath('/usr/share/doc/')
if 'share' in path:
....print('Do something')

The patch will make the example above work and will open up for a simple shortcut.

This is my first attempt to contribute, all opinions are very welcome.

I have also pushed the commit to my own branch https://github.com/tomplast/cpython/tree/implement-path-contains.
msg386919 - (view) Author: Vedran Čačić (veky) * Date: 2021-02-13 11:07
While it might be useful, I don't think it is what you want. For example, you wouldn't say it contains 'r/sh', right? I think it should only refer to full names of path parts.
msg386920 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-02-13 11:56
Path is not string, and it was made not string-like intentionally. Otherwise it would be made a subclass of str.

If you want to check whether a string is a substring of the string representation of the path, just do it explicitly: 'share' in str(path). But in most cases it is not what the user intended.

There were several propositions about implementing "in" (and iteration, these operations are related and should be consistent). The problem is that there several different meaning of that operation. Should it check that the directory referred by the path contains the specified file name? Or that the path contains the specified path component? Or that one path is a subpath of other path? Or that the specified string is a substring of the string representation of the path? (The latter proposition is the least useful.) It is better to avoid ambiguity, so all these proposition were rejected.
msg386921 - (view) Author: Tomas Gustavsson (tomplast) * Date: 2021-02-13 12:10
Okay, maybe a bad example. But let's say I want to find all folders and
files but filter out those which contains .git,.svn in the paths.

Anyhow, I believe this minor feature would make such use cases (and other)
more clean and intuitive. I am lazy and I like when things work without me
having to write more lines or characters then I need.

On Sat, 13 Feb 2021, 12:08 Vedran Čačić <report@bugs.python.org> wrote:

>
> Vedran Čačić <vedgar@gmail.com> added the comment:
>
> While it might be useful, I don't think it is what you want. For example,
> you wouldn't say it contains 'r/sh', right? I think it should only refer to
> full names of path parts.
>
> ----------
> nosy: +veky
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue43213>
> _______________________________________
>
msg386922 - (view) Author: Tomas Gustavsson (tomplast) * Date: 2021-02-13 12:13
Sorry Serhiy, missed your answer there. I understand your point. I guess
I'll have to look for other things to help with :)

Thank you for the answer.

Guess this can be closed then.

Best regards
Tomas

On Sat, 13 Feb 2021, 12:57 Serhiy Storchaka <report@bugs.python.org> wrote:

>
> Serhiy Storchaka <storchaka+cpython@gmail.com> added the comment:
>
> Path is not string, and it was made not string-like intentionally.
> Otherwise it would be made a subclass of str.
>
> If you want to check whether a string is a substring of the string
> representation of the path, just do it explicitly: 'share' in str(path).
> But in most cases it is not what the user intended.
>
> There were several propositions about implementing "in" (and iteration,
> these operations are related and should be consistent). The problem is that
> there several different meaning of that operation. Should it check that the
> directory referred by the path contains the specified file name? Or that
> the path contains the specified path component? Or that one path is a
> subpath of other path? Or that the specified string is a substring of the
> string representation of the path? (The latter proposition is the least
> useful.) It is better to avoid ambiguity, so all these proposition were
> rejected.
>
> ----------
> nosy: +serhiy.storchaka
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue43213>
> _______________________________________
>
msg386924 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-02-13 14:34
I guess that you need to use the parts attribute. Note that `'share' in str(path)` and `'share' in path.parts` are two very different things, even if they can give the same result in some cases.

When reply be email, please remove the quoted text. It makes reading difficult, especially for people with disabilities.
History
Date User Action Args
2022-04-11 14:59:41adminsetgithub: 87379
2021-02-13 14:34:54serhiy.storchakasetstatus: open -> closed
resolution: rejected
messages: + msg386924

stage: resolved
2021-02-13 12:13:28tomplastsetmessages: + msg386922
2021-02-13 12:10:02tomplastsetmessages: + msg386921
title: Shortcut for checking if PurePath object contains str -> Shortcut for checking if PurePath object contains str
2021-02-13 11:56:47serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg386920
2021-02-13 11:08:00vekysetnosy: + veky
messages: + msg386919
2021-02-13 04:47:24tomplastsetfiles: + pure_path_contains.patch
keywords: + patch
2021-02-13 04:44:50tomplastcreate