-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
unittest assertRaises should verify excClass is actually a BaseException class #60040
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
The following code in a unittest test is a no-op: self.assertRaises(lambda: 1) I would expect this to fail the test, because I naively assumed omitting the exception class would act as: self.assertRaises(BaseException, lambda: 1) verifying that *any* Exception is raised. I believe the correct behaviour is to raise a TypeError if excClass is not a BaseException-derived type, similar to if a non-type is passed as the first arg to issubclass. Attached is a patch to do so. It also removes a no-op self.assertRaises from libimport's tests (because it started failing when I ran the tests with the patch). That assertion is redundant, because the two lines above perform the assertion being attempted. |
Sounds like a reasonable suggestion. However, the patch is not valid for 2.7, since there exceptions can be old style classes. |
I put some review comments in rietveld (you should have gotten an email). |
I seem to be getting exceptions why trying to upload a new patch to rietveld, either by the web interface (in several browsers), or by upload.py - attaching new patchset here Also, if I wanted to backport to 2.7 including an isinstance(e, types.ClassType) check, how would I go about doing so? Thanks for the quick review! |
Uploading the new patch here is the correct procedure. It will automatically be uploaded to rietveld as well. If by "how" you mean how to submit a backport, just create a patch against 2.7 tip and upload it separately. Revised patch looks good. |
Added patch for 2.7. I'll sign the contributor form just as soon as I can get to a printer. Thanks for taking me through my first contribution. |
You are welcome, and thanks for your contribution. |
Yep, certainly worth fixing. When 3.3 is out the door I will look at applying this to all branches. |
Since it is a bugfix it can be applied at any time now. Checkins to default will end up in 3.3.1 and 3.4. (Only features need to wait until after 3.3 is branched in the main repo.) |
Cool, my contributor agreement has been received, please merge if happy! |
Would using assertRaises to test assertRaises in the tests be to meta? |
Ezio: I don't really care whether or not it would be too meta, if you look at the two versions, it is a *lot* clearer what is being tested in the try/except version than it is in the assertRaises version. |
I missed the initial patch. What I was thinking about was to use simply with self.assertRaises(TypeError):
self.assertRaises(1) instead of: + ctx = self.assertRaises(TypeError) or + try: Unless I'm missing something, all these should be equivalent. |
Is anything blocking this patch's submission? |
The patch is just waiting for me to look over it and commit. I'll get to it ASAP. |
This seems to be a reasonable fix. Michael, could you have a look at this patch, please? |
Ezio requested I comment on his suggestion: I still prefer the try/except form, but I don't feel strongly about it. |
I'm +0.5 for the variant suggested by Berker and Ezio. Do you have time to look at the patch Michael? I could commit modified patch (there is one defect in tests). |
I like the first variant suggested by Ezio as more concise. I'll try and look at the substance of the patch today. |
The change to unittest is fine. I'd prefer the tests tweaking as Ezio suggested. |
Ping. |
Since the patch has been reviewed by several core developers, I think you can go ahead and commit it. I'm +0 on the 2.7 version of the patch (the isinstance(e, types.ClassType) part looks fine, but I haven't tested it). It's probably not worth to change anything on the 2.7 branch now :) |
Core developers left a couple of notes and the patch itself is outdated. Here is updated patch that addresses all comments. It also extends the checking to assertRaisesRegex(), assertWarns() and assertWarnsRegex(). There is a risk to break existing incorrect tests if the argument is a tuple. They can be passed for now because caught exception or warning is found before incorrect value. For example: with self.assertRaises((ValueError, None)):
int('a') |
I posted a question on Reitveld, but the new patch looks fine in general. I wouldn’t worry too much about the (ValueError, None) case, since such code is probably already broken. If it is a problem though, maybe this could only go into the next feature relase (3.5). |
New changeset 84d7ec21cc43 by Serhiy Storchaka in branch 'default': |
Applied to 3.5 only, because this issue looks rather as new feature (preventing possible user error) and there is minimal chance to break existing tests (see bpo-24134). |
Thank you for your contribution Daniel. |
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: