-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
unittest.assert*Regex functions should verify that expected_regex has a valid type #64344
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
Comments
A normal thing for a developer to do is to convert a use of an assert* function to a use of an assert*Regex function and foolishly forget to actually specify the expected regular expression. If they do this, the test will always pass because the callable expression will be in the place of the expected regular expression and the callable expression will, therefore, be None. It would be nice if in the constructor in AssertRaisesBaseContext (for 3.5) not only was the expected regex converted to a regex (if actually a string or bytes) but if it's not if it were checked if it were a valid regular expression object and an exception raised if this is not the case. In the current version of AssertRaisesBaseContext.handle the comments say that if the callable object is None then the function is being used as a context manager. Not always the case, alas, perhaps the developer just forgot to add that necessary regular expression before the callable object. |
The two signatures of assertRaisesRegex are: IIUC what you are saying is that if you forget the regex and call assertRaisesRegex(exception, callable) the test will pass. I think it would be ok to add a check to see if the second argument is a callable or None (or maybe check if it's a string or regex object?), and give an appropriate error message if it's not. |
Agreed. |
Yes. I'ld check if it was a string or a regex object...there is already code that converts the string to a regular expression in there. |
Just to be sure, the check must be implemented inside the assertRaisesRegex method, right? |
That's correct. |
A simple way of checking is to actually re.compile(regex), I think. It should automatically raise TypeError on invalid input. |
Heh. If unittest had used duck typing instead of isinstance for its string-to-regex conversion check in the first place, this issue wouldn't even have come up. |
I've implemented a piece of code to check if the expected_regex is a string or regex object. If it's not it raises a TypeError exception. The check is inside the __init__ method of the _AssertRaisesBaseContext class so it will always check the expected_regex in all methods that use the _AssertRaises. I've added tests too. They are verifying the assertRaisesRegex and the assertWarnsRegex methods. |
Applying changes as suggested by R. David Murray in the Core-mentorship e-mail list. Instead of doing the if tests I've replaced the existing if isinstance(expected_regex, (bytes, str)): by if expected_regex is not None: And also made a change in one of the tests because I figure out it was wrong. |
Thanks, I'ld definitely be _much_ happier w/ a TypeError than with silent success. |
New changeset ec556e45641a by R David Murray in branch 'default': |
Thanks, Kammie. I removed the extra whitespace from your fix and simplified the tests a bit. |
New changeset 3f8b801e7e76 by R David Murray in branch '2.7': New changeset 32407a677215 by R David Murray in branch '3.4': New changeset 8f72f8359987 by R David Murray in branch 'default': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: