Repository navigation
Updates - #1
Merged
Merged
Updates#1
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request introduces significant improvements to type safety and pattern handling in the
dict_patternslibrary. The most important changes include the addition of a newDictTypeMismatchErrorexception 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:
DictTypeMismatchErrorexception, raised when the types of template and actual values differ, and integrated it throughout the matcher logic for more precise error reporting.matchmethod inDictMatcherto return a copy of thevaluesdictionary, preventing accidental mutation of internal state.Pattern Compilation and Matching:
compile_templatefunction to neutralise user-supplied capturing groups, converting them to non-capturing groups to avoid group index shifting and ensure correct field extraction.Testing:
Miscellaneous:
make teststomake test.