Skip to content

Fix negated glob character classes - #162

Merged
jaraco merged 2 commits into
jaraco:mainfrom
kokotatan:fix-glob-character-classes
Oct 3, 2026
Merged

jaraco merged 2 commits into
jaraco:mainfrom
kokotatan:fix-glob-character-classes

Conversation

@kokotatan

@kokotatan kokotatan commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Path.glob('[!a].txt') currently matches a.txt and !.txt, because glob character classes are copied directly into the generated regular expression. Shell-style ! negation is consequently treated as a literal character. Conversely, [^a] is incorrectly interpreted as regex negation.

This is wrong because glob character classes have shell-style semantics, not regex semantics, and so need translation like the other wildcards. The fnmatch docs (which glob and pathlib follow) define [!seq] as matching any character not in seq, and give ^ no special meaning (fnmatch.translate('[^a]') produces [\^a]). zipfile.Path.glob should agree with pathlib.Path.glob.

Translate recognized character classes using fnmatch.translate, then remove its outer group/end marker before inserting the result into the complete pattern. A separator guard keeps negated classes from matching across directory boundaries. Other wildcard and character-class tokenization logic is unchanged.

The new public Path.glob regression covers negation, caret inclusion, positive/negative ranges, directory matches and a path-separator control, for both implicit and explicit archive directories. Before the source change, 12 of the 16 subcases failed; all now pass.

Validation on Windows:

  • Python 3.12 focused glob checks: 10 passed, 34 subtests passed.
  • Full tox -e py312: 106 passed, 2 xfailed, 8 failed. All eight failures are existing Ruff checks and reproduce on unmodified source (106 passed, 1 xfailed, 8 failed); the marked performance tests vary between pass/xfail across runs.
  • Functional suite with only the Ruff plugin disabled, preserving the tox warning environment: 75 passed, 1 xfailed, 100% coverage, including every changed source line.
  • tox -e docs (Sphinx with warnings as errors and sphinx-lint): passed.
  • Additional public glob checks on Python 3.10 and 3.14: passed, covering both fnmatch.translate end-marker formats.
  • git diff --check: passed.

AI assistance: developed with OpenAI Codex and checked through independent Claude Opus and Codex AI reviews. This is not a claim of human review.

@jaraco jaraco left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks for the contrib. It would be nice if the problem description provided a little more context. In particular, instead of stating that the behavior is wrong, elaborate on why the behavior is wrong. I think the answer is that glob patterns do support character classes and need to be translated to regular expressions as well. How do we know that?

Comment thread zipp/glob.py Resolved
Comment thread zipp/glob.py Resolved
Comment thread zipp/glob.py Resolved
Comment thread tests/test_path.py Resolved
@jaraco

jaraco commented Oct 3, 2026

Copy link
Copy Markdown
Owner

I wrote these comments, but I'll also be addressing them.

- Dispatch symmetrically to _replace_set/_replace_plain.
- Extract _strip_fnmatch with a docstring, matching the end marker rather than slicing.
- Use the parameterize helper for the character-class test cases.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jaraco
jaraco merged commit b0911d5 into jaraco:main Oct 3, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants