Skip to content
Merged
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
35 changes: 25 additions & 10 deletions Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -4449,6 +4449,12 @@ def test__all__(self):
COMPLEX_A = 2j
COMPLEX_B = 3j

class _ModuleWrapper:
Copy link
Member

Choose a reason for hiding this comment

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

I like this. Please switch over the other _convert_ tests to use it, which will allow the removal of the setUp() methods in those tests.

"""We use this class as a namespace for swapping modules."""

def __init__(self, module):
self.__dict__.update(module.__dict__)

class TestIntEnumConvert(unittest.TestCase):
def setUp(self):
# Reset the module-level test variables to their original integer
Expand Down Expand Up @@ -4487,11 +4493,17 @@ def test_convert(self):
[], msg='Names other than CONVERT_TEST_* found.')

def test_convert_uncomparable(self):
uncomp = enum.Enum._convert_(
'Uncomparable',
MODULE,
filter=lambda x: x.startswith('UNCOMPARABLE_'),
)
# We swap a module to some other object with `__dict__`
# because otherwise refleak is created.
# `_convert_` uses a module side effect that does this. See 30472
with support.swap_item(
sys.modules, MODULE, _ModuleWrapper(sys.modules[MODULE]),
):
Copy link
Member

Choose a reason for hiding this comment

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

Do not use Black formatting on enum code, please. Continuation lines should be eight space indents, and each closing ), ], and } should line up as if it were code and not syntax, with the final line (if it introduces a block) only indented the normal four spaces. In other words:

a_long_list_comprehension = [
        x
        for x in spam(eggs, ham)
        if x not in some_previous_group
        ]

and

with yada(
        arg1, arg2, something_else=None,
    ):
    # body of code

uncomp = enum.Enum._convert_(
'Uncomparable',
MODULE,
filter=lambda x: x.startswith('UNCOMPARABLE_'),
)

# Should be ordered by `name` only:
self.assertEqual(
Expand All @@ -4500,11 +4512,14 @@ def test_convert_uncomparable(self):
)

def test_convert_complex(self):
uncomp = enum.Enum._convert_(
'Uncomparable',
MODULE,
filter=lambda x: x.startswith('COMPLEX_'),
)
with support.swap_item(
sys.modules, MODULE, _ModuleWrapper(sys.modules[MODULE]),
):
uncomp = enum.Enum._convert_(
'Uncomparable',
MODULE,
filter=lambda x: x.startswith('COMPLEX_'),
)

# Should be ordered by `name` only:
self.assertEqual(
Expand Down