8000 bpo-46262: cover error path in `enum.Flag._missing_` by sobolevn · Pull Request #30408 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-46262: cover error path in enum.Flag._missing_ #30408

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

Merged
merged 3 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 26 additions & 0 deletions Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -3414,6 +3414,19 @@ class NeverEnum(WhereEnum):
self.assertFalse(NeverEnum.__dict__.get('_test1', False))
self.assertFalse(NeverEnum.__dict__.get('_test2', False))

def test_default_missing(self):
with self.assertRaisesRegex(
ValueError,
"'RED' is not a valid TestFlag.Color",
) as ctx:
self.Color('RED')
self.assertIs(ctx.exception.__context__, None)

P = Flag('P', 'X Y')
with self.assertRaisesRegex(ValueError, "'X' is not a valid P") as ctx:
P('X')
self.assertIs(ctx.exception.__context__, None)


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

def test_default_missing(self):
with self.assertRaisesRegex(
ValueError,
"'RED' is not a valid TestIntFlag.Color",
) as ctx:
self.Color('RED')
self.assertIs(ctx.exception.__context__, None)

P = IntFlag('P', 'X Y')
with self.assertRaisesRegex(ValueError, "'X' is not a valid P") as ctx:
P('X')
self.assertIs(ctx.exception.__context__, None)


class TestEmptyAndNonLatinStrings(unittest.TestCase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cover ``ValueError`` path in tests for :meth:`enum.Flag._missing_`.
0