8000 bpo-39728: enum with invalid value results in ValueError twice by dorosch · Pull Request #18641 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-39728: enum with invalid value results in ValueError twice #18641

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

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
bpo-39728: Fixed after review
  • Loading branch information
Andrey Darascheka committed Feb 24, 2020
commit 72eb2c4fe3acdcd77d67a6a8160aa4e99691a1b9
1 change: 1 addition & 0 deletions Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ def __new__(cls, value):
'error in %s._missing_: returned %r instead of None or a valid member'
% (cls.__name__, result)
)
exc.__context__ = ve_exc
raise exc

def _generate_next_value_(name, start, count, last_values):
Expand Down
12 changes: 10 additions & 2 deletions Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -1778,10 +1778,18 @@ def _missing_(cls, item):
self.assertIs(Color('three'), Color.blue)
with self.assertRaises(ValueError):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This statement passes on the old code as well. Is it possible to specifically assert that a ValueError has been raised, but without the second ValueError being raised in the process?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that can (and should) be tested, with something like this:

        with self.assertRaises(ValueError) as cm:
            Color(7)
        self.assertIsNone(cm.exception.__context__)

Color(7)
with self.assertRaises(TypeError):
try:
Color('bad return')
with self.assertRaises(ZeroDivisionError):
except TypeError as exc:
self.assertTrue(isinstance(exc.__context__, ValueError))
else:
raise Exception('Exception not raised.')
try:
Color('error out')
except ZeroDivisionError as exc:
self.assertTrue(not exc.__context__)
else:
raise Exception('Exception not raised.')

def test_multiple_mixin(self):
class MaxMixin:
Expand Down
0