Skip to content

GH-70303: Emit FutureWarning when pathlib glob pattern ends with ** #105413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,11 @@ call fails (for example because the path doesn't exist).
.. versionchanged:: 3.13
The *follow_symlinks* parameter was added.

.. versionchanged:: 3.13
Emits :exc:`FutureWarning` if the pattern ends with "``**``". In a
future Python release, patterns with this ending will match both files
and directories. Add a trailing slash to match only directories.

.. method:: Path.group()

Return the name of the group owning the file. :exc:`KeyError` is raised
Expand Down
5 changes: 5 additions & 0 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,11 @@ def _glob(self, pattern, case_sensitive, follow_symlinks):
pattern_parts.append('')
if pattern_parts[-1] == '**':
# GH-70303: '**' only matches directories. Add trailing slash.
warnings.warn(
"Pattern ending '**' will match files and directories in a "
"future Python release. Add a trailing slash to match only "
"directories and remove this warning.",
FutureWarning, 3)
pattern_parts.append('')

if case_sensitive is None:
Expand Down
19 changes: 16 additions & 3 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1903,11 +1903,11 @@ def _check(glob, expected):
"dirC/dirD", "dirC/dirD/fileD"])
_check(p.rglob("file*"), ["dirC/fileC", "dirC/dirD/fileD"])
_check(p.rglob("**/file*"), ["dirC/fileC", "dirC/dirD/fileD"])
_check(p.rglob("dir*/**"), ["dirC/dirD"])
_check(p.rglob("dir*/**/"), ["dirC/dirD"])
_check(p.rglob("*/*"), ["dirC/dirD/fileD"])
_check(p.rglob("*/"), ["dirC/dirD"])
_check(p.rglob(""), ["dirC", "dirC/dirD"])
_check(p.rglob("**"), ["dirC", "dirC/dirD"])
_check(p.rglob("**/"), ["dirC", "dirC/dirD"])
# gh-91616, a re module regression
_check(p.rglob("*.txt"), ["dirC/novel.txt"])
_check(p.rglob("*.*"), ["dirC/novel.txt"])
Expand Down Expand Up @@ -2057,7 +2057,20 @@ def test_glob_above_recursion_limit(self):
path.mkdir(parents=True)

with set_recursion_limit(recursion_limit):
list(base.glob('**'))
list(base.glob('**/'))

def test_glob_recursive_no_trailing_slash(self):
P = self.cls
p = P(BASE)
with self.assertWarns(FutureWarning):
p.glob('**')
with self.assertWarns(FutureWarning):
p.glob('*/**')
with self.assertWarns(FutureWarning):
p.rglob('**')
with self.assertWarns(FutureWarning):
p.rglob('*/**')


def test_readlink(self):
if not self.can_symlink:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Emit :exc:`FutureWarning` from :meth:`pathlib.Path.glob` and
:meth:`~pathlib.Path.rglob` if the given pattern ends with "``**``". In a
future Python release, patterns with this ending will match both files and
directories. Add a trailing slash to only match directories.