-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fnmatch: support pathlib #12522
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
fnmatch: support pathlib #12522
Conversation
This comment has been minimized.
This comment has been minimized.
Welcome! I think you're in the right place :-)
Hmm, it looks to me like this is because |
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
|
(You'll need to add |
This comment has been minimized.
This comment has been minimized.
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
| 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: ... |
There was a problem hiding this comment.
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".
There was a problem hiding this comment.
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
|
Thanks for your contribution, @adamjstewart! Based on the comments by my co-maintainers, I'm going to close this PR, though. Being able to use paths instead of strings looks like an unintended side effect of the implementation and could introduce subtle bugs. |
First time contributor, honestly not sure if I'm in the right place.
fnmatchis a module for "Unix filename pattern matching". However, at the moment, the type hints do not supportpathlibpaths. From my testing,pathlibpaths work just fine.