Skip to content

fix: anchor string patterns in match() to consume full keypath#579

Open
gaoflow wants to merge 1 commit into
fabiocaccamo:mainfrom
gaoflow:fix/match-suffix-wildcard
Open

fix: anchor string patterns in match() to consume full keypath#579
gaoflow wants to merge 1 commit into
fabiocaccamo:mainfrom
gaoflow:fix/match-suffix-wildcard

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 24, 2026

Copy link
Copy Markdown

Bug

benedict.match() converts string wildcard patterns to regex and applies
regex.match(), which only verifies a prefix match — the rest of the
keypath is silently ignored.

For a pattern like "*a" the transformation produces (.)*a. Because
regex.match("ab") anchors at the start but not the end, it succeeds on
"ab" (. consumes "a", a matches the first character, "b" is
left unconsumed). The result: keys that do not end in "a" are
erroneously returned.

from benedict import benedict

d = benedict({"aa": 1, "ab": 2, "ba": 3, "bb": 4})
d.match("*a")
# Before fix: [1, 2, 3]   ← "ab" wrongly included
# After fix:  [1, 3]      ← only "aa" and "ba"

Fix

Append "$" to string-derived patterns before compiling so the full
keypath must be consumed. Raw compiled-regex patterns (re.Pattern)
are unaffected and continue to use regex.match() as before.

Tests

Added test_match_with_suffix_wildcard to tests/core/test_match.py;
all 261 existing core + keypath tests continue to pass.

String patterns like '*a' were matched with regex.match() which only
checks a prefix, so '(.)*a' matched 'ab' (consuming just 'a', leaving
'b' unconsumed). Fix by appending '$' to string-derived patterns so the
entire keypath must be consumed. Raw compiled-regex patterns are
unaffected and continue to use match() as before.
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.

1 participant