Repository navigation
fix: handle non-string input in Replace validator gracefully - #540
binggao1230 wants to merge 2 commits into
Conversation
Replace.__call__ passed non-string values straight to re.Pattern.sub(), which raises an unhandled TypeError. Wrap it and raise MatchInvalid (honouring a custom msg), matching how Match reports the same case.
7ade908 to
4c75e63
Compare
|
Rebased onto master and dropped the unrelated quote-style churn from the diff — it's now just the Replace fix and its test. |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4c75e63f90
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| except TypeError: | ||
| raise MatchInvalid(self.msg or 'expected string or buffer') |
There was a problem hiding this comment.
Avoid swallowing TypeError from replacement callbacks
When substitution is a callable accepted by re.Pattern.sub, any TypeError raised inside that callback is now misreported as invalid input. In combinators such as Any, the resulting MatchInvalid is caught and another branch may silently accept the value, hiding a real callback defect; limit the conversion to TypeErrors caused by an incompatible input value.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Addressed in 80d3204: Replace now validates regex input compatibility before running sub(), so TypeError from a callable replacement propagates unchanged. Added coverage for callback failures and valid bytes-like inputs.
The Replace validator calls re.Pattern.sub() directly on the input value. When passed a non-string type (int, None, list, etc.), this raises an unhandled TypeError instead of the expected Invalid exception.
This matches the behavior of the Match validator, which already catches TypeError and raises a proper Invalid error with a descriptive message.
Before (crashes):
After:
Changes: