mirror of
https://github.com/python/cpython.git
synced 2025-07-12 13:55:34 +00:00
bpo-39728: Enum: fix duplicate ValueError
(GH-22277)
fix default `_missing_` to return `None` instead of raising a `ValueError` Co-authored-by: Andrey Darascheka <andrei.daraschenka@leverx.com>
This commit is contained in:
parent
83f6dcd207
commit
c95ad7a91f
4 changed files with 21 additions and 2 deletions
|
@ -1845,6 +1845,18 @@ class TestEnum(unittest.TestCase):
|
|||
third = auto()
|
||||
self.assertEqual([Dupes.first, Dupes.second, Dupes.third], list(Dupes))
|
||||
|
||||
def test_default_missing(self):
|
||||
class Color(Enum):
|
||||
RED = 1
|
||||
GREEN = 2
|
||||
BLUE = 3
|
||||
try:
|
||||
Color(7)
|
||||
except ValueError as exc:
|
||||
self.assertTrue(exc.__context__ is None)
|
||||
else:
|
||||
raise Exception('Exception not raised.')
|
||||
|
||||
def test_missing(self):
|
||||
class Color(Enum):
|
||||
red = 1
|
||||
|
@ -1863,7 +1875,12 @@ class TestEnum(unittest.TestCase):
|
|||
# trigger not found
|
||||
return None
|
||||
self.assertIs(Color('three'), Color.blue)
|
||||
self.assertRaises(ValueError, Color, 7)
|
||||
try:
|
||||
Color(7)
|
||||
except ValueError as exc:
|
||||
self.assertTrue(exc.__context__ is None)
|
||||
else:
|
||||
raise Exception('Exception not raised.')
|
||||
try:
|
||||
Color('bad return')
|
||||
except TypeError as exc:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue