bpo-35899: Support of empty and weird strings by Enum#11840
Conversation
There was a problem hiding this comment.
There should be a test for the original report where empty string causes an error like Yay = Enum('Yay', ('', 'B', 'C')) in https://bugs.python.org/issue35899#msg334866
There was a problem hiding this comment.
Hi @tirkarthi
Thank you for your feedback, I have just added this test.
bdbaraban
left a comment
There was a problem hiding this comment.
Building on @tirkarthi's comment, I'd like to offer alternative tests that would test both empty and "weird" strings while also aligning more with the existing unittest layout in test_enum.py. I propose the following unittest functions to be entered starting at line 762 in the original test_enum.py file:
def test_programmatic_function_string_list_with_empty_string(self):
SummerMonth = Enum('SummerMonth', ['june', 'july', ''])
lst = list(SummerMonth)
self.assertEqual(len(lst), len(SummerMonth))
self.assertEqual(len(SummerMonth), 3, SummerMonth)
self.assertEqual(
[SummerMonth.june, SummerMonth.july, getattr(SummerMonth, '')],
lst
)
for i, month in enumerate(['june', 'july', ''], 1):
e = SummerMonth(i)
self.assertEqual(int(e.value), i)
self.assertNotEqual(e, i)
self.assertEqual(e.name, month)
self.assertIn(e, SummerMonth)
self.assertIs(type(e), SummerMonth)
def test_programmatic_function_string_list_with_weird_string(self):
SummerMonth = Enum('SummerMonth', ['june', 'july', '!'])
lst = list(SummerMonth)
self.assertEqual(len(lst), len(SummerMonth))
self.assertEqual(len(SummerMonth), 3, SummerMonth)
self.assertEqual(
[SummerMonth.june, SummerMonth.july, getattr(SummerMonth, '!')],
lst
)
for i, month in enumerate('june july !'.split(), 1):
e = SummerMonth(i)
self.assertEqual(int(e.value), i)
self.assertNotEqual(e, i)
self.assertEqual(e.name, month)
self.assertIn(e, SummerMonth)
self.assertIs(type(e), SummerMonth)
|
Hi @bdbaraban
Thank you |
Co-authored-by: Maxwell <maxwellpxt@gmail.com> Co-authored-by: Stéphane Wirtel <stephane@wirtel.be>
Understood, thank you for the feedback! For clarification purposes, I based these tests off the other |
|
@bdbaraban For my part, I prefer to use subTest (or in the case of pytest, Now, when we write new code for Python, test or code, we don't use the new features of Python. For example, the PEP 572 has been merged into CPython, we won't start to rewrite the library and the tests with the new syntax. It's the same with the f-string. |
|
@ethanfurman I close this PR because you can use the PR of @bdbaraban. Have a nice day, Stéphane |
Co-authored-by: Maxwell maxwellpxt@gmail.com
Co-authored-by: Stéphane Wirtel stephane@wirtel.be
https://bugs.python.org/issue35899