Skip to content

fix: escape square brackets in patterns to match literal bracket file…#1173

Open
koteshyelamati wants to merge 1 commit into
gorakhargosh:masterfrom
koteshyelamati:patch-1
Open

fix: escape square brackets in patterns to match literal bracket file…#1173
koteshyelamati wants to merge 1 commit into
gorakhargosh:masterfrom
koteshyelamati:patch-1

Conversation

@koteshyelamati

Copy link
Copy Markdown

Summary

Square brackets in watchdog patterns (e.g. [test].txt) were being interpreted as fnmatch character classes, causing:

  • Pattern [test].txt matches t.txt ✗ (wrong — [test] treated as a char class)
  • Pattern [test].txt does NOT match [test].txt ✗ (wrong — literal file not detected)

Root Cause

_full_match() delegates to pathlib.PurePath.full_match() (Python ≥ 3.13) or the translate() fallback, both of which use fnmatch-style glob patterns where […] denotes a character class.

Fix

Before pattern matching, replace [ with [[[] so the bracket is literal in fnmatch/glob syntax. This makes [test].txt correctly match a file literally named [test].txt.

Fixes #991

…names

Square brackets in patterns (e.g. "[test].txt") were being interpreted as
character classes by fnmatch/pathlib, causing the pattern to match files like
"t.txt" instead of a file literally named "[test].txt".

Fix by replacing "[" with "[[[]" in the pattern before matching, which makes
the bracket literal in fnmatch/glob syntax.

Fixes gorakhargosh#991
@BoboTiG

BoboTiG commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

Can you add regression tests?

@koteshyelamati

Copy link
Copy Markdown
Author

Yes @BoboTiG — adding regression tests for literal-bracket patterns, pushing to this branch shortly.

@BoboTiG

BoboTiG commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator

Hmm this way it will be no more possible to use character classes. 🤔

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.

If the pattern contains "[]" square brackets in it, no event was triggered in PatternMatchingEventHandler

2 participants