fix: support path-like pattern in match, reject ContainerPath#524
Open
tonyandrewmeyer wants to merge 3 commits into
Open
fix: support path-like pattern in match, reject ContainerPath#524tonyandrewmeyer wants to merge 3 commits into
tonyandrewmeyer wants to merge 3 commits into
Conversation
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
e54ffea to
229b2f6
Compare
Comment on lines
+174
to
+186
| def match(self, path_pattern: str | os.PathLike[str]) -> bool: | ||
| def match( | ||
| self, path_pattern: str | os.PathLike[str], *, case_sensitive: bool | None = None | ||
| ) -> bool: | ||
| # On Python 3.11, pathlib.Path.match only accepts a str pattern (3.12 widened it to | ||
| # path-like). On Python 3.14, pathlib.PurePath.match accepts any object with | ||
| # `with_segments`, which would silently allow a ContainerPath through. Normalising | ||
| # via os.fspath gives us a consistent, narrow API: str | os.PathLike[str] on every | ||
| # supported version, with a clear TypeError for anything else (including ContainerPath, | ||
| # which is not os.PathLike). | ||
| return super().match(os.fspath(path_pattern)) | ||
| kwargs: dict[str, bool] = {} | ||
| if case_sensitive is not None: | ||
| kwargs['case_sensitive'] = case_sensitive | ||
| return super().match(os.fspath(path_pattern), **kwargs) |
Collaborator
There was a problem hiding this comment.
I'm not sure about this change. It does resolve the incompatible override error across all Python versions. But the error is because match added case_sensitive in Python 3.12. It's valid in typing terms for the override to add an extra argument (as we're now doing on Python 3.10), but we've implemented our handling of the extra argument in such a way that we hide from both callers of the function and from our own static analysis that it's an error to pass case_sensitive on Python <3.12.
Contributor
Author
There was a problem hiding this comment.
The problem is that we need to satisfy the checker that wants an ignore in one version and forbids having an ignore in another version. Any suggestions?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR widens
match()onPathProtocol,LocalPath, andContainerPathto acceptstr | os.PathLike[str], matchingpathlib.Path.matchon Python 3.12+ and the signature already used byglob()(see #505).ContainerPath.matchadditionally rejectsContainerPatharguments with aTypeError. This restores the pre-3.14 behaviour: Python 3.14'spathlib.PurePath.matchaccepts any object that implementswith_segments, which would otherwise silently let aContainerPaththrough and match the wrong filesystem.LocalPathgains a thinmatchoverride that callsos.fspathbefore delegating, so the path-like pattern works on 3.10/3.11 too (wherepathlib.Path.matchisstr-only). The override also forwards thecase_sensitivekeyword on 3.12+ to stay signature-compatible with the parent across all supported Python versions.Refs #369