fix(#742): match keywords as regex, matching the documented tutorial - #753
Open
bosd wants to merge 1 commit into
Open
fix(#742): match keywords as regex, matching the documented tutorial#753bosd wants to merge 1 commit into
bosd wants to merge 1 commit into
Conversation
The template tutorial has always documented ``keywords`` and ``exclude_keywords`` as regex patterns (``Company\s+US``, ``(?i)Accor``, ``Wystawił\(a\)`` ...), and ~16 bundled templates already wrote them that way -- but ``matches_input`` used plain ``in`` substring matching, so those regex-shaped keywords silently under-matched. Route both keyword checks through :func:`regex.search` (via the module-local ``_regex`` compile-once cache) so they honour the documented contract. A pattern that doesn't compile as valid regex falls back to a literal substring check with a DEBUG log line, so a stray metachar in a simple-string keyword like ``Company [Ltd`` keeps matching literally instead of breaking. Re-exports ``_regex.error`` so the fallback does not need to know which engine is active. Closes #742. Claude-Session: https://claude.ai/code/session_01YLnqiMRXGtQQsM7TGRQx6v
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.
Summary
Closes #742.
The template-authoring tutorial has always documented
keywordsandexclude_keywordsas regex patterns — e.g. "Company USandCompany\s+USboth work". ButInvoiceTemplate.matches_inputused a plaininsubstring test, so any keyword that leaned on a regex metachar silently under-matched.An audit of the bundled templates shows ~16 already write regex-shaped keywords that were effectively broken:
(?i)Accor,(?i)Digital\sRiverBE\s0673\s923Express\s+\w+Wystawił\(a\)NL8152[.]54[.]295[.]B01Change
keywordsandexclude_keywordschecks through a new module-local_keyword_matcheshelper that calls_regex.search(the compile-once cache used everywhere else inextract/).[or(in a simple-string keyword likeCompany [Ltdkeeps matching literally instead of breaking._regex.error(the active engine's error type —re.errorby default,regex.errorunderINVOICE2DATA_REGEX_ENGINE=regex) so the fallback doesn't need to know which engine is active.Compatibility
Acme Corp) behave exactly as before —regex.search(\"Acme Corp\", text)is the same as\"Acme Corp\" in text.Company [Ltdwith an unclosed[) still match via the literal fallback.Test plan
\s+,(?i), alternation, exclude-as-regex, and invalid-regex-falls-back-to-substring.test_gvision,test_copyneeding pdftotext/tesseract, andQualityHosting.pdfcascade, all red on master too).test_default_templates_are_loadedstill loads every bundled template (nothing rejected as invalid regex).https://claude.ai/code/session_01YLnqiMRXGtQQsM7TGRQx6v