Skip to content
Closed
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
7 changes: 4 additions & 3 deletions stdlib/fnmatch.pyi
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from _typeshed import GenericPath
from collections.abc import Iterable
from typing import AnyStr

__all__ = ["filter", "fnmatch", "fnmatchcase", "translate"]

def fnmatch(name: AnyStr, pat: AnyStr) -> bool: ...
def fnmatchcase(name: AnyStr, pat: AnyStr) -> bool: ...
def filter(names: Iterable[AnyStr], pat: AnyStr) -> list[AnyStr]: ...
def fnmatch(name: GenericPath[AnyStr], pat: AnyStr) -> bool: ...
def fnmatchcase(name: GenericPath[AnyStr], pat: AnyStr) -> bool: ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work

>>> import fnmatch
>>> from pathlib import Path
>>> fnmatch.fnmatchcase(Path("hi"), "x")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/jelle/Library/Application Support/uv/python/cpython-3.8.19-macos-aarch64-none/lib/python3.8/fnmatch.py", line 71, in fnmatchcase
    return match(name) is not None
TypeError: expected string or bytes-like object

The other functions do currently work with paths, but I'm not sure we should change the stubs; it feels accidental, and the module documentation talks about "path strings".

Copy link
Collaborator

@hauntsaninja hauntsaninja Aug 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another reason to not accept Paths: pathlib's trailing slash normalisation can also be undesirable in fnmatch like situations, e.g. Black has had bugs because of this kind of thing

def filter(names: Iterable[GenericPath[AnyStr]], pat: AnyStr) -> list[AnyStr]: ...
def translate(pat: str) -> str: ...