-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[WIP] Better error messages for regex assertions #6659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@kalekundert |
Thanks for the link to
It seems like this is a promising idea, so I'll try to make this a more complete PR over the next week or so... |
/cc @asottile |
This comment has been minimized.
This comment has been minimized.
Thanks @kalekundert, it does seem interesting. This introduces regex as a new dependency, correct? It seems it is not a pure Python library, so I'm not sure it is a good fit for having it as a standard dependency in pytest. Perhaps we can have this improved representation only if |
(I've changed the base on my own now). |
Thanks for rebasing for me. Definitely it's possible to only provide the improved message only if regex is installed separately, and I think that's probably the best way to go. |
Hi @kalekundert! Recently we decided to close PRs older than a month since last update with the intent to clear up our PR queue, which is quite long. In light of that, should we close this now? You are free to reopen at a later date of course when you decide to pick this up again. Cheers! |
Yes, I'm still meaning to get back to this, but I can open a new PR when I do. |
Sure thing, feel free to reopen this PR then, whichever you prefer. 😁 Thanks again! |
This pull request implements a more helpful error message when assertions involving regular expressions fail. More specifically, the improved error message indicates the positions in the query string that would need to change in order for the regular expression to match.
This feature is analogous to the way pytest already provides more helpful messages for certain binary operators, e.g.
==
between two dicts. The difference is that there is no operator in this case; just a function call. So I modified the assertion rewriting code to handle function calls more like binary operators, in that the task of creating the error message is delegated depending on the specific function being called.The code I've written so far is just a prototype. I'm hoping to see if there's any interest in this feature before going to all the effort to make the code actually ready to be merged, i.e. writing tests and documentation, etc. I'd also appreciate feedback on ways I could implement this feature better; the assertion rewriting code is pretty foreign to me, and I have the feeling I didn't really do things "right".
Example
Here is an simplified example from a real test I was working on the other day. The regular expression is meant to match a date, but it has a bug. It should be
\d{1,2}
to match one or two digits.\d{1-2}
instead matches any number followed by a literal{1-2}
:The error message indicates where insertions or substitutions would have to be made in order for the string to match the pattern. Deletions can be indicated too, but this example doesn't have any. The hints aren't perfect, but they indicate that the problem starts around the day number, which would hopefully get us on the right track.
Caveats
This code adds a dependency on the regex package, which provides the "fuzzy matching" necessary to make the error message.
Only the regular expression functions that either return a
Match
orNone
are supported.These are:
re.match()
,re.search()
, andre.fullmatch()
.