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.
@codecov

codecov Bot commented Jul 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.32%. Comparing base (068813d) to head (21c35a9).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #579   +/-   ##
=======================================
  Coverage   98.32%   98.32%           
=======================================
  Files          64       64           
  Lines        2392     2393    +1     
=======================================
+ Hits         2352     2353    +1     
  Misses         40       40           
Flag Coverage Δ
unittests 98.32% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes incorrect behavior in benedict.match() where string wildcard patterns were converted to regex and evaluated with regex.match(), allowing unintended prefix-only matches (e.g., patterns intended to match suffixes).

Changes:

  • Anchor string-derived wildcard patterns so matches must consume the full keypath.
  • Add a regression test to ensure suffix wildcard patterns (e.g., "*.jpg") don’t match longer strings like "*.jpg.bak".

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
benedict/core/match.py Anchors string-derived regex patterns to prevent unintended prefix matches.
tests/core/test_match.py Adds a regression test covering suffix-wildcard matching behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread benedict/core/match.py
Comment on lines +24 to 26
# anchor to end so the full keypath must be consumed
pattern = pattern + "$"
regex = re.compile(pattern, flags=re.DOTALL)
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