8000 bpo-46262: [Enum] test error path in `Flag._missing_` (GH-30408) · python/cpython@91bc6f9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 91bc6f9

Browse files
sobolevnAlexWaygoodethanfurman
authored
bpo-46262: [Enum] test error path in Flag._missing_ (GH-30408)
add tests that exercise the `_missing_` error path for `Flag` and `IntFlag` Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com> Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
1 parent 31e43cb commit 91bc6f9

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

Lib/test/test_enum.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3414,6 +3414,19 @@ class NeverEnum(WhereEnum):
34143414
self.assertFalse(NeverEnum.__dict__.get('_test1', False))
34153415
self.assertFalse(NeverEnum.__dict__.get('_test2', False))
34163416

3417+
def test_default_missing(self):
3418+
with self.assertRaisesRegex(
3419+
ValueError,
3420+
"'RED' is not a valid TestFlag.Color",
3421+
) as ctx:
3422+
self.Color('RED')
3423+
self.assertIs(ctx.exception.__context__, None)
3424+
3425+
P = Flag('P', 'X Y')
3426+
with self.assertRaisesRegex(ValueError, "'X' is not a valid P") as ctx:
3427+
P('X')
3428+
self.assertIs(ctx.exception.__context__, None)
3429+
34173430

34183431
class TestIntFlag(unittest.TestCase):
34193432
"""Tests of the IntFlags."""
@@ -3975,6 +3988,19 @@ def cycle_enum():
39753988
'at least one thread failed while creating composite members')
39763989
self.assertEqual(256, len(seen), 'too many composite members created')
39773990

3991+
def test_default_missing(self):
3992+
with self.assertRaisesRegex(
3993+
ValueError,
3994+
"'RED' is not a valid TestIntFlag.Color",
3995+
) as ctx:
3996+
self.Color('RED')
3997+
self.assertIs(ctx.exception.__context__, None)
3998+
3999+
P = IntFlag('P', 'X Y')
4000+
with self.assertRaisesRegex(ValueError, "'X' is not a valid P") as ctx:
4001+
P('X')
4002+
self.assertIs(ctx.exception.__context__, None)
4003+
39784004

39794005
class TestEmptyAndNonLatinStrings(unittest.TestCase):
39804006

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Cover ``ValueError`` path in tests for :meth:`enum.Flag._missing_`.

0 commit comments

Comments
 (0)
0