test.py 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867
  1. from pickle import dumps, loads, PicklingError, HIGHEST_PROTOCOL
  2. import sys
  3. import unittest
  4. pyver = float('%s.%s' % sys.version_info[:2])
  5. if pyver < 2.5:
  6. sys.path.insert(0, '.')
  7. import enum
  8. from enum import Enum, IntEnum, unique, EnumMeta
  9. if pyver < 2.6:
  10. from __builtin__ import enumerate as bltin_enumerate
  11. def enumerate(thing, start=0):
  12. result = []
  13. for i, item in bltin_enumerate(thing):
  14. i = i + start
  15. result.append((i, item))
  16. return result
  17. try:
  18. any
  19. except NameError:
  20. def any(iterable):
  21. for element in iterable:
  22. if element:
  23. return True
  24. return False
  25. try:
  26. unicode
  27. except NameError:
  28. unicode = str
  29. try:
  30. from collections import OrderedDict
  31. except ImportError:
  32. OrderedDict = None
  33. # for pickle tests
  34. try:
  35. class Stooges(Enum):
  36. LARRY = 1
  37. CURLY = 2
  38. MOE = 3
  39. except Exception:
  40. Stooges = sys.exc_info()[1]
  41. try:
  42. class IntStooges(int, Enum):
  43. LARRY = 1
  44. CURLY = 2
  45. MOE = 3
  46. except Exception:
  47. IntStooges = sys.exc_info()[1]
  48. try:
  49. class FloatStooges(float, Enum):
  50. LARRY = 1.39
  51. CURLY = 2.72
  52. MOE = 3.142596
  53. except Exception:
  54. FloatStooges = sys.exc_info()[1]
  55. # for pickle test and subclass tests
  56. try:
  57. class StrEnum(str, Enum):
  58. 'accepts only string values'
  59. class Name(StrEnum):
  60. BDFL = 'Guido van Rossum'
  61. FLUFL = 'Barry Warsaw'
  62. except Exception:
  63. Name = sys.exc_info()[1]
  64. try:
  65. Question = Enum('Question', 'who what when where why', module=__name__)
  66. except Exception:
  67. Question = sys.exc_info()[1]
  68. try:
  69. Answer = Enum('Answer', 'him this then there because')
  70. except Exception:
  71. Answer = sys.exc_info()[1]
  72. try:
  73. Theory = Enum('Theory', 'rule law supposition', qualname='spanish_inquisition')
  74. except Exception:
  75. Theory = sys.exc_info()[1]
  76. # for doctests
  77. try:
  78. class Fruit(Enum):
  79. tomato = 1
  80. banana = 2
  81. cherry = 3
  82. except Exception:
  83. pass
  84. import pytest
  85. def check_pickle_dump_load(assertion, source, target=None,
  86. protocol=(0, HIGHEST_PROTOCOL)):
  87. start, stop = protocol
  88. failures = []
  89. for protocol in range(start, stop+1):
  90. try:
  91. if target is None:
  92. assertion(loads(dumps(source, protocol=protocol)) is source)
  93. else:
  94. assertion(loads(dumps(source, protocol=protocol)), target)
  95. except Exception:
  96. exc, tb = sys.exc_info()[1:]
  97. failures.append('%2d: %s' %(protocol, exc))
  98. if failures:
  99. raise ValueError('Failed with protocols: %s' % ', '.join(failures))
  100. def check_pickle_exception(assertion, exception, obj,
  101. protocol=(0, HIGHEST_PROTOCOL)):
  102. start, stop = protocol
  103. failures = []
  104. for protocol in range(start, stop+1):
  105. try:
  106. assertion(exception, dumps, obj, protocol=protocol)
  107. except Exception:
  108. exc = sys.exc_info()[1]
  109. failures.append('%d: %s %s' % (protocol, exc.__class__.__name__, exc))
  110. if failures:
  111. raise ValueError('Failed with protocols: %s' % ', '.join(failures))
  112. class TestHelpers(unittest.TestCase):
  113. # _is_descriptor, _is_sunder, _is_dunder
  114. def test_is_descriptor(self):
  115. class foo:
  116. pass
  117. for attr in ('__get__','__set__','__delete__'):
  118. obj = foo()
  119. self.assertFalse(enum._is_descriptor(obj))
  120. setattr(obj, attr, 1)
  121. self.assertTrue(enum._is_descriptor(obj))
  122. def test_is_sunder(self):
  123. for s in ('_a_', '_aa_'):
  124. self.assertTrue(enum._is_sunder(s))
  125. for s in ('a', 'a_', '_a', '__a', 'a__', '__a__', '_a__', '__a_', '_',
  126. '__', '___', '____', '_____',):
  127. self.assertFalse(enum._is_sunder(s))
  128. def test_is_dunder(self):
  129. for s in ('__a__', '__aa__'):
  130. self.assertTrue(enum._is_dunder(s))
  131. for s in ('a', 'a_', '_a', '__a', 'a__', '_a_', '_a__', '__a_', '_',
  132. '__', '___', '____', '_____',):
  133. self.assertFalse(enum._is_dunder(s))
  134. class TestEnum(unittest.TestCase):
  135. def setUp(self):
  136. class Season(Enum):
  137. SPRING = 1
  138. SUMMER = 2
  139. AUTUMN = 3
  140. WINTER = 4
  141. self.Season = Season
  142. class Konstants(float, Enum):
  143. E = 2.7182818
  144. PI = 3.1415926
  145. TAU = 2 * PI
  146. self.Konstants = Konstants
  147. class Grades(IntEnum):
  148. A = 5
  149. B = 4
  150. C = 3
  151. D = 2
  152. F = 0
  153. self.Grades = Grades
  154. class Directional(str, Enum):
  155. EAST = 'east'
  156. WEST = 'west'
  157. NORTH = 'north'
  158. SOUTH = 'south'
  159. self.Directional = Directional
  160. from datetime import date
  161. class Holiday(date, Enum):
  162. NEW_YEAR = 2013, 1, 1
  163. IDES_OF_MARCH = 2013, 3, 15
  164. self.Holiday = Holiday
  165. if pyver >= 3.0: # do not specify custom `dir` on previous versions
  166. def test_dir_on_class(self):
  167. Season = self.Season
  168. self.assertEqual(
  169. set(dir(Season)),
  170. set(['__class__', '__doc__', '__members__', '__module__',
  171. 'SPRING', 'SUMMER', 'AUTUMN', 'WINTER']),
  172. )
  173. def test_dir_on_item(self):
  174. Season = self.Season
  175. self.assertEqual(
  176. set(dir(Season.WINTER)),
  177. set(['__class__', '__doc__', '__module__', 'name', 'value']),
  178. )
  179. def test_dir_with_added_behavior(self):
  180. class Test(Enum):
  181. this = 'that'
  182. these = 'those'
  183. def wowser(self):
  184. return ("Wowser! I'm %s!" % self.name)
  185. self.assertEqual(
  186. set(dir(Test)),
  187. set(['__class__', '__doc__', '__members__', '__module__', 'this', 'these']),
  188. )
  189. self.assertEqual(
  190. set(dir(Test.this)),
  191. set(['__class__', '__doc__', '__module__', 'name', 'value', 'wowser']),
  192. )
  193. def test_dir_on_sub_with_behavior_on_super(self):
  194. # see issue22506
  195. class SuperEnum(Enum):
  196. def invisible(self):
  197. return "did you see me?"
  198. class SubEnum(SuperEnum):
  199. sample = 5
  200. self.assertEqual(
  201. set(dir(SubEnum.sample)),
  202. set(['__class__', '__doc__', '__module__', 'name', 'value', 'invisible']),
  203. )
  204. if pyver >= 2.7: # OrderedDict first available here
  205. def test_members_is_ordereddict_if_ordered(self):
  206. class Ordered(Enum):
  207. __order__ = 'first second third'
  208. first = 'bippity'
  209. second = 'boppity'
  210. third = 'boo'
  211. self.assertTrue(type(Ordered.__members__) is OrderedDict)
  212. def test_members_is_ordereddict_if_not_ordered(self):
  213. class Unordered(Enum):
  214. this = 'that'
  215. these = 'those'
  216. self.assertTrue(type(Unordered.__members__) is OrderedDict)
  217. if pyver >= 3.0: # all objects are ordered in Python 2.x
  218. def test_members_is_always_ordered(self):
  219. class AlwaysOrdered(Enum):
  220. first = 1
  221. second = 2
  222. third = 3
  223. self.assertTrue(type(AlwaysOrdered.__members__) is OrderedDict)
  224. def test_comparisons(self):
  225. def bad_compare():
  226. Season.SPRING > 4
  227. Season = self.Season
  228. self.assertNotEqual(Season.SPRING, 1)
  229. self.assertRaises(TypeError, bad_compare)
  230. class Part(Enum):
  231. SPRING = 1
  232. CLIP = 2
  233. BARREL = 3
  234. self.assertNotEqual(Season.SPRING, Part.SPRING)
  235. def bad_compare():
  236. Season.SPRING < Part.CLIP
  237. self.assertRaises(TypeError, bad_compare)
  238. def test_enum_in_enum_out(self):
  239. Season = self.Season
  240. self.assertTrue(Season(Season.WINTER) is Season.WINTER)
  241. def test_enum_value(self):
  242. Season = self.Season
  243. self.assertEqual(Season.SPRING.value, 1)
  244. def test_intenum_value(self):
  245. self.assertEqual(IntStooges.CURLY.value, 2)
  246. def test_enum(self):
  247. Season = self.Season
  248. lst = list(Season)
  249. self.assertEqual(len(lst), len(Season))
  250. self.assertEqual(len(Season), 4, Season)
  251. self.assertEqual(
  252. [Season.SPRING, Season.SUMMER, Season.AUTUMN, Season.WINTER], lst)
  253. for i, season in enumerate('SPRING SUMMER AUTUMN WINTER'.split()):
  254. i += 1
  255. e = Season(i)
  256. self.assertEqual(e, getattr(Season, season))
  257. self.assertEqual(e.value, i)
  258. self.assertNotEqual(e, i)
  259. self.assertEqual(e.name, season)
  260. self.assertTrue(e in Season)
  261. self.assertTrue(type(e) is Season)
  262. self.assertTrue(isinstance(e, Season))
  263. self.assertEqual(str(e), 'Season.' + season)
  264. self.assertEqual(
  265. repr(e),
  266. '<Season.%s: %s>' % (season, i),
  267. )
  268. def test_value_name(self):
  269. Season = self.Season
  270. self.assertEqual(Season.SPRING.name, 'SPRING')
  271. self.assertEqual(Season.SPRING.value, 1)
  272. def set_name(obj, new_value):
  273. obj.name = new_value
  274. def set_value(obj, new_value):
  275. obj.value = new_value
  276. self.assertRaises(AttributeError, set_name, Season.SPRING, 'invierno', )
  277. self.assertRaises(AttributeError, set_value, Season.SPRING, 2)
  278. def test_attribute_deletion(self):
  279. class Season(Enum):
  280. SPRING = 1
  281. SUMMER = 2
  282. AUTUMN = 3
  283. WINTER = 4
  284. def spam(cls):
  285. pass
  286. self.assertTrue(hasattr(Season, 'spam'))
  287. del Season.spam
  288. self.assertFalse(hasattr(Season, 'spam'))
  289. self.assertRaises(AttributeError, delattr, Season, 'SPRING')
  290. self.assertRaises(AttributeError, delattr, Season, 'DRY')
  291. self.assertRaises(AttributeError, delattr, Season.SPRING, 'name')
  292. def test_bool_of_class(self):
  293. class Empty(Enum):
  294. pass
  295. self.assertTrue(bool(Empty))
  296. def test_bool_of_member(self):
  297. class Count(Enum):
  298. zero = 0
  299. one = 1
  300. two = 2
  301. for member in Count:
  302. self.assertTrue(bool(member))
  303. def test_invalid_names(self):
  304. def create_bad_class_1():
  305. class Wrong(Enum):
  306. mro = 9
  307. def create_bad_class_2():
  308. class Wrong(Enum):
  309. _reserved_ = 3
  310. self.assertRaises(ValueError, create_bad_class_1)
  311. self.assertRaises(ValueError, create_bad_class_2)
  312. def test_contains(self):
  313. Season = self.Season
  314. self.assertTrue(Season.AUTUMN in Season)
  315. self.assertTrue(3 not in Season)
  316. val = Season(3)
  317. self.assertTrue(val in Season)
  318. class OtherEnum(Enum):
  319. one = 1; two = 2
  320. self.assertTrue(OtherEnum.two not in Season)
  321. if pyver >= 2.6: # when `format` came into being
  322. def test_format_enum(self):
  323. Season = self.Season
  324. self.assertEqual('{0}'.format(Season.SPRING),
  325. '{0}'.format(str(Season.SPRING)))
  326. self.assertEqual( '{0:}'.format(Season.SPRING),
  327. '{0:}'.format(str(Season.SPRING)))
  328. self.assertEqual('{0:20}'.format(Season.SPRING),
  329. '{0:20}'.format(str(Season.SPRING)))
  330. self.assertEqual('{0:^20}'.format(Season.SPRING),
  331. '{0:^20}'.format(str(Season.SPRING)))
  332. self.assertEqual('{0:>20}'.format(Season.SPRING),
  333. '{0:>20}'.format(str(Season.SPRING)))
  334. self.assertEqual('{0:<20}'.format(Season.SPRING),
  335. '{0:<20}'.format(str(Season.SPRING)))
  336. def test_format_enum_custom(self):
  337. class TestFloat(float, Enum):
  338. one = 1.0
  339. two = 2.0
  340. def __format__(self, spec):
  341. return 'TestFloat success!'
  342. self.assertEqual('{0}'.format(TestFloat.one), 'TestFloat success!')
  343. def assertFormatIsValue(self, spec, member):
  344. self.assertEqual(spec.format(member), spec.format(member.value))
  345. def test_format_enum_date(self):
  346. Holiday = self.Holiday
  347. self.assertFormatIsValue('{0}', Holiday.IDES_OF_MARCH)
  348. self.assertFormatIsValue('{0:}', Holiday.IDES_OF_MARCH)
  349. self.assertFormatIsValue('{0:20}', Holiday.IDES_OF_MARCH)
  350. self.assertFormatIsValue('{0:^20}', Holiday.IDES_OF_MARCH)
  351. self.assertFormatIsValue('{0:>20}', Holiday.IDES_OF_MARCH)
  352. self.assertFormatIsValue('{0:<20}', Holiday.IDES_OF_MARCH)
  353. self.assertFormatIsValue('{0:%Y %m}', Holiday.IDES_OF_MARCH)
  354. self.assertFormatIsValue('{0:%Y %m %M:00}', Holiday.IDES_OF_MARCH)
  355. def test_format_enum_float(self):
  356. Konstants = self.Konstants
  357. self.assertFormatIsValue('{0}', Konstants.TAU)
  358. self.assertFormatIsValue('{0:}', Konstants.TAU)
  359. self.assertFormatIsValue('{0:20}', Konstants.TAU)
  360. self.assertFormatIsValue('{0:^20}', Konstants.TAU)
  361. self.assertFormatIsValue('{0:>20}', Konstants.TAU)
  362. self.assertFormatIsValue('{0:<20}', Konstants.TAU)
  363. self.assertFormatIsValue('{0:n}', Konstants.TAU)
  364. self.assertFormatIsValue('{0:5.2}', Konstants.TAU)
  365. self.assertFormatIsValue('{0:f}', Konstants.TAU)
  366. def test_format_enum_int(self):
  367. Grades = self.Grades
  368. self.assertFormatIsValue('{0}', Grades.C)
  369. self.assertFormatIsValue('{0:}', Grades.C)
  370. self.assertFormatIsValue('{0:20}', Grades.C)
  371. self.assertFormatIsValue('{0:^20}', Grades.C)
  372. self.assertFormatIsValue('{0:>20}', Grades.C)
  373. self.assertFormatIsValue('{0:<20}', Grades.C)
  374. self.assertFormatIsValue('{0:+}', Grades.C)
  375. self.assertFormatIsValue('{0:08X}', Grades.C)
  376. self.assertFormatIsValue('{0:b}', Grades.C)
  377. def test_format_enum_str(self):
  378. Directional = self.Directional
  379. self.assertFormatIsValue('{0}', Directional.WEST)
  380. self.assertFormatIsValue('{0:}', Directional.WEST)
  381. self.assertFormatIsValue('{0:20}', Directional.WEST)
  382. self.assertFormatIsValue('{0:^20}', Directional.WEST)
  383. self.assertFormatIsValue('{0:>20}', Directional.WEST)
  384. self.assertFormatIsValue('{0:<20}', Directional.WEST)
  385. def test_hash(self):
  386. Season = self.Season
  387. dates = {}
  388. dates[Season.WINTER] = '1225'
  389. dates[Season.SPRING] = '0315'
  390. dates[Season.SUMMER] = '0704'
  391. dates[Season.AUTUMN] = '1031'
  392. self.assertEqual(dates[Season.AUTUMN], '1031')
  393. def test_enum_duplicates(self):
  394. class Season(Enum):
  395. _order_ = "SPRING SUMMER AUTUMN WINTER"
  396. SPRING = 1
  397. SUMMER = 2
  398. AUTUMN = FALL = 3
  399. WINTER = 4
  400. ANOTHER_SPRING = 1
  401. lst = list(Season)
  402. self.assertEqual(
  403. lst,
  404. [Season.SPRING, Season.SUMMER,
  405. Season.AUTUMN, Season.WINTER,
  406. ])
  407. self.assertTrue(Season.FALL is Season.AUTUMN)
  408. self.assertEqual(Season.FALL.value, 3)
  409. self.assertEqual(Season.AUTUMN.value, 3)
  410. self.assertTrue(Season(3) is Season.AUTUMN)
  411. self.assertTrue(Season(1) is Season.SPRING)
  412. self.assertEqual(Season.FALL.name, 'AUTUMN')
  413. self.assertEqual(
  414. set([k for k,v in Season.__members__.items() if v.name != k]),
  415. set(['FALL', 'ANOTHER_SPRING']),
  416. )
  417. if pyver >= 3.0:
  418. cls = vars()
  419. result = {'Enum':Enum}
  420. exec("""def test_duplicate_name(self):
  421. with self.assertRaises(TypeError):
  422. class Color(Enum):
  423. red = 1
  424. green = 2
  425. blue = 3
  426. red = 4
  427. with self.assertRaises(TypeError):
  428. class Color(Enum):
  429. red = 1
  430. green = 2
  431. blue = 3
  432. def red(self):
  433. return 'red'
  434. with self.assertRaises(TypeError):
  435. class Color(Enum):
  436. @property
  437. def red(self):
  438. return 'redder'
  439. red = 1
  440. green = 2
  441. blue = 3""",
  442. result)
  443. cls['test_duplicate_name'] = result['test_duplicate_name']
  444. def test_enum_with_value_name(self):
  445. class Huh(Enum):
  446. name = 1
  447. value = 2
  448. self.assertEqual(
  449. list(Huh),
  450. [Huh.name, Huh.value],
  451. )
  452. self.assertTrue(type(Huh.name) is Huh)
  453. self.assertEqual(Huh.name.name, 'name')
  454. self.assertEqual(Huh.name.value, 1)
  455. def test_intenum_from_scratch(self):
  456. class phy(int, Enum):
  457. pi = 3
  458. tau = 2 * pi
  459. self.assertTrue(phy.pi < phy.tau)
  460. def test_intenum_inherited(self):
  461. class IntEnum(int, Enum):
  462. pass
  463. class phy(IntEnum):
  464. pi = 3
  465. tau = 2 * pi
  466. self.assertTrue(phy.pi < phy.tau)
  467. def test_floatenum_from_scratch(self):
  468. class phy(float, Enum):
  469. pi = 3.1415926
  470. tau = 2 * pi
  471. self.assertTrue(phy.pi < phy.tau)
  472. def test_floatenum_inherited(self):
  473. class FloatEnum(float, Enum):
  474. pass
  475. class phy(FloatEnum):
  476. pi = 3.1415926
  477. tau = 2 * pi
  478. self.assertTrue(phy.pi < phy.tau)
  479. def test_strenum_from_scratch(self):
  480. class phy(str, Enum):
  481. pi = 'Pi'
  482. tau = 'Tau'
  483. self.assertTrue(phy.pi < phy.tau)
  484. def test_strenum_inherited(self):
  485. class StrEnum(str, Enum):
  486. pass
  487. class phy(StrEnum):
  488. pi = 'Pi'
  489. tau = 'Tau'
  490. self.assertTrue(phy.pi < phy.tau)
  491. def test_intenum(self):
  492. class WeekDay(IntEnum):
  493. SUNDAY = 1
  494. MONDAY = 2
  495. TUESDAY = 3
  496. WEDNESDAY = 4
  497. THURSDAY = 5
  498. FRIDAY = 6
  499. SATURDAY = 7
  500. self.assertEqual(['a', 'b', 'c'][WeekDay.MONDAY], 'c')
  501. self.assertEqual([i for i in range(WeekDay.TUESDAY)], [0, 1, 2])
  502. lst = list(WeekDay)
  503. self.assertEqual(len(lst), len(WeekDay))
  504. self.assertEqual(len(WeekDay), 7)
  505. target = 'SUNDAY MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY'
  506. target = target.split()
  507. for i, weekday in enumerate(target):
  508. i += 1
  509. e = WeekDay(i)
  510. self.assertEqual(e, i)
  511. self.assertEqual(int(e), i)
  512. self.assertEqual(e.name, weekday)
  513. self.assertTrue(e in WeekDay)
  514. self.assertEqual(lst.index(e)+1, i)
  515. self.assertTrue(0 < e < 8)
  516. self.assertTrue(type(e) is WeekDay)
  517. self.assertTrue(isinstance(e, int))
  518. self.assertTrue(isinstance(e, Enum))
  519. def test_intenum_duplicates(self):
  520. class WeekDay(IntEnum):
  521. __order__ = 'SUNDAY MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY'
  522. SUNDAY = 1
  523. MONDAY = 2
  524. TUESDAY = TEUSDAY = 3
  525. WEDNESDAY = 4
  526. THURSDAY = 5
  527. FRIDAY = 6
  528. SATURDAY = 7
  529. self.assertTrue(WeekDay.TEUSDAY is WeekDay.TUESDAY)
  530. self.assertEqual(WeekDay(3).name, 'TUESDAY')
  531. self.assertEqual([k for k,v in WeekDay.__members__.items()
  532. if v.name != k], ['TEUSDAY', ])
  533. def test_pickle_enum(self):
  534. if isinstance(Stooges, Exception):
  535. raise Stooges
  536. check_pickle_dump_load(self.assertTrue, Stooges.CURLY)
  537. check_pickle_dump_load(self.assertTrue, Stooges)
  538. def test_pickle_int(self):
  539. if isinstance(IntStooges, Exception):
  540. raise IntStooges
  541. check_pickle_dump_load(self.assertTrue, IntStooges.CURLY)
  542. check_pickle_dump_load(self.assertTrue, IntStooges)
  543. def test_pickle_float(self):
  544. if isinstance(FloatStooges, Exception):
  545. raise FloatStooges
  546. check_pickle_dump_load(self.assertTrue, FloatStooges.CURLY)
  547. check_pickle_dump_load(self.assertTrue, FloatStooges)
  548. def test_pickle_enum_function(self):
  549. if isinstance(Answer, Exception):
  550. raise Answer
  551. check_pickle_dump_load(self.assertTrue, Answer.him)
  552. check_pickle_dump_load(self.assertTrue, Answer)
  553. def test_pickle_enum_function_with_module(self):
  554. if isinstance(Question, Exception):
  555. raise Question
  556. check_pickle_dump_load(self.assertTrue, Question.who)
  557. check_pickle_dump_load(self.assertTrue, Question)
  558. if pyver == 3.4:
  559. def test_class_nested_enum_and_pickle_protocol_four(self):
  560. # would normally just have this directly in the class namespace
  561. class NestedEnum(Enum):
  562. twigs = 'common'
  563. shiny = 'rare'
  564. self.__class__.NestedEnum = NestedEnum
  565. self.NestedEnum.__qualname__ = '%s.NestedEnum' % self.__class__.__name__
  566. check_pickle_exception(
  567. self.assertRaises, PicklingError, self.NestedEnum.twigs,
  568. protocol=(0, 3))
  569. check_pickle_dump_load(self.assertTrue, self.NestedEnum.twigs,
  570. protocol=(4, HIGHEST_PROTOCOL))
  571. elif pyver == 3.5:
  572. def test_class_nested_enum_and_pickle_protocol_four(self):
  573. # would normally just have this directly in the class namespace
  574. class NestedEnum(Enum):
  575. twigs = 'common'
  576. shiny = 'rare'
  577. self.__class__.NestedEnum = NestedEnum
  578. self.NestedEnum.__qualname__ = '%s.NestedEnum' % self.__class__.__name__
  579. check_pickle_dump_load(self.assertTrue, self.NestedEnum.twigs,
  580. protocol=(0, HIGHEST_PROTOCOL))
  581. def test_exploding_pickle(self):
  582. BadPickle = Enum('BadPickle', 'dill sweet bread_n_butter')
  583. enum._make_class_unpicklable(BadPickle)
  584. globals()['BadPickle'] = BadPickle
  585. check_pickle_exception(self.assertRaises, TypeError, BadPickle.dill)
  586. check_pickle_exception(self.assertRaises, PicklingError, BadPickle)
  587. def test_string_enum(self):
  588. class SkillLevel(str, Enum):
  589. master = 'what is the sound of one hand clapping?'
  590. journeyman = 'why did the chicken cross the road?'
  591. apprentice = 'knock, knock!'
  592. self.assertEqual(SkillLevel.apprentice, 'knock, knock!')
  593. def test_getattr_getitem(self):
  594. class Period(Enum):
  595. morning = 1
  596. noon = 2
  597. evening = 3
  598. night = 4
  599. self.assertTrue(Period(2) is Period.noon)
  600. self.assertTrue(getattr(Period, 'night') is Period.night)
  601. self.assertTrue(Period['morning'] is Period.morning)
  602. def test_getattr_dunder(self):
  603. Season = self.Season
  604. self.assertTrue(getattr(Season, '__hash__'))
  605. def test_iteration_order(self):
  606. class Season(Enum):
  607. _order_ = 'SUMMER WINTER AUTUMN SPRING'
  608. SUMMER = 2
  609. WINTER = 4
  610. AUTUMN = 3
  611. SPRING = 1
  612. self.assertEqual(
  613. list(Season),
  614. [Season.SUMMER, Season.WINTER, Season.AUTUMN, Season.SPRING],
  615. )
  616. def test_iteration_order_reversed(self):
  617. self.assertEqual(
  618. list(reversed(self.Season)),
  619. [self.Season.WINTER, self.Season.AUTUMN, self.Season.SUMMER,
  620. self.Season.SPRING]
  621. )
  622. def test_iteration_order_with_unorderable_values(self):
  623. class Complex(Enum):
  624. a = complex(7, 9)
  625. b = complex(3.14, 2)
  626. c = complex(1, -1)
  627. d = complex(-77, 32)
  628. self.assertEqual(
  629. list(Complex),
  630. [Complex.a, Complex.b, Complex.c, Complex.d],
  631. )
  632. def test_programatic_function_string(self):
  633. SummerMonth = Enum('SummerMonth', 'june july august')
  634. lst = list(SummerMonth)
  635. self.assertEqual(len(lst), len(SummerMonth))
  636. self.assertEqual(len(SummerMonth), 3, SummerMonth)
  637. self.assertEqual(
  638. [SummerMonth.june, SummerMonth.july, SummerMonth.august],
  639. lst,
  640. )
  641. for i, month in enumerate('june july august'.split()):
  642. i += 1
  643. e = SummerMonth(i)
  644. self.assertEqual(int(e.value), i)
  645. self.assertNotEqual(e, i)
  646. self.assertEqual(e.name, month)
  647. self.assertTrue(e in SummerMonth)
  648. self.assertTrue(type(e) is SummerMonth)
  649. def test_programatic_function_string_with_start(self):
  650. SummerMonth = Enum('SummerMonth', 'june july august', start=10)
  651. lst = list(SummerMonth)
  652. self.assertEqual(len(lst), len(SummerMonth))
  653. self.assertEqual(len(SummerMonth), 3, SummerMonth)
  654. self.assertEqual(
  655. [SummerMonth.june, SummerMonth.july, SummerMonth.august],
  656. lst,
  657. )
  658. for i, month in enumerate('june july august'.split(), 10):
  659. e = SummerMonth(i)
  660. self.assertEqual(int(e.value), i)
  661. self.assertNotEqual(e, i)
  662. self.assertEqual(e.name, month)
  663. self.assertTrue(e in SummerMonth)
  664. self.assertTrue(type(e) is SummerMonth)
  665. def test_programatic_function_string_list(self):
  666. SummerMonth = Enum('SummerMonth', ['june', 'july', 'august'])
  667. lst = list(SummerMonth)
  668. self.assertEqual(len(lst), len(SummerMonth))
  669. self.assertEqual(len(SummerMonth), 3, SummerMonth)
  670. self.assertEqual(
  671. [SummerMonth.june, SummerMonth.july, SummerMonth.august],
  672. lst,
  673. )
  674. for i, month in enumerate('june july august'.split()):
  675. i += 1
  676. e = SummerMonth(i)
  677. self.assertEqual(int(e.value), i)
  678. self.assertNotEqual(e, i)
  679. self.assertEqual(e.name, month)
  680. self.assertTrue(e in SummerMonth)
  681. self.assertTrue(type(e) is SummerMonth)
  682. def test_programatic_function_string_list_with_start(self):
  683. SummerMonth = Enum('SummerMonth', ['june', 'july', 'august'], start=20)
  684. lst = list(SummerMonth)
  685. self.assertEqual(len(lst), len(SummerMonth))
  686. self.assertEqual(len(SummerMonth), 3, SummerMonth)
  687. self.assertEqual(
  688. [SummerMonth.june, SummerMonth.july, SummerMonth.august],
  689. lst,
  690. )
  691. for i, month in enumerate('june july august'.split(), 20):
  692. e = SummerMonth(i)
  693. self.assertEqual(int(e.value), i)
  694. self.assertNotEqual(e, i)
  695. self.assertEqual(e.name, month)
  696. self.assertTrue(e in SummerMonth)
  697. self.assertTrue(type(e) is SummerMonth)
  698. def test_programatic_function_iterable(self):
  699. SummerMonth = Enum(
  700. 'SummerMonth',
  701. (('june', 1), ('july', 2), ('august', 3))
  702. )
  703. lst = list(SummerMonth)
  704. self.assertEqual(len(lst), len(SummerMonth))
  705. self.assertEqual(len(SummerMonth), 3, SummerMonth)
  706. self.assertEqual(
  707. [SummerMonth.june, SummerMonth.july, SummerMonth.august],
  708. lst,
  709. )
  710. for i, month in enumerate('june july august'.split()):
  711. i += 1
  712. e = SummerMonth(i)
  713. self.assertEqual(int(e.value), i)
  714. self.assertNotEqual(e, i)
  715. self.assertEqual(e.name, month)
  716. self.assertTrue(e in SummerMonth)
  717. self.assertTrue(type(e) is SummerMonth)
  718. def test_programatic_function_iterable_with_weird_names(self):
  719. SummerMonth = Enum(
  720. 'SummerMonth',
  721. (('june', 1), ('july', 2), ('august', 3), ('fabulous september', 4))
  722. )
  723. lst = list(SummerMonth)
  724. self.assertEqual(len(lst), len(SummerMonth))
  725. self.assertEqual(len(SummerMonth), 4, SummerMonth)
  726. self.assertEqual(
  727. [SummerMonth.june, SummerMonth.july, SummerMonth.august, SummerMonth['fabulous september']],
  728. lst,
  729. )
  730. for i, month in enumerate('june july august'.split() + ['fabulous september']):
  731. i += 1
  732. e = SummerMonth(i)
  733. self.assertEqual(int(e.value), i)
  734. self.assertNotEqual(e, i)
  735. self.assertEqual(e.name, month)
  736. self.assertTrue(e in SummerMonth)
  737. self.assertTrue(type(e) is SummerMonth)
  738. def test_programatic_function_from_dict(self):
  739. SummerMonth = Enum(
  740. 'SummerMonth',
  741. dict((('june', 1), ('july', 2), ('august', 3)))
  742. )
  743. lst = list(SummerMonth)
  744. self.assertEqual(len(lst), len(SummerMonth))
  745. self.assertEqual(len(SummerMonth), 3, SummerMonth)
  746. if pyver < 3.0:
  747. self.assertEqual(
  748. [SummerMonth.june, SummerMonth.july, SummerMonth.august],
  749. lst,
  750. )
  751. for i, month in enumerate('june july august'.split()):
  752. i += 1
  753. e = SummerMonth(i)
  754. self.assertEqual(int(e.value), i)
  755. self.assertNotEqual(e, i)
  756. self.assertEqual(e.name, month)
  757. self.assertTrue(e in SummerMonth)
  758. self.assertTrue(type(e) is SummerMonth)
  759. def test_programatic_function_type(self):
  760. SummerMonth = Enum('SummerMonth', 'june july august', type=int)
  761. lst = list(SummerMonth)
  762. self.assertEqual(len(lst), len(SummerMonth))
  763. self.assertEqual(len(SummerMonth), 3, SummerMonth)
  764. self.assertEqual(
  765. [SummerMonth.june, SummerMonth.july, SummerMonth.august],
  766. lst,
  767. )
  768. for i, month in enumerate('june july august'.split()):
  769. i += 1
  770. e = SummerMonth(i)
  771. self.assertEqual(e, i)
  772. self.assertEqual(e.name, month)
  773. self.assertTrue(e in SummerMonth)
  774. self.assertTrue(type(e) is SummerMonth)
  775. def test_programatic_function_type_with_start(self):
  776. SummerMonth = Enum('SummerMonth', 'june july august', type=int, start=30)
  777. lst = list(SummerMonth)
  778. self.assertEqual(len(lst), len(SummerMonth))
  779. self.assertEqual(len(SummerMonth), 3, SummerMonth)
  780. self.assertEqual(
  781. [SummerMonth.june, SummerMonth.july, SummerMonth.august],
  782. lst,
  783. )
  784. for i, month in enumerate('june july august'.split(), 30):
  785. e = SummerMonth(i)
  786. self.assertEqual(e, i)
  787. self.assertEqual(e.name, month)
  788. self.assertTrue(e in SummerMonth)
  789. self.assertTrue(type(e) is SummerMonth)
  790. def test_programatic_function_type_from_subclass(self):
  791. SummerMonth = IntEnum('SummerMonth', 'june july august')
  792. lst = list(SummerMonth)
  793. self.assertEqual(len(lst), len(SummerMonth))
  794. self.assertEqual(len(SummerMonth), 3, SummerMonth)
  795. self.assertEqual(
  796. [SummerMonth.june, SummerMonth.july, SummerMonth.august],
  797. lst,
  798. )
  799. for i, month in enumerate('june july august'.split()):
  800. i += 1
  801. e = SummerMonth(i)
  802. self.assertEqual(e, i)
  803. self.assertEqual(e.name, month)
  804. self.assertTrue(e in SummerMonth)
  805. self.assertTrue(type(e) is SummerMonth)
  806. def test_programatic_function_type_from_subclass_with_start(self):
  807. SummerMonth = IntEnum('SummerMonth', 'june july august', start=40)
  808. lst = list(SummerMonth)
  809. self.assertEqual(len(lst), len(SummerMonth))
  810. self.assertEqual(len(SummerMonth), 3, SummerMonth)
  811. self.assertEqual(
  812. [SummerMonth.june, SummerMonth.july, SummerMonth.august],
  813. lst,
  814. )
  815. for i, month in enumerate('june july august'.split(), 40):
  816. e = SummerMonth(i)
  817. self.assertEqual(e, i)
  818. self.assertEqual(e.name, month)
  819. self.assertTrue(e in SummerMonth)
  820. self.assertTrue(type(e) is SummerMonth)
  821. def test_programatic_function_unicode(self):
  822. SummerMonth = Enum('SummerMonth', unicode('june july august'))
  823. lst = list(SummerMonth)
  824. self.assertEqual(len(lst), len(SummerMonth))
  825. self.assertEqual(len(SummerMonth), 3, SummerMonth)
  826. self.assertEqual(
  827. [SummerMonth.june, SummerMonth.july, SummerMonth.august],
  828. lst,
  829. )
  830. for i, month in enumerate(unicode('june july august').split()):
  831. i += 1
  832. e = SummerMonth(i)
  833. self.assertEqual(int(e.value), i)
  834. self.assertNotEqual(e, i)
  835. self.assertEqual(e.name, month)
  836. self.assertTrue(e in SummerMonth)
  837. self.assertTrue(type(e) is SummerMonth)
  838. def test_programatic_function_unicode_list(self):
  839. SummerMonth = Enum('SummerMonth', [unicode('june'), unicode('july'), unicode('august')])
  840. lst = list(SummerMonth)
  841. self.assertEqual(len(lst), len(SummerMonth))
  842. self.assertEqual(len(SummerMonth), 3, SummerMonth)
  843. self.assertEqual(
  844. [SummerMonth.june, SummerMonth.july, SummerMonth.august],
  845. lst,
  846. )
  847. for i, month in enumerate(unicode('june july august').split()):
  848. i += 1
  849. e = SummerMonth(i)
  850. self.assertEqual(int(e.value), i)
  851. self.assertNotEqual(e, i)
  852. self.assertEqual(e.name, month)
  853. self.assertTrue(e in SummerMonth)
  854. self.assertTrue(type(e) is SummerMonth)
  855. def test_programatic_function_unicode_iterable(self):
  856. SummerMonth = Enum(
  857. 'SummerMonth',
  858. ((unicode('june'), 1), (unicode('july'), 2), (unicode('august'), 3))
  859. )
  860. lst = list(SummerMonth)
  861. self.assertEqual(len(lst), len(SummerMonth))
  862. self.assertEqual(len(SummerMonth), 3, SummerMonth)
  863. self.assertEqual(
  864. [SummerMonth.june, SummerMonth.july, SummerMonth.august],
  865. lst,
  866. )
  867. for i, month in enumerate(unicode('june july august').split()):
  868. i += 1
  869. e = SummerMonth(i)
  870. self.assertEqual(int(e.value), i)
  871. self.assertNotEqual(e, i)
  872. self.assertEqual(e.name, month)
  873. self.assertTrue(e in SummerMonth)
  874. self.assertTrue(type(e) is SummerMonth)
  875. def test_programatic_function_from_unicode_dict(self):
  876. SummerMonth = Enum(
  877. 'SummerMonth',
  878. dict(((unicode('june'), 1), (unicode('july'), 2), (unicode('august'), 3)))
  879. )
  880. lst = list(SummerMonth)
  881. self.assertEqual(len(lst), len(SummerMonth))
  882. self.assertEqual(len(SummerMonth), 3, SummerMonth)
  883. if pyver < 3.0:
  884. self.assertEqual(
  885. [SummerMonth.june, SummerMonth.july, SummerMonth.august],
  886. lst,
  887. )
  888. for i, month in enumerate(unicode('june july august').split()):
  889. i += 1
  890. e = SummerMonth(i)
  891. self.assertEqual(int(e.value), i)
  892. self.assertNotEqual(e, i)
  893. self.assertEqual(e.name, month)
  894. self.assertTrue(e in SummerMonth)
  895. self.assertTrue(type(e) is SummerMonth)
  896. def test_programatic_function_unicode_type(self):
  897. SummerMonth = Enum('SummerMonth', unicode('june july august'), type=int)
  898. lst = list(SummerMonth)
  899. self.assertEqual(len(lst), len(SummerMonth))
  900. self.assertEqual(len(SummerMonth), 3, SummerMonth)
  901. self.assertEqual(
  902. [SummerMonth.june, SummerMonth.july, SummerMonth.august],
  903. lst,
  904. )
  905. for i, month in enumerate(unicode('june july august').split()):
  906. i += 1
  907. e = SummerMonth(i)
  908. self.assertEqual(e, i)
  909. self.assertEqual(e.name, month)
  910. self.assertTrue(e in SummerMonth)
  911. self.assertTrue(type(e) is SummerMonth)
  912. def test_programatic_function_unicode_type_from_subclass(self):
  913. SummerMonth = IntEnum('SummerMonth', unicode('june july august'))
  914. lst = list(SummerMonth)
  915. self.assertEqual(len(lst), len(SummerMonth))
  916. self.assertEqual(len(SummerMonth), 3, SummerMonth)
  917. self.assertEqual(
  918. [SummerMonth.june, SummerMonth.july, SummerMonth.august],
  919. lst,
  920. )
  921. for i, month in enumerate(unicode('june july august').split()):
  922. i += 1
  923. e = SummerMonth(i)
  924. self.assertEqual(e, i)
  925. self.assertEqual(e.name, month)
  926. self.assertTrue(e in SummerMonth)
  927. self.assertTrue(type(e) is SummerMonth)
  928. def test_programmatic_function_unicode_class(self):
  929. if pyver < 3.0:
  930. class_names = unicode('SummerMonth'), 'S\xfcmm\xe9rM\xf6nth'.decode('latin1')
  931. else:
  932. class_names = 'SummerMonth', 'S\xfcmm\xe9rM\xf6nth'
  933. for i, class_name in enumerate(class_names):
  934. if pyver < 3.0 and i == 1:
  935. self.assertRaises(TypeError, Enum, class_name, unicode('june july august'))
  936. else:
  937. SummerMonth = Enum(class_name, unicode('june july august'))
  938. lst = list(SummerMonth)
  939. self.assertEqual(len(lst), len(SummerMonth))
  940. self.assertEqual(len(SummerMonth), 3, SummerMonth)
  941. self.assertEqual(
  942. [SummerMonth.june, SummerMonth.july, SummerMonth.august],
  943. lst,
  944. )
  945. for i, month in enumerate(unicode('june july august').split()):
  946. i += 1
  947. e = SummerMonth(i)
  948. self.assertEqual(e.value, i)
  949. self.assertEqual(e.name, month)
  950. self.assertTrue(e in SummerMonth)
  951. self.assertTrue(type(e) is SummerMonth)
  952. def test_subclassing(self):
  953. if isinstance(Name, Exception):
  954. raise Name
  955. self.assertEqual(Name.BDFL, 'Guido van Rossum')
  956. self.assertTrue(Name.BDFL, Name('Guido van Rossum'))
  957. self.assertTrue(Name.BDFL is getattr(Name, 'BDFL'))
  958. check_pickle_dump_load(self.assertTrue, Name.BDFL)
  959. def test_extending(self):
  960. def bad_extension():
  961. class Color(Enum):
  962. red = 1
  963. green = 2
  964. blue = 3
  965. class MoreColor(Color):
  966. cyan = 4
  967. magenta = 5
  968. yellow = 6
  969. self.assertRaises(TypeError, bad_extension)
  970. def test_exclude_methods(self):
  971. class whatever(Enum):
  972. this = 'that'
  973. these = 'those'
  974. def really(self):
  975. return 'no, not %s' % self.value
  976. self.assertFalse(type(whatever.really) is whatever)
  977. self.assertEqual(whatever.this.really(), 'no, not that')
  978. def test_wrong_inheritance_order(self):
  979. def wrong_inherit():
  980. class Wrong(Enum, str):
  981. NotHere = 'error before this point'
  982. self.assertRaises(TypeError, wrong_inherit)
  983. def test_intenum_transitivity(self):
  984. class number(IntEnum):
  985. one = 1
  986. two = 2
  987. three = 3
  988. class numero(IntEnum):
  989. uno = 1
  990. dos = 2
  991. tres = 3
  992. self.assertEqual(number.one, numero.uno)
  993. self.assertEqual(number.two, numero.dos)
  994. self.assertEqual(number.three, numero.tres)
  995. def test_introspection(self):
  996. class Number(IntEnum):
  997. one = 100
  998. two = 200
  999. self.assertTrue(Number.one._member_type_ is int)
  1000. self.assertTrue(Number._member_type_ is int)
  1001. class String(str, Enum):
  1002. yarn = 'soft'
  1003. rope = 'rough'
  1004. wire = 'hard'
  1005. self.assertTrue(String.yarn._member_type_ is str)
  1006. self.assertTrue(String._member_type_ is str)
  1007. class Plain(Enum):
  1008. vanilla = 'white'
  1009. one = 1
  1010. self.assertTrue(Plain.vanilla._member_type_ is object)
  1011. self.assertTrue(Plain._member_type_ is object)
  1012. def test_wrong_enum_in_call(self):
  1013. class Monochrome(Enum):
  1014. black = 0
  1015. white = 1
  1016. class Gender(Enum):
  1017. male = 0
  1018. female = 1
  1019. self.assertRaises(ValueError, Monochrome, Gender.male)
  1020. def test_wrong_enum_in_mixed_call(self):
  1021. class Monochrome(IntEnum):
  1022. black = 0
  1023. white = 1
  1024. class Gender(Enum):
  1025. male = 0
  1026. female = 1
  1027. self.assertRaises(ValueError, Monochrome, Gender.male)
  1028. def test_mixed_enum_in_call_1(self):
  1029. class Monochrome(IntEnum):
  1030. black = 0
  1031. white = 1
  1032. class Gender(IntEnum):
  1033. male = 0
  1034. female = 1
  1035. self.assertTrue(Monochrome(Gender.female) is Monochrome.white)
  1036. def test_mixed_enum_in_call_2(self):
  1037. class Monochrome(Enum):
  1038. black = 0
  1039. white = 1
  1040. class Gender(IntEnum):
  1041. male = 0
  1042. female = 1
  1043. self.assertTrue(Monochrome(Gender.male) is Monochrome.black)
  1044. def test_flufl_enum(self):
  1045. class Fluflnum(Enum):
  1046. def __int__(self):
  1047. return int(self.value)
  1048. class MailManOptions(Fluflnum):
  1049. option1 = 1
  1050. option2 = 2
  1051. option3 = 3
  1052. self.assertEqual(int(MailManOptions.option1), 1)
  1053. def test_no_such_enum_member(self):
  1054. class Color(Enum):
  1055. red = 1
  1056. green = 2
  1057. blue = 3
  1058. self.assertRaises(ValueError, Color, 4)
  1059. self.assertRaises(KeyError, Color.__getitem__, 'chartreuse')
  1060. def test_new_repr(self):
  1061. class Color(Enum):
  1062. red = 1
  1063. green = 2
  1064. blue = 3
  1065. def __repr__(self):
  1066. return "don't you just love shades of %s?" % self.name
  1067. self.assertEqual(
  1068. repr(Color.blue),
  1069. "don't you just love shades of blue?",
  1070. )
  1071. def test_inherited_repr(self):
  1072. class MyEnum(Enum):
  1073. def __repr__(self):
  1074. return "My name is %s." % self.name
  1075. class MyIntEnum(int, MyEnum):
  1076. this = 1
  1077. that = 2
  1078. theother = 3
  1079. self.assertEqual(repr(MyIntEnum.that), "My name is that.")
  1080. def test_multiple_mixin_mro(self):
  1081. class auto_enum(EnumMeta):
  1082. def __new__(metacls, cls, bases, classdict):
  1083. original_dict = classdict
  1084. classdict = enum._EnumDict()
  1085. for k, v in original_dict.items():
  1086. classdict[k] = v
  1087. temp = type(classdict)()
  1088. names = set(classdict._member_names)
  1089. i = 0
  1090. for k in classdict._member_names:
  1091. v = classdict[k]
  1092. if v == ():
  1093. v = i
  1094. else:
  1095. i = v
  1096. i += 1
  1097. temp[k] = v
  1098. for k, v in classdict.items():
  1099. if k not in names:
  1100. temp[k] = v
  1101. return super(auto_enum, metacls).__new__(
  1102. metacls, cls, bases, temp)
  1103. AutoNumberedEnum = auto_enum('AutoNumberedEnum', (Enum,), {})
  1104. AutoIntEnum = auto_enum('AutoIntEnum', (IntEnum,), {})
  1105. class TestAutoNumber(AutoNumberedEnum):
  1106. a = ()
  1107. b = 3
  1108. c = ()
  1109. class TestAutoInt(AutoIntEnum):
  1110. a = ()
  1111. b = 3
  1112. c = ()
  1113. def test_subclasses_with_getnewargs(self):
  1114. class NamedInt(int):
  1115. __qualname__ = 'NamedInt' # needed for pickle protocol 4
  1116. def __new__(cls, *args):
  1117. _args = args
  1118. if len(args) < 1:
  1119. raise TypeError("name and value must be specified")
  1120. name, args = args[0], args[1:]
  1121. self = int.__new__(cls, *args)
  1122. self._intname = name
  1123. self._args = _args
  1124. return self
  1125. def __getnewargs__(self):
  1126. return self._args
  1127. @property
  1128. def __name__(self):
  1129. return self._intname
  1130. def __repr__(self):
  1131. # repr() is updated to include the name and type info
  1132. return "%s(%r, %s)" % (type(self).__name__,
  1133. self.__name__,
  1134. int.__repr__(self))
  1135. def __str__(self):
  1136. # str() is unchanged, even if it relies on the repr() fallback
  1137. base = int
  1138. base_str = base.__str__
  1139. if base_str.__objclass__ is object:
  1140. return base.__repr__(self)
  1141. return base_str(self)
  1142. # for simplicity, we only define one operator that
  1143. # propagates expressions
  1144. def __add__(self, other):
  1145. temp = int(self) + int( other)
  1146. if isinstance(self, NamedInt) and isinstance(other, NamedInt):
  1147. return NamedInt(
  1148. '(%s + %s)' % (self.__name__, other.__name__),
  1149. temp )
  1150. else:
  1151. return temp
  1152. class NEI(NamedInt, Enum):
  1153. __qualname__ = 'NEI' # needed for pickle protocol 4
  1154. x = ('the-x', 1)
  1155. y = ('the-y', 2)
  1156. self.assertTrue(NEI.__new__ is Enum.__new__)
  1157. self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)")
  1158. globals()['NamedInt'] = NamedInt
  1159. globals()['NEI'] = NEI
  1160. NI5 = NamedInt('test', 5)
  1161. self.assertEqual(NI5, 5)
  1162. check_pickle_dump_load(self.assertTrue, NI5, 5)
  1163. self.assertEqual(NEI.y.value, 2)
  1164. check_pickle_dump_load(self.assertTrue, NEI.y)
  1165. if pyver >= 3.4:
  1166. def test_subclasses_with_getnewargs_ex(self):
  1167. class NamedInt(int):
  1168. __qualname__ = 'NamedInt' # needed for pickle protocol 4
  1169. def __new__(cls, *args):
  1170. _args = args
  1171. if len(args) < 2:
  1172. raise TypeError("name and value must be specified")
  1173. name, args = args[0], args[1:]
  1174. self = int.__new__(cls, *args)
  1175. self._intname = name
  1176. self._args = _args
  1177. return self
  1178. def __getnewargs_ex__(self):
  1179. return self._args, {}
  1180. @property
  1181. def __name__(self):
  1182. return self._intname
  1183. def __repr__(self):
  1184. # repr() is updated to include the name and type info
  1185. return "{}({!r}, {})".format(type(self).__name__,
  1186. self.__name__,
  1187. int.__repr__(self))
  1188. def __str__(self):
  1189. # str() is unchanged, even if it relies on the repr() fallback
  1190. base = int
  1191. base_str = base.__str__
  1192. if base_str.__objclass__ is object:
  1193. return base.__repr__(self)
  1194. return base_str(self)
  1195. # for simplicity, we only define one operator that
  1196. # propagates expressions
  1197. def __add__(self, other):
  1198. temp = int(self) + int( other)
  1199. if isinstance(self, NamedInt) and isinstance(other, NamedInt):
  1200. return NamedInt(
  1201. '({0} + {1})'.format(self.__name__, other.__name__),
  1202. temp )
  1203. else:
  1204. return temp
  1205. class NEI(NamedInt, Enum):
  1206. __qualname__ = 'NEI' # needed for pickle protocol 4
  1207. x = ('the-x', 1)
  1208. y = ('the-y', 2)
  1209. self.assertIs(NEI.__new__, Enum.__new__)
  1210. self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)")
  1211. globals()['NamedInt'] = NamedInt
  1212. globals()['NEI'] = NEI
  1213. NI5 = NamedInt('test', 5)
  1214. self.assertEqual(NI5, 5)
  1215. check_pickle_dump_load(self.assertEqual, NI5, 5, protocol=(4, HIGHEST_PROTOCOL))
  1216. self.assertEqual(NEI.y.value, 2)
  1217. check_pickle_dump_load(self.assertTrue, NEI.y, protocol=(4, HIGHEST_PROTOCOL))
  1218. def test_subclasses_with_reduce(self):
  1219. class NamedInt(int):
  1220. __qualname__ = 'NamedInt' # needed for pickle protocol 4
  1221. def __new__(cls, *args):
  1222. _args = args
  1223. if len(args) < 1:
  1224. raise TypeError("name and value must be specified")
  1225. name, args = args[0], args[1:]
  1226. self = int.__new__(cls, *args)
  1227. self._intname = name
  1228. self._args = _args
  1229. return self
  1230. def __reduce__(self):
  1231. return self.__class__, self._args
  1232. @property
  1233. def __name__(self):
  1234. return self._intname
  1235. def __repr__(self):
  1236. # repr() is updated to include the name and type info
  1237. return "%s(%r, %s)" % (type(self).__name__,
  1238. self.__name__,
  1239. int.__repr__(self))
  1240. def __str__(self):
  1241. # str() is unchanged, even if it relies on the repr() fallback
  1242. base = int
  1243. base_str = base.__str__
  1244. if base_str.__objclass__ is object:
  1245. return base.__repr__(self)
  1246. return base_str(self)
  1247. # for simplicity, we only define one operator that
  1248. # propagates expressions
  1249. def __add__(self, other):
  1250. temp = int(self) + int( other)
  1251. if isinstance(self, NamedInt) and isinstance(other, NamedInt):
  1252. return NamedInt(
  1253. '(%s + %s)' % (self.__name__, other.__name__),
  1254. temp )
  1255. else:
  1256. return temp
  1257. class NEI(NamedInt, Enum):
  1258. __qualname__ = 'NEI' # needed for pickle protocol 4
  1259. x = ('the-x', 1)
  1260. y = ('the-y', 2)
  1261. self.assertTrue(NEI.__new__ is Enum.__new__)
  1262. self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)")
  1263. globals()['NamedInt'] = NamedInt
  1264. globals()['NEI'] = NEI
  1265. NI5 = NamedInt('test', 5)
  1266. self.assertEqual(NI5, 5)
  1267. check_pickle_dump_load(self.assertEqual, NI5, 5)
  1268. self.assertEqual(NEI.y.value, 2)
  1269. check_pickle_dump_load(self.assertTrue, NEI.y)
  1270. def test_subclasses_with_reduce_ex(self):
  1271. class NamedInt(int):
  1272. __qualname__ = 'NamedInt' # needed for pickle protocol 4
  1273. def __new__(cls, *args):
  1274. _args = args
  1275. if len(args) < 1:
  1276. raise TypeError("name and value must be specified")
  1277. name, args = args[0], args[1:]
  1278. self = int.__new__(cls, *args)
  1279. self._intname = name
  1280. self._args = _args
  1281. return self
  1282. def __reduce_ex__(self, proto):
  1283. return self.__class__, self._args
  1284. @property
  1285. def __name__(self):
  1286. return self._intname
  1287. def __repr__(self):
  1288. # repr() is updated to include the name and type info
  1289. return "%s(%r, %s)" % (type(self).__name__,
  1290. self.__name__,
  1291. int.__repr__(self))
  1292. def __str__(self):
  1293. # str() is unchanged, even if it relies on the repr() fallback
  1294. base = int
  1295. base_str = base.__str__
  1296. if base_str.__objclass__ is object:
  1297. return base.__repr__(self)
  1298. return base_str(self)
  1299. # for simplicity, we only define one operator that
  1300. # propagates expressions
  1301. def __add__(self, other):
  1302. temp = int(self) + int( other)
  1303. if isinstance(self, NamedInt) and isinstance(other, NamedInt):
  1304. return NamedInt(
  1305. '(%s + %s)' % (self.__name__, other.__name__),
  1306. temp )
  1307. else:
  1308. return temp
  1309. class NEI(NamedInt, Enum):
  1310. __qualname__ = 'NEI' # needed for pickle protocol 4
  1311. x = ('the-x', 1)
  1312. y = ('the-y', 2)
  1313. self.assertTrue(NEI.__new__ is Enum.__new__)
  1314. self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)")
  1315. globals()['NamedInt'] = NamedInt
  1316. globals()['NEI'] = NEI
  1317. NI5 = NamedInt('test', 5)
  1318. self.assertEqual(NI5, 5)
  1319. check_pickle_dump_load(self.assertEqual, NI5, 5)
  1320. self.assertEqual(NEI.y.value, 2)
  1321. check_pickle_dump_load(self.assertTrue, NEI.y)
  1322. def test_subclasses_without_direct_pickle_support(self):
  1323. class NamedInt(int):
  1324. __qualname__ = 'NamedInt'
  1325. def __new__(cls, *args):
  1326. _args = args
  1327. name, args = args[0], args[1:]
  1328. if len(args) == 0:
  1329. raise TypeError("name and value must be specified")
  1330. self = int.__new__(cls, *args)
  1331. self._intname = name
  1332. self._args = _args
  1333. return self
  1334. @property
  1335. def __name__(self):
  1336. return self._intname
  1337. def __repr__(self):
  1338. # repr() is updated to include the name and type info
  1339. return "%s(%r, %s)" % (type(self).__name__,
  1340. self.__name__,
  1341. int.__repr__(self))
  1342. def __str__(self):
  1343. # str() is unchanged, even if it relies on the repr() fallback
  1344. base = int
  1345. base_str = base.__str__
  1346. if base_str.__objclass__ is object:
  1347. return base.__repr__(self)
  1348. return base_str(self)
  1349. # for simplicity, we only define one operator that
  1350. # propagates expressions
  1351. def __add__(self, other):
  1352. temp = int(self) + int( other)
  1353. if isinstance(self, NamedInt) and isinstance(other, NamedInt):
  1354. return NamedInt(
  1355. '(%s + %s)' % (self.__name__, other.__name__),
  1356. temp )
  1357. else:
  1358. return temp
  1359. class NEI(NamedInt, Enum):
  1360. __qualname__ = 'NEI'
  1361. x = ('the-x', 1)
  1362. y = ('the-y', 2)
  1363. self.assertTrue(NEI.__new__ is Enum.__new__)
  1364. self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)")
  1365. globals()['NamedInt'] = NamedInt
  1366. globals()['NEI'] = NEI
  1367. NI5 = NamedInt('test', 5)
  1368. self.assertEqual(NI5, 5)
  1369. self.assertEqual(NEI.y.value, 2)
  1370. check_pickle_exception(self.assertRaises, TypeError, NEI.x)
  1371. check_pickle_exception(self.assertRaises, PicklingError, NEI)
  1372. def test_subclasses_without_direct_pickle_support_using_name(self):
  1373. class NamedInt(int):
  1374. __qualname__ = 'NamedInt'
  1375. def __new__(cls, *args):
  1376. _args = args
  1377. name, args = args[0], args[1:]
  1378. if len(args) == 0:
  1379. raise TypeError("name and value must be specified")
  1380. self = int.__new__(cls, *args)
  1381. self._intname = name
  1382. self._args = _args
  1383. return self
  1384. @property
  1385. def __name__(self):
  1386. return self._intname
  1387. def __repr__(self):
  1388. # repr() is updated to include the name and type info
  1389. return "%s(%r, %s)" % (type(self).__name__,
  1390. self.__name__,
  1391. int.__repr__(self))
  1392. def __str__(self):
  1393. # str() is unchanged, even if it relies on the repr() fallback
  1394. base = int
  1395. base_str = base.__str__
  1396. if base_str.__objclass__ is object:
  1397. return base.__repr__(self)
  1398. return base_str(self)
  1399. # for simplicity, we only define one operator that
  1400. # propagates expressions
  1401. def __add__(self, other):
  1402. temp = int(self) + int( other)
  1403. if isinstance(self, NamedInt) and isinstance(other, NamedInt):
  1404. return NamedInt(
  1405. '(%s + %s)' % (self.__name__, other.__name__),
  1406. temp )
  1407. else:
  1408. return temp
  1409. class NEI(NamedInt, Enum):
  1410. __qualname__ = 'NEI'
  1411. x = ('the-x', 1)
  1412. y = ('the-y', 2)
  1413. def __reduce_ex__(self, proto):
  1414. return getattr, (self.__class__, self._name_)
  1415. self.assertTrue(NEI.__new__ is Enum.__new__)
  1416. self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)")
  1417. globals()['NamedInt'] = NamedInt
  1418. globals()['NEI'] = NEI
  1419. NI5 = NamedInt('test', 5)
  1420. self.assertEqual(NI5, 5)
  1421. self.assertEqual(NEI.y.value, 2)
  1422. check_pickle_dump_load(self.assertTrue, NEI.y)
  1423. check_pickle_dump_load(self.assertTrue, NEI)
  1424. def test_tuple_subclass(self):
  1425. class SomeTuple(tuple, Enum):
  1426. __qualname__ = 'SomeTuple'
  1427. first = (1, 'for the money')
  1428. second = (2, 'for the show')
  1429. third = (3, 'for the music')
  1430. self.assertTrue(type(SomeTuple.first) is SomeTuple)
  1431. self.assertTrue(isinstance(SomeTuple.second, tuple))
  1432. self.assertEqual(SomeTuple.third, (3, 'for the music'))
  1433. globals()['SomeTuple'] = SomeTuple
  1434. check_pickle_dump_load(self.assertTrue, SomeTuple.first)
  1435. def test_duplicate_values_give_unique_enum_items(self):
  1436. class AutoNumber(Enum):
  1437. __order__ = 'enum_m enum_d enum_y'
  1438. enum_m = ()
  1439. enum_d = ()
  1440. enum_y = ()
  1441. def __new__(cls):
  1442. value = len(cls.__members__) + 1
  1443. obj = object.__new__(cls)
  1444. obj._value_ = value
  1445. return obj
  1446. def __int__(self):
  1447. return int(self._value_)
  1448. self.assertEqual(int(AutoNumber.enum_d), 2)
  1449. self.assertEqual(AutoNumber.enum_y.value, 3)
  1450. self.assertTrue(AutoNumber(1) is AutoNumber.enum_m)
  1451. self.assertEqual(
  1452. list(AutoNumber),
  1453. [AutoNumber.enum_m, AutoNumber.enum_d, AutoNumber.enum_y],
  1454. )
  1455. def test_inherited_new_from_enhanced_enum(self):
  1456. class AutoNumber2(Enum):
  1457. def __new__(cls):
  1458. value = len(cls.__members__) + 1
  1459. obj = object.__new__(cls)
  1460. obj._value_ = value
  1461. return obj
  1462. def __int__(self):
  1463. return int(self._value_)
  1464. class Color(AutoNumber2):
  1465. _order_ = 'red green blue'
  1466. red = ()
  1467. green = ()
  1468. blue = ()
  1469. self.assertEqual(len(Color), 3, "wrong number of elements: %d (should be %d)" % (len(Color), 3))
  1470. self.assertEqual(list(Color), [Color.red, Color.green, Color.blue])
  1471. if pyver >= 3.0:
  1472. self.assertEqual(list(map(int, Color)), [1, 2, 3])
  1473. def test_inherited_new_from_mixed_enum(self):
  1474. class AutoNumber3(IntEnum):
  1475. def __new__(cls):
  1476. value = len(cls.__members__) + 1
  1477. obj = int.__new__(cls, value)
  1478. obj._value_ = value
  1479. return obj
  1480. class Color(AutoNumber3):
  1481. red = ()
  1482. green = ()
  1483. blue = ()
  1484. self.assertEqual(len(Color), 3, "wrong number of elements: %d (should be %d)" % (len(Color), 3))
  1485. Color.red
  1486. Color.green
  1487. Color.blue
  1488. def test_equality(self):
  1489. class AlwaysEqual:
  1490. def __eq__(self, other):
  1491. return True
  1492. class OrdinaryEnum(Enum):
  1493. a = 1
  1494. self.assertEqual(AlwaysEqual(), OrdinaryEnum.a)
  1495. self.assertEqual(OrdinaryEnum.a, AlwaysEqual())
  1496. def test_ordered_mixin(self):
  1497. class OrderedEnum(Enum):
  1498. def __ge__(self, other):
  1499. if self.__class__ is other.__class__:
  1500. return self._value_ >= other._value_
  1501. return NotImplemented
  1502. def __gt__(self, other):
  1503. if self.__class__ is other.__class__:
  1504. return self._value_ > other._value_
  1505. return NotImplemented
  1506. def __le__(self, other):
  1507. if self.__class__ is other.__class__:
  1508. return self._value_ <= other._value_
  1509. return NotImplemented
  1510. def __lt__(self, other):
  1511. if self.__class__ is other.__class__:
  1512. return self._value_ < other._value_
  1513. return NotImplemented
  1514. class Grade(OrderedEnum):
  1515. __order__ = 'A B C D F'
  1516. A = 5
  1517. B = 4
  1518. C = 3
  1519. D = 2
  1520. F = 1
  1521. self.assertEqual(list(Grade), [Grade.A, Grade.B, Grade.C, Grade.D, Grade.F])
  1522. self.assertTrue(Grade.A > Grade.B)
  1523. self.assertTrue(Grade.F <= Grade.C)
  1524. self.assertTrue(Grade.D < Grade.A)
  1525. self.assertTrue(Grade.B >= Grade.B)
  1526. def test_extending2(self):
  1527. def bad_extension():
  1528. class Shade(Enum):
  1529. def shade(self):
  1530. print(self.name)
  1531. class Color(Shade):
  1532. red = 1
  1533. green = 2
  1534. blue = 3
  1535. class MoreColor(Color):
  1536. cyan = 4
  1537. magenta = 5
  1538. yellow = 6
  1539. self.assertRaises(TypeError, bad_extension)
  1540. def test_extending3(self):
  1541. class Shade(Enum):
  1542. def shade(self):
  1543. return self.name
  1544. class Color(Shade):
  1545. def hex(self):
  1546. return '%s hexlified!' % self.value
  1547. class MoreColor(Color):
  1548. cyan = 4
  1549. magenta = 5
  1550. yellow = 6
  1551. self.assertEqual(MoreColor.magenta.hex(), '5 hexlified!')
  1552. def test_no_duplicates(self):
  1553. def bad_duplicates():
  1554. class UniqueEnum(Enum):
  1555. def __init__(self, *args):
  1556. cls = self.__class__
  1557. if any(self.value == e.value for e in cls):
  1558. a = self.name
  1559. e = cls(self.value).name
  1560. raise ValueError(
  1561. "aliases not allowed in UniqueEnum: %r --> %r"
  1562. % (a, e)
  1563. )
  1564. class Color(UniqueEnum):
  1565. red = 1
  1566. green = 2
  1567. blue = 3
  1568. class Color(UniqueEnum):
  1569. red = 1
  1570. green = 2
  1571. blue = 3
  1572. grene = 2
  1573. self.assertRaises(ValueError, bad_duplicates)
  1574. def test_init(self):
  1575. class Planet(Enum):
  1576. MERCURY = (3.303e+23, 2.4397e6)
  1577. VENUS = (4.869e+24, 6.0518e6)
  1578. EARTH = (5.976e+24, 6.37814e6)
  1579. MARS = (6.421e+23, 3.3972e6)
  1580. JUPITER = (1.9e+27, 7.1492e7)
  1581. SATURN = (5.688e+26, 6.0268e7)
  1582. URANUS = (8.686e+25, 2.5559e7)
  1583. NEPTUNE = (1.024e+26, 2.4746e7)
  1584. def __init__(self, mass, radius):
  1585. self.mass = mass # in kilograms
  1586. self.radius = radius # in meters
  1587. @property
  1588. def surface_gravity(self):
  1589. # universal gravitational constant (m3 kg-1 s-2)
  1590. G = 6.67300E-11
  1591. return G * self.mass / (self.radius * self.radius)
  1592. self.assertEqual(round(Planet.EARTH.surface_gravity, 2), 9.80)
  1593. self.assertEqual(Planet.EARTH.value, (5.976e+24, 6.37814e6))
  1594. def test_nonhash_value(self):
  1595. class AutoNumberInAList(Enum):
  1596. def __new__(cls):
  1597. value = [len(cls.__members__) + 1]
  1598. obj = object.__new__(cls)
  1599. obj._value_ = value
  1600. return obj
  1601. class ColorInAList(AutoNumberInAList):
  1602. _order_ = 'red green blue'
  1603. red = ()
  1604. green = ()
  1605. blue = ()
  1606. self.assertEqual(list(ColorInAList), [ColorInAList.red, ColorInAList.green, ColorInAList.blue])
  1607. self.assertEqual(ColorInAList.red.value, [1])
  1608. self.assertEqual(ColorInAList([1]), ColorInAList.red)
  1609. def test_conflicting_types_resolved_in_new(self):
  1610. class LabelledIntEnum(int, Enum):
  1611. def __new__(cls, *args):
  1612. value, label = args
  1613. obj = int.__new__(cls, value)
  1614. obj.label = label
  1615. obj._value_ = value
  1616. return obj
  1617. class LabelledList(LabelledIntEnum):
  1618. unprocessed = (1, "Unprocessed")
  1619. payment_complete = (2, "Payment Complete")
  1620. self.assertEqual(list(LabelledList), [LabelledList.unprocessed, LabelledList.payment_complete])
  1621. self.assertEqual(LabelledList.unprocessed, 1)
  1622. self.assertEqual(LabelledList(1), LabelledList.unprocessed)
  1623. def test_empty_with_functional_api(self):
  1624. empty = enum.IntEnum('Foo', {})
  1625. self.assertEqual(len(empty), 0)
  1626. class TestUnique(unittest.TestCase):
  1627. """2.4 doesn't allow class decorators, use function syntax."""
  1628. def test_unique_clean(self):
  1629. class Clean(Enum):
  1630. one = 1
  1631. two = 'dos'
  1632. tres = 4.0
  1633. unique(Clean)
  1634. class Cleaner(IntEnum):
  1635. single = 1
  1636. double = 2
  1637. triple = 3
  1638. unique(Cleaner)
  1639. def test_unique_dirty(self):
  1640. try:
  1641. class Dirty(Enum):
  1642. __order__ = 'one two tres'
  1643. one = 1
  1644. two = 'dos'
  1645. tres = 1
  1646. unique(Dirty)
  1647. except ValueError:
  1648. exc = sys.exc_info()[1]
  1649. message = exc.args[0]
  1650. self.assertTrue('tres -> one' in message)
  1651. try:
  1652. class Dirtier(IntEnum):
  1653. _order_ = 'single double triple turkey'
  1654. single = 1
  1655. double = 1
  1656. triple = 3
  1657. turkey = 3
  1658. unique(Dirtier)
  1659. except ValueError:
  1660. exc = sys.exc_info()[1]
  1661. message = exc.args[0]
  1662. self.assertTrue('double -> single' in message)
  1663. self.assertTrue('turkey -> triple' in message)
  1664. def test_unique_class(self):
  1665. values = [
  1666. ('switzerland', 1),
  1667. ('sweden', 2),
  1668. ('usa', 3),
  1669. ('iran', 4),
  1670. ('iraq', 4),
  1671. ]
  1672. with self.assertRaises(ValueError):
  1673. enum.Enum('Country', values, allow_synonyms=False)
  1674. with self.assertRaises(ValueError):
  1675. class Country(enum.Enum):
  1676. __allow_synonyms__ = False
  1677. austria = 1
  1678. australia = 1
  1679. with self.assertRaises(ValueError):
  1680. class NoDuplicatesAllowed(enum.Enum):
  1681. __allow_synonyms__ = False
  1682. t = NoDuplicatesAllowed('NewEnum', [('russia', 1), ('belorussia', 1)])
  1683. class TestMe(unittest.TestCase):
  1684. pass
  1685. if __name__ == '__main__':
  1686. unittest.main()