Skip to content
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

change assertRaises message when wrong exception is raised #87762

Closed
rsk2 mannequin opened this issue Mar 22, 2021 · 6 comments
Closed

change assertRaises message when wrong exception is raised #87762

rsk2 mannequin opened this issue Mar 22, 2021 · 6 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@rsk2
Copy link
Mannequin

rsk2 mannequin commented Mar 22, 2021

BPO 43596
Nosy @mdickinson, @serhiy-storchaka, @nathanielmanistaatgoogle, @tirkarthi, @iritkatriel, @kamilturek

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:

assignee = None
closed_at = <Date 2021-08-01.14:53:55.651>
created_at = <Date 2021-03-22.16:16:06.909>
labels = ['type-feature', 'library']
title = 'change assertRaises message when wrong exception is raised'
updated_at = <Date 2021-08-01.14:56:19.346>
user = 'https://bugs.python.org/rsk2'

bugs.python.org fields:

activity = <Date 2021-08-01.14:56:19.346>
actor = 'iritkatriel'
assignee = 'none'
closed = True
closed_date = <Date 2021-08-01.14:53:55.651>
closer = 'mark.dickinson'
components = ['Library (Lib)']
creation = <Date 2021-03-22.16:16:06.909>
creator = 'rsk2'
dependencies = []
files = []
hgrepos = []
issue_num = 43596
keywords = []
message_count = 6.0
messages = ['389328', '398516', '398691', '398693', '398701', '398702']
nosy_count = 7.0
nosy_names = ['mark.dickinson', 'serhiy.storchaka', 'Nathaniel Manista', 'xtreak', 'iritkatriel', 'kamilturek', 'rsk2']
pr_nums = []
priority = 'normal'
resolution = 'rejected'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue43596'
versions = []

@rsk2
Copy link
Mannequin Author

rsk2 mannequin commented Mar 22, 2021

Right now, this code:

    class FooError(Exception): pass
    class BarError(Exception): pass

    def test_me(self):
      with self.assertRaises(FooError):
        raise BarError("something")

will have the error "BarError: something" with no indication that an exception was expected but just that we got the wrong one.

It would be help to change the message to something like:

Expected exception of type FooError but exception BarError('something') was raised.

@iritkatriel
Copy link
Member

What you are suggesting replaces the type of the exception being raised. If it's something like a MemoryError or KeyboardInterrupt you don't want that, you want your test process to terminate.

@iritkatriel iritkatriel added stdlib Python modules in the Lib dir labels Jul 29, 2021
@iritkatriel
Copy link
Member

Another way to look at it is that your test has two problems. One is that the expected exception didn’t happen and the other is that an unexpected exception happened.

The two problems are usually unrelated, and trying to conflate them into one error message can be confusing in some cases. When a test has multiple problems you get an error for the first one.

@serhiy-storchaka
Copy link
Member

I concur with Irit. The test is failed in any case, so you need to look at the code to fix it.

For the same reason we do not have assertNotRaises().

@mdickinson
Copy link
Member

I think I understand where the request comes from: in the case where you've changed the exception type that an API is expected to raise, and you've either updated the test to check for the new exception type and missed an update in the business logic, or vice versa, you'd like the test to tell you that the wrong exception type was raised. I've definitely done this myself on more than a few occasions.

So this *particular* case is an exception to Irit's observation that "The two problems are usually unrelated [...]". But I think it *is* an exception: I agree that the rule holds in general, and that the current behaviour shouldn't be changed.

I think we've got sufficient agreement from core devs to close here.

@mdickinson mdickinson added the type-feature A feature request or enhancement label Aug 1, 2021
@mdickinson mdickinson added the type-feature A feature request or enhancement label Aug 1, 2021
@iritkatriel
Copy link
Member

I agree. That’s what the “usually” was for.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants