mirror of
https://github.com/python/cpython.git
synced 2025-08-29 21:25:01 +00:00
closes issue18042 -- a unique
decorator is added to enum.py
The docs also clarify the 'Interesting Example' duplicate-free enum is for demonstration purposes.
This commit is contained in:
parent
d85032e25d
commit
f24bb35a69
3 changed files with 115 additions and 29 deletions
|
@ -2,7 +2,7 @@ import enum
|
|||
import unittest
|
||||
from collections import OrderedDict
|
||||
from pickle import dumps, loads, PicklingError
|
||||
from enum import Enum, IntEnum
|
||||
from enum import Enum, IntEnum, unique
|
||||
|
||||
# for pickle tests
|
||||
try:
|
||||
|
@ -917,5 +917,38 @@ class TestEnum(unittest.TestCase):
|
|||
self.assertEqual(Planet.EARTH.value, (5.976e+24, 6.37814e6))
|
||||
|
||||
|
||||
class TestUnique(unittest.TestCase):
|
||||
|
||||
def test_unique_clean(self):
|
||||
@unique
|
||||
class Clean(Enum):
|
||||
one = 1
|
||||
two = 'dos'
|
||||
tres = 4.0
|
||||
@unique
|
||||
class Cleaner(IntEnum):
|
||||
single = 1
|
||||
double = 2
|
||||
triple = 3
|
||||
|
||||
def test_unique_dirty(self):
|
||||
with self.assertRaisesRegex(ValueError, 'tres.*one'):
|
||||
@unique
|
||||
class Dirty(Enum):
|
||||
one = 1
|
||||
two = 'dos'
|
||||
tres = 1
|
||||
with self.assertRaisesRegex(
|
||||
ValueError,
|
||||
'double.*single.*turkey.*triple',
|
||||
):
|
||||
@unique
|
||||
class Dirtier(IntEnum):
|
||||
single = 1
|
||||
double = 1
|
||||
triple = 3
|
||||
turkey = 3
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue