From 25fb5bf5f89a7c2d5c250c8a16227fe00b7b11cb Mon Sep 17 00:00:00 2001 From: Harjoth Khara Date: Tue, 20 Oct 2026 12:41:52 -0700 Subject: [PATCH] gh-139580: Clarify pathlib full_match recursive pattern docs (GH-151479) Documents how PurePath.full_match() handles a pattern ending in /**: /a/** matches /a/b but not /a, because the separator before ** must be present in the path and PurePath drops trailing slashes. Adds two doctest examples under full_match() and a note at the ** entry of the pattern language list contrasting it with Path.glob(). --------- (cherry picked from commit 264860472c1ec9446974c6c235b26c1774cec75d) Co-authored-by: Harjoth Khara Co-authored-by: Terry J Reedy --- Doc/library/pathlib.rst | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/Doc/library/pathlib.rst b/Doc/library/pathlib.rst index c24b2d539eb397..325a1cfe7caf74 100644 --- a/Doc/library/pathlib.rst +++ b/Doc/library/pathlib.rst @@ -600,10 +600,23 @@ Pure paths provide the following methods and properties: True >>> PurePath('a/b.py').full_match('*.py') False - >>> PurePath('/a/b/c.py').full_match('/a/**') - True >>> PurePath('/a/b/c.py').full_match('**/*.py') True + >>> PurePath('/a/b/c.py').full_match('/a/**') + True + + Patterns ending in "``/**``", as in the example above, may surprise when + passed to ``full_match``. Even though "``**``" can match 0 segments, + "``.../**``" requires a separator to match. Hence "``/a/**``" does not + match path "``/a``". Since :class:`PurePath` drops trailing slashes, this + pattern also does not match "``/a/``". + + :: + + >>> PurePath('/a').full_match('/a/**') + False + >>> PurePath('/a/').full_match('/a/**') + False .. seealso:: :ref:`pathlib-pattern-language` documentation. @@ -1814,6 +1827,8 @@ The following wildcards are supported in patterns for ``**`` (entire segment) Matches any number of file or directory segments, including zero. + Note that in :meth:`~PurePath.full_match`, "``a/**``" does not match + "``a``", whereas :meth:`~Path.glob` yields "``a``" for that pattern. ``*`` (entire segment) Matches one file or directory segment. ``*`` (part of a segment)