Skip to content

Updates - #1

Merged
fferegrino merged 6 commits into
mainfrom
updates-19-04-2026
Apr 20, 2026
Merged

fferegrino merged 6 commits into
mainfrom
updates-19-04-2026

Conversation

@fferegrino

Copy link
Copy Markdown
Owner

This pull request introduces significant improvements to type safety and pattern handling in the dict_patterns library. The most important changes include the addition of a new DictTypeMismatchError exception for clearer error reporting when template and actual values have different types, enhancements to the pattern matching logic to raise this error, and updates to the pattern compilation to safely handle user-supplied capturing groups. Comprehensive tests have also been added to cover these new behaviours.

Type Safety and Error Handling:

  • Added a new DictTypeMismatchError exception, raised when the types of template and actual values differ, and integrated it throughout the matcher logic for more precise error reporting.
  • Updated the match method in DictMatcher to return a copy of the values dictionary, preventing accidental mutation of internal state.

Pattern Compilation and Matching:

  • Modified the compile_template function to neutralise user-supplied capturing groups, converting them to non-capturing groups to avoid group index shifting and ensure correct field extraction.
  • Improved the matcher to cache compiled templates for efficiency.

Testing:

  • Added extensive tests for type mismatch scenarios and for correct handling of user-supplied capturing groups in patterns, ensuring robust error detection and group extraction.

Miscellaneous:

  • Minor CI workflow fix: corrected the make target from make tests to make test.
  • Removed an unused pytest configuration hook for code cleanliness.

User-supplied regex patterns containing capturing groups (e.g. `(\d{3})`)
would shift the group indices used to extract matched values, causing
incorrect values to be returned when multiple patterns appear in a single
template string. Convert user capturing groups to non-capturing (`(?:`)
before wrapping them.
Previously, when a template value like "{number:age}" was matched against
a non-string (e.g. integer 42), the comparison fell through to a generic
DictValueMismatchError with no indication that the real problem was a type
difference. Now raises DictTypeMismatchError with a clear message including
the path and both types.
The identifier in compile_template's return type can be None for anonymous
patterns, so the annotation should be tuple[str, str | None]. Also remove
the empty pytest_configure hook which served no purpose.
…ilation

compile_template was called on every string comparison during matching.
When the same pattern string appears many times (e.g. in a list of dicts),
the regex was recompiled each time. Now cached per-instance in a dict
keyed on the template string.
match() previously returned self.values directly, meaning the returned
dict would be emptied on the next match() call. Now returns a copy so
each call produces an independent result. self.values still reflects the
most recent match for direct access.
@fferegrino
fferegrino merged commit 80bdfa7 into main Apr 20, 2026
1 check 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.

1 participant