Skip to content

PurePath.match accepts a PurePath pattern only since Python 3.12 #112758

@vlopezferrando

Description

@vlopezferrando

Documentation

In the docs on PurePath.match(pattern, *, case_sensitive=None) on pathlib, it says:

The pattern may be another path object; this speeds up matching the same pattern against multiple files:

But it doesn't say that pattern can only be a path object since Python 3.12.

For example, on Python 3.11, you get this error when trying to use a PurePath as the pattern:

Python 3.11.6 (main, Oct 18 2023, 12:13:43) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pathlib import PurePath
>>> pattern = PurePath("*.py")
>>> PurePath("hello.py").match(pattern)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/victor/.pyenv/versions/3.11.6/lib/python3.11/pathlib.py", line 810, in match
    drv, root, pat_parts = self._flavour.parse_parts((path_pattern,))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/victor/.pyenv/versions/3.11.6/lib/python3.11/pathlib.py", line 67, in parse_parts
    drv, root, rel = self.splitroot(part)
                     ^^^^^^^^^^^^^^^^^^^^
  File "/home/victor/.pyenv/versions/3.11.6/lib/python3.11/pathlib.py", line 240, in splitroot
    if part and part[0] == sep:
                ~~~~^^^
TypeError: 'PurePosixPath' object is not subscriptable
>>> vim /home/victor/.pyenv/versions/3.11.6/lib/python3.11/pathlib.py
  File "<stdin>", line 1
    vim /home/victor/.pyenv/versions/3.11.6/lib/python3.11/pathlib.py
                     ^
SyntaxError: invalid syntax
>>> :810
  File "<stdin>", line 1
    :810
    ^
SyntaxError: invalid syntax
>>>

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixes3.13bugs and security fixesdocsDocumentation in the Doc dirtopic-pathlib

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions