Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_enum.py @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | ca909a3728d3 |
| children |
| rev | line source |
|---|---|
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1 import enum |
|
86558
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
2 import inspect |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
3 import pydoc |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
4 import unittest |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
5 from collections import OrderedDict |
|
86558
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
6 from enum import Enum, IntEnum, EnumMeta, unique |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
7 from io import StringIO |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
8 from pickle import dumps, loads, PicklingError |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
9 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
10 # for pickle tests |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
11 try: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
12 class Stooges(Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
13 LARRY = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
14 CURLY = 2 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
15 MOE = 3 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
16 except Exception as exc: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
17 Stooges = exc |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
18 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
19 try: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
20 class IntStooges(int, Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
21 LARRY = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
22 CURLY = 2 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
23 MOE = 3 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
24 except Exception as exc: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
25 IntStooges = exc |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
26 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
27 try: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
28 class FloatStooges(float, Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
29 LARRY = 1.39 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
30 CURLY = 2.72 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
31 MOE = 3.142596 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
32 except Exception as exc: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
33 FloatStooges = exc |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
34 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
35 # for pickle test and subclass tests |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
36 try: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
37 class StrEnum(str, Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
38 'accepts only string values' |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
39 class Name(StrEnum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
40 BDFL = 'Guido van Rossum' |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
41 FLUFL = 'Barry Warsaw' |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
42 except Exception as exc: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
43 Name = exc |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
44 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
45 try: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
46 Question = Enum('Question', 'who what when where why', module=__name__) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
47 except Exception as exc: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
48 Question = exc |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
49 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
50 try: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
51 Answer = Enum('Answer', 'him this then there because') |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
52 except Exception as exc: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
53 Answer = exc |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
54 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
55 # for doctests |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
56 try: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
57 class Fruit(Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
58 tomato = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
59 banana = 2 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
60 cherry = 3 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
61 except Exception: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
62 pass |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
63 |
|
86139
516576f5f9dc
Close #19156: add tests and fix for Enum helper edge cases. Patch from CliffM.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
64 |
|
516576f5f9dc
Close #19156: add tests and fix for Enum helper edge cases. Patch from CliffM.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
65 class TestHelpers(unittest.TestCase): |
|
516576f5f9dc
Close #19156: add tests and fix for Enum helper edge cases. Patch from CliffM.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
66 # _is_descriptor, _is_sunder, _is_dunder |
|
516576f5f9dc
Close #19156: add tests and fix for Enum helper edge cases. Patch from CliffM.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
67 |
|
516576f5f9dc
Close #19156: add tests and fix for Enum helper edge cases. Patch from CliffM.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
68 def test_is_descriptor(self): |
|
516576f5f9dc
Close #19156: add tests and fix for Enum helper edge cases. Patch from CliffM.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
69 class foo: |
|
516576f5f9dc
Close #19156: add tests and fix for Enum helper edge cases. Patch from CliffM.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
70 pass |
|
516576f5f9dc
Close #19156: add tests and fix for Enum helper edge cases. Patch from CliffM.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
71 for attr in ('__get__','__set__','__delete__'): |
|
516576f5f9dc
Close #19156: add tests and fix for Enum helper edge cases. Patch from CliffM.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
72 obj = foo() |
|
516576f5f9dc
Close #19156: add tests and fix for Enum helper edge cases. Patch from CliffM.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
73 self.assertFalse(enum._is_descriptor(obj)) |
|
516576f5f9dc
Close #19156: add tests and fix for Enum helper edge cases. Patch from CliffM.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
74 setattr(obj, attr, 1) |
|
516576f5f9dc
Close #19156: add tests and fix for Enum helper edge cases. Patch from CliffM.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
75 self.assertTrue(enum._is_descriptor(obj)) |
|
516576f5f9dc
Close #19156: add tests and fix for Enum helper edge cases. Patch from CliffM.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
76 |
|
516576f5f9dc
Close #19156: add tests and fix for Enum helper edge cases. Patch from CliffM.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
77 def test_is_sunder(self): |
|
516576f5f9dc
Close #19156: add tests and fix for Enum helper edge cases. Patch from CliffM.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
78 for s in ('_a_', '_aa_'): |
|
516576f5f9dc
Close #19156: add tests and fix for Enum helper edge cases. Patch from CliffM.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
79 self.assertTrue(enum._is_sunder(s)) |
|
516576f5f9dc
Close #19156: add tests and fix for Enum helper edge cases. Patch from CliffM.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
80 |
|
516576f5f9dc
Close #19156: add tests and fix for Enum helper edge cases. Patch from CliffM.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
81 for s in ('a', 'a_', '_a', '__a', 'a__', '__a__', '_a__', '__a_', '_', |
|
516576f5f9dc
Close #19156: add tests and fix for Enum helper edge cases. Patch from CliffM.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
82 '__', '___', '____', '_____',): |
|
516576f5f9dc
Close #19156: add tests and fix for Enum helper edge cases. Patch from CliffM.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
83 self.assertFalse(enum._is_sunder(s)) |
|
516576f5f9dc
Close #19156: add tests and fix for Enum helper edge cases. Patch from CliffM.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
84 |
|
516576f5f9dc
Close #19156: add tests and fix for Enum helper edge cases. Patch from CliffM.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
85 def test_is_dunder(self): |
|
516576f5f9dc
Close #19156: add tests and fix for Enum helper edge cases. Patch from CliffM.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
86 for s in ('__a__', '__aa__'): |
|
516576f5f9dc
Close #19156: add tests and fix for Enum helper edge cases. Patch from CliffM.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
87 self.assertTrue(enum._is_dunder(s)) |
|
516576f5f9dc
Close #19156: add tests and fix for Enum helper edge cases. Patch from CliffM.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
88 for s in ('a', 'a_', '_a', '__a', 'a__', '_a_', '_a__', '__a_', '_', |
|
516576f5f9dc
Close #19156: add tests and fix for Enum helper edge cases. Patch from CliffM.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
89 '__', '___', '____', '_____',): |
|
516576f5f9dc
Close #19156: add tests and fix for Enum helper edge cases. Patch from CliffM.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
90 self.assertFalse(enum._is_dunder(s)) |
|
516576f5f9dc
Close #19156: add tests and fix for Enum helper edge cases. Patch from CliffM.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
91 |
|
516576f5f9dc
Close #19156: add tests and fix for Enum helper edge cases. Patch from CliffM.
Ethan Furman <ethan@stoneleaf.us>
parents:
85790
diff
changeset
|
92 |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
93 class TestEnum(unittest.TestCase): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
94 def setUp(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
95 class Season(Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
96 SPRING = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
97 SUMMER = 2 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
98 AUTUMN = 3 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
99 WINTER = 4 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
100 self.Season = Season |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
101 |
|
85485
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
102 class Konstants(float, Enum): |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
103 E = 2.7182818 |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
104 PI = 3.1415926 |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
105 TAU = 2 * PI |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
106 self.Konstants = Konstants |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
107 |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
108 class Grades(IntEnum): |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
109 A = 5 |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
110 B = 4 |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
111 C = 3 |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
112 D = 2 |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
113 F = 0 |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
114 self.Grades = Grades |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
115 |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
116 class Directional(str, Enum): |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
117 EAST = 'east' |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
118 WEST = 'west' |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
119 NORTH = 'north' |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
120 SOUTH = 'south' |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
121 self.Directional = Directional |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
122 |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
123 from datetime import date |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
124 class Holiday(date, Enum): |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
125 NEW_YEAR = 2013, 1, 1 |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
126 IDES_OF_MARCH = 2013, 3, 15 |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
127 self.Holiday = Holiday |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
128 |
|
85127
39697dcd97e3
Issue 18693: Put custom __dir__ back in place. Will instead look at fixing `help()`.
Ethan Furman <ethan@stoneleaf.us>
parents:
85104
diff
changeset
|
129 def test_dir_on_class(self): |
|
39697dcd97e3
Issue 18693: Put custom __dir__ back in place. Will instead look at fixing `help()`.
Ethan Furman <ethan@stoneleaf.us>
parents:
85104
diff
changeset
|
130 Season = self.Season |
|
39697dcd97e3
Issue 18693: Put custom __dir__ back in place. Will instead look at fixing `help()`.
Ethan Furman <ethan@stoneleaf.us>
parents:
85104
diff
changeset
|
131 self.assertEqual( |
|
39697dcd97e3
Issue 18693: Put custom __dir__ back in place. Will instead look at fixing `help()`.
Ethan Furman <ethan@stoneleaf.us>
parents:
85104
diff
changeset
|
132 set(dir(Season)), |
|
85724
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
133 set(['__class__', '__doc__', '__members__', '__module__', |
|
85127
39697dcd97e3
Issue 18693: Put custom __dir__ back in place. Will instead look at fixing `help()`.
Ethan Furman <ethan@stoneleaf.us>
parents:
85104
diff
changeset
|
134 'SPRING', 'SUMMER', 'AUTUMN', 'WINTER']), |
|
39697dcd97e3
Issue 18693: Put custom __dir__ back in place. Will instead look at fixing `help()`.
Ethan Furman <ethan@stoneleaf.us>
parents:
85104
diff
changeset
|
135 ) |
|
39697dcd97e3
Issue 18693: Put custom __dir__ back in place. Will instead look at fixing `help()`.
Ethan Furman <ethan@stoneleaf.us>
parents:
85104
diff
changeset
|
136 |
|
39697dcd97e3
Issue 18693: Put custom __dir__ back in place. Will instead look at fixing `help()`.
Ethan Furman <ethan@stoneleaf.us>
parents:
85104
diff
changeset
|
137 def test_dir_on_item(self): |
|
39697dcd97e3
Issue 18693: Put custom __dir__ back in place. Will instead look at fixing `help()`.
Ethan Furman <ethan@stoneleaf.us>
parents:
85104
diff
changeset
|
138 Season = self.Season |
|
39697dcd97e3
Issue 18693: Put custom __dir__ back in place. Will instead look at fixing `help()`.
Ethan Furman <ethan@stoneleaf.us>
parents:
85104
diff
changeset
|
139 self.assertEqual( |
|
39697dcd97e3
Issue 18693: Put custom __dir__ back in place. Will instead look at fixing `help()`.
Ethan Furman <ethan@stoneleaf.us>
parents:
85104
diff
changeset
|
140 set(dir(Season.WINTER)), |
|
85724
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
141 set(['__class__', '__doc__', '__module__', 'name', 'value']), |
|
85127
39697dcd97e3
Issue 18693: Put custom __dir__ back in place. Will instead look at fixing `help()`.
Ethan Furman <ethan@stoneleaf.us>
parents:
85104
diff
changeset
|
142 ) |
|
39697dcd97e3
Issue 18693: Put custom __dir__ back in place. Will instead look at fixing `help()`.
Ethan Furman <ethan@stoneleaf.us>
parents:
85104
diff
changeset
|
143 |
|
85724
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
144 def test_dir_with_added_behavior(self): |
|
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
145 class Test(Enum): |
|
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
146 this = 'that' |
|
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
147 these = 'those' |
|
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
148 def wowser(self): |
|
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
149 return ("Wowser! I'm %s!" % self.name) |
|
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
150 self.assertEqual( |
|
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
151 set(dir(Test)), |
|
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
152 set(['__class__', '__doc__', '__members__', '__module__', 'this', 'these']), |
|
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
153 ) |
|
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
154 self.assertEqual( |
|
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
155 set(dir(Test.this)), |
|
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
156 set(['__class__', '__doc__', '__module__', 'name', 'value', 'wowser']), |
|
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
157 ) |
|
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
158 |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
159 def test_enum_in_enum_out(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
160 Season = self.Season |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
161 self.assertIs(Season(Season.WINTER), Season.WINTER) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
162 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
163 def test_enum_value(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
164 Season = self.Season |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
165 self.assertEqual(Season.SPRING.value, 1) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
166 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
167 def test_intenum_value(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
168 self.assertEqual(IntStooges.CURLY.value, 2) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
169 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
170 def test_enum(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
171 Season = self.Season |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
172 lst = list(Season) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
173 self.assertEqual(len(lst), len(Season)) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
174 self.assertEqual(len(Season), 4, Season) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
175 self.assertEqual( |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
176 [Season.SPRING, Season.SUMMER, Season.AUTUMN, Season.WINTER], lst) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
177 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
178 for i, season in enumerate('SPRING SUMMER AUTUMN WINTER'.split(), 1): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
179 e = Season(i) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
180 self.assertEqual(e, getattr(Season, season)) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
181 self.assertEqual(e.value, i) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
182 self.assertNotEqual(e, i) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
183 self.assertEqual(e.name, season) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
184 self.assertIn(e, Season) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
185 self.assertIs(type(e), Season) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
186 self.assertIsInstance(e, Season) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
187 self.assertEqual(str(e), 'Season.' + season) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
188 self.assertEqual( |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
189 repr(e), |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
190 '<Season.{0}: {1}>'.format(season, i), |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
191 ) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
192 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
193 def test_value_name(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
194 Season = self.Season |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
195 self.assertEqual(Season.SPRING.name, 'SPRING') |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
196 self.assertEqual(Season.SPRING.value, 1) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
197 with self.assertRaises(AttributeError): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
198 Season.SPRING.name = 'invierno' |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
199 with self.assertRaises(AttributeError): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
200 Season.SPRING.value = 2 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
201 |
|
85574
1d88d04aade2
Close #18924: Block naive attempts to change an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85485
diff
changeset
|
202 def test_changing_member(self): |
|
1d88d04aade2
Close #18924: Block naive attempts to change an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85485
diff
changeset
|
203 Season = self.Season |
|
1d88d04aade2
Close #18924: Block naive attempts to change an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85485
diff
changeset
|
204 with self.assertRaises(AttributeError): |
|
1d88d04aade2
Close #18924: Block naive attempts to change an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85485
diff
changeset
|
205 Season.WINTER = 'really cold' |
|
1d88d04aade2
Close #18924: Block naive attempts to change an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85485
diff
changeset
|
206 |
|
85790
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
207 def test_attribute_deletion(self): |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
208 class Season(Enum): |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
209 SPRING = 1 |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
210 SUMMER = 2 |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
211 AUTUMN = 3 |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
212 WINTER = 4 |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
213 |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
214 def spam(cls): |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
215 pass |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
216 |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
217 self.assertTrue(hasattr(Season, 'spam')) |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
218 del Season.spam |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
219 self.assertFalse(hasattr(Season, 'spam')) |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
220 |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
221 with self.assertRaises(AttributeError): |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
222 del Season.SPRING |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
223 with self.assertRaises(AttributeError): |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
224 del Season.DRY |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
225 with self.assertRaises(AttributeError): |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
226 del Season.SPRING.name |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
227 |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
228 def test_invalid_names(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
229 with self.assertRaises(ValueError): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
230 class Wrong(Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
231 mro = 9 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
232 with self.assertRaises(ValueError): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
233 class Wrong(Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
234 _create_= 11 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
235 with self.assertRaises(ValueError): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
236 class Wrong(Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
237 _get_mixins_ = 9 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
238 with self.assertRaises(ValueError): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
239 class Wrong(Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
240 _find_new_ = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
241 with self.assertRaises(ValueError): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
242 class Wrong(Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
243 _any_name_ = 9 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
244 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
245 def test_contains(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
246 Season = self.Season |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
247 self.assertIn(Season.AUTUMN, Season) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
248 self.assertNotIn(3, Season) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
249 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
250 val = Season(3) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
251 self.assertIn(val, Season) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
252 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
253 class OtherEnum(Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
254 one = 1; two = 2 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
255 self.assertNotIn(OtherEnum.two, Season) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
256 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
257 def test_comparisons(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
258 Season = self.Season |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
259 with self.assertRaises(TypeError): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
260 Season.SPRING < Season.WINTER |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
261 with self.assertRaises(TypeError): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
262 Season.SPRING > 4 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
263 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
264 self.assertNotEqual(Season.SPRING, 1) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
265 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
266 class Part(Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
267 SPRING = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
268 CLIP = 2 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
269 BARREL = 3 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
270 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
271 self.assertNotEqual(Season.SPRING, Part.SPRING) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
272 with self.assertRaises(TypeError): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
273 Season.SPRING < Part.CLIP |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
274 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
275 def test_enum_duplicates(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
276 class Season(Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
277 SPRING = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
278 SUMMER = 2 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
279 AUTUMN = FALL = 3 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
280 WINTER = 4 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
281 ANOTHER_SPRING = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
282 lst = list(Season) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
283 self.assertEqual( |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
284 lst, |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
285 [Season.SPRING, Season.SUMMER, |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
286 Season.AUTUMN, Season.WINTER, |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
287 ]) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
288 self.assertIs(Season.FALL, Season.AUTUMN) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
289 self.assertEqual(Season.FALL.value, 3) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
290 self.assertEqual(Season.AUTUMN.value, 3) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
291 self.assertIs(Season(3), Season.AUTUMN) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
292 self.assertIs(Season(1), Season.SPRING) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
293 self.assertEqual(Season.FALL.name, 'AUTUMN') |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
294 self.assertEqual( |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
295 [k for k,v in Season.__members__.items() if v.name != k], |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
296 ['FALL', 'ANOTHER_SPRING'], |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
297 ) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
298 |
|
85717
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
299 def test_duplicate_name(self): |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
300 with self.assertRaises(TypeError): |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
301 class Color(Enum): |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
302 red = 1 |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
303 green = 2 |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
304 blue = 3 |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
305 red = 4 |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
306 |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
307 with self.assertRaises(TypeError): |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
308 class Color(Enum): |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
309 red = 1 |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
310 green = 2 |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
311 blue = 3 |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
312 def red(self): |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
313 return 'red' |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
314 |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
315 with self.assertRaises(TypeError): |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
316 class Color(Enum): |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
317 @property |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
318 def red(self): |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
319 return 'redder' |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
320 red = 1 |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
321 green = 2 |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
322 blue = 3 |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
323 |
|
54ddd1124df8
Close #18989: enum members will no longer overwrite other attributes, nor be overwritten by them.
Ethan Furman <ethan@stoneleaf.us>
parents:
85707
diff
changeset
|
324 |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
325 def test_enum_with_value_name(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
326 class Huh(Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
327 name = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
328 value = 2 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
329 self.assertEqual( |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
330 list(Huh), |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
331 [Huh.name, Huh.value], |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
332 ) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
333 self.assertIs(type(Huh.name), Huh) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
334 self.assertEqual(Huh.name.name, 'name') |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
335 self.assertEqual(Huh.name.value, 1) |
|
85485
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
336 |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
337 def test_format_enum(self): |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
338 Season = self.Season |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
339 self.assertEqual('{}'.format(Season.SPRING), |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
340 '{}'.format(str(Season.SPRING))) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
341 self.assertEqual( '{:}'.format(Season.SPRING), |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
342 '{:}'.format(str(Season.SPRING))) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
343 self.assertEqual('{:20}'.format(Season.SPRING), |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
344 '{:20}'.format(str(Season.SPRING))) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
345 self.assertEqual('{:^20}'.format(Season.SPRING), |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
346 '{:^20}'.format(str(Season.SPRING))) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
347 self.assertEqual('{:>20}'.format(Season.SPRING), |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
348 '{:>20}'.format(str(Season.SPRING))) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
349 self.assertEqual('{:<20}'.format(Season.SPRING), |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
350 '{:<20}'.format(str(Season.SPRING))) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
351 |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
352 def test_format_enum_custom(self): |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
353 class TestFloat(float, Enum): |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
354 one = 1.0 |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
355 two = 2.0 |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
356 def __format__(self, spec): |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
357 return 'TestFloat success!' |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
358 self.assertEqual('{}'.format(TestFloat.one), 'TestFloat success!') |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
359 |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
360 def assertFormatIsValue(self, spec, member): |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
361 self.assertEqual(spec.format(member), spec.format(member.value)) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
362 |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
363 def test_format_enum_date(self): |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
364 Holiday = self.Holiday |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
365 self.assertFormatIsValue('{}', Holiday.IDES_OF_MARCH) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
366 self.assertFormatIsValue('{:}', Holiday.IDES_OF_MARCH) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
367 self.assertFormatIsValue('{:20}', Holiday.IDES_OF_MARCH) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
368 self.assertFormatIsValue('{:^20}', Holiday.IDES_OF_MARCH) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
369 self.assertFormatIsValue('{:>20}', Holiday.IDES_OF_MARCH) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
370 self.assertFormatIsValue('{:<20}', Holiday.IDES_OF_MARCH) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
371 self.assertFormatIsValue('{:%Y %m}', Holiday.IDES_OF_MARCH) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
372 self.assertFormatIsValue('{:%Y %m %M:00}', Holiday.IDES_OF_MARCH) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
373 |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
374 def test_format_enum_float(self): |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
375 Konstants = self.Konstants |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
376 self.assertFormatIsValue('{}', Konstants.TAU) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
377 self.assertFormatIsValue('{:}', Konstants.TAU) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
378 self.assertFormatIsValue('{:20}', Konstants.TAU) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
379 self.assertFormatIsValue('{:^20}', Konstants.TAU) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
380 self.assertFormatIsValue('{:>20}', Konstants.TAU) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
381 self.assertFormatIsValue('{:<20}', Konstants.TAU) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
382 self.assertFormatIsValue('{:n}', Konstants.TAU) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
383 self.assertFormatIsValue('{:5.2}', Konstants.TAU) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
384 self.assertFormatIsValue('{:f}', Konstants.TAU) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
385 |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
386 def test_format_enum_int(self): |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
387 Grades = self.Grades |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
388 self.assertFormatIsValue('{}', Grades.C) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
389 self.assertFormatIsValue('{:}', Grades.C) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
390 self.assertFormatIsValue('{:20}', Grades.C) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
391 self.assertFormatIsValue('{:^20}', Grades.C) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
392 self.assertFormatIsValue('{:>20}', Grades.C) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
393 self.assertFormatIsValue('{:<20}', Grades.C) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
394 self.assertFormatIsValue('{:+}', Grades.C) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
395 self.assertFormatIsValue('{:08X}', Grades.C) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
396 self.assertFormatIsValue('{:b}', Grades.C) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
397 |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
398 def test_format_enum_str(self): |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
399 Directional = self.Directional |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
400 self.assertFormatIsValue('{}', Directional.WEST) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
401 self.assertFormatIsValue('{:}', Directional.WEST) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
402 self.assertFormatIsValue('{:20}', Directional.WEST) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
403 self.assertFormatIsValue('{:^20}', Directional.WEST) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
404 self.assertFormatIsValue('{:>20}', Directional.WEST) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
405 self.assertFormatIsValue('{:<20}', Directional.WEST) |
|
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
406 |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
407 def test_hash(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
408 Season = self.Season |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
409 dates = {} |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
410 dates[Season.WINTER] = '1225' |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
411 dates[Season.SPRING] = '0315' |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
412 dates[Season.SUMMER] = '0704' |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
413 dates[Season.AUTUMN] = '1031' |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
414 self.assertEqual(dates[Season.AUTUMN], '1031') |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
415 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
416 def test_intenum_from_scratch(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
417 class phy(int, Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
418 pi = 3 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
419 tau = 2 * pi |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
420 self.assertTrue(phy.pi < phy.tau) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
421 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
422 def test_intenum_inherited(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
423 class IntEnum(int, Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
424 pass |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
425 class phy(IntEnum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
426 pi = 3 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
427 tau = 2 * pi |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
428 self.assertTrue(phy.pi < phy.tau) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
429 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
430 def test_floatenum_from_scratch(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
431 class phy(float, Enum): |
|
85485
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
432 pi = 3.1415926 |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
433 tau = 2 * pi |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
434 self.assertTrue(phy.pi < phy.tau) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
435 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
436 def test_floatenum_inherited(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
437 class FloatEnum(float, Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
438 pass |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
439 class phy(FloatEnum): |
|
85485
058cb219b3b5
Close #18738: Route __format__ calls to mixed-in type for mixed Enums (such as IntEnum).
Ethan Furman <ethan@stoneleaf.us>
parents:
85127
diff
changeset
|
440 pi = 3.1415926 |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
441 tau = 2 * pi |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
442 self.assertTrue(phy.pi < phy.tau) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
443 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
444 def test_strenum_from_scratch(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
445 class phy(str, Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
446 pi = 'Pi' |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
447 tau = 'Tau' |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
448 self.assertTrue(phy.pi < phy.tau) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
449 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
450 def test_strenum_inherited(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
451 class StrEnum(str, Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
452 pass |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
453 class phy(StrEnum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
454 pi = 'Pi' |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
455 tau = 'Tau' |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
456 self.assertTrue(phy.pi < phy.tau) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
457 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
458 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
459 def test_intenum(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
460 class WeekDay(IntEnum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
461 SUNDAY = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
462 MONDAY = 2 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
463 TUESDAY = 3 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
464 WEDNESDAY = 4 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
465 THURSDAY = 5 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
466 FRIDAY = 6 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
467 SATURDAY = 7 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
468 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
469 self.assertEqual(['a', 'b', 'c'][WeekDay.MONDAY], 'c') |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
470 self.assertEqual([i for i in range(WeekDay.TUESDAY)], [0, 1, 2]) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
471 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
472 lst = list(WeekDay) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
473 self.assertEqual(len(lst), len(WeekDay)) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
474 self.assertEqual(len(WeekDay), 7) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
475 target = 'SUNDAY MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY' |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
476 target = target.split() |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
477 for i, weekday in enumerate(target, 1): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
478 e = WeekDay(i) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
479 self.assertEqual(e, i) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
480 self.assertEqual(int(e), i) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
481 self.assertEqual(e.name, weekday) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
482 self.assertIn(e, WeekDay) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
483 self.assertEqual(lst.index(e)+1, i) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
484 self.assertTrue(0 < e < 8) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
485 self.assertIs(type(e), WeekDay) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
486 self.assertIsInstance(e, int) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
487 self.assertIsInstance(e, Enum) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
488 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
489 def test_intenum_duplicates(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
490 class WeekDay(IntEnum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
491 SUNDAY = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
492 MONDAY = 2 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
493 TUESDAY = TEUSDAY = 3 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
494 WEDNESDAY = 4 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
495 THURSDAY = 5 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
496 FRIDAY = 6 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
497 SATURDAY = 7 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
498 self.assertIs(WeekDay.TEUSDAY, WeekDay.TUESDAY) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
499 self.assertEqual(WeekDay(3).name, 'TUESDAY') |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
500 self.assertEqual([k for k,v in WeekDay.__members__.items() |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
501 if v.name != k], ['TEUSDAY', ]) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
502 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
503 def test_pickle_enum(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
504 if isinstance(Stooges, Exception): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
505 raise Stooges |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
506 self.assertIs(Stooges.CURLY, loads(dumps(Stooges.CURLY))) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
507 self.assertIs(Stooges, loads(dumps(Stooges))) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
508 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
509 def test_pickle_int(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
510 if isinstance(IntStooges, Exception): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
511 raise IntStooges |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
512 self.assertIs(IntStooges.CURLY, loads(dumps(IntStooges.CURLY))) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
513 self.assertIs(IntStooges, loads(dumps(IntStooges))) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
514 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
515 def test_pickle_float(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
516 if isinstance(FloatStooges, Exception): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
517 raise FloatStooges |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
518 self.assertIs(FloatStooges.CURLY, loads(dumps(FloatStooges.CURLY))) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
519 self.assertIs(FloatStooges, loads(dumps(FloatStooges))) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
520 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
521 def test_pickle_enum_function(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
522 if isinstance(Answer, Exception): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
523 raise Answer |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
524 self.assertIs(Answer.him, loads(dumps(Answer.him))) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
525 self.assertIs(Answer, loads(dumps(Answer))) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
526 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
527 def test_pickle_enum_function_with_module(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
528 if isinstance(Question, Exception): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
529 raise Question |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
530 self.assertIs(Question.who, loads(dumps(Question.who))) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
531 self.assertIs(Question, loads(dumps(Question))) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
532 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
533 def test_exploding_pickle(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
534 BadPickle = Enum('BadPickle', 'dill sweet bread-n-butter') |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
535 enum._make_class_unpicklable(BadPickle) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
536 globals()['BadPickle'] = BadPickle |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
537 with self.assertRaises(TypeError): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
538 dumps(BadPickle.dill) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
539 with self.assertRaises(PicklingError): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
540 dumps(BadPickle) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
541 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
542 def test_string_enum(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
543 class SkillLevel(str, Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
544 master = 'what is the sound of one hand clapping?' |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
545 journeyman = 'why did the chicken cross the road?' |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
546 apprentice = 'knock, knock!' |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
547 self.assertEqual(SkillLevel.apprentice, 'knock, knock!') |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
548 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
549 def test_getattr_getitem(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
550 class Period(Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
551 morning = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
552 noon = 2 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
553 evening = 3 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
554 night = 4 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
555 self.assertIs(Period(2), Period.noon) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
556 self.assertIs(getattr(Period, 'night'), Period.night) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
557 self.assertIs(Period['morning'], Period.morning) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
558 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
559 def test_getattr_dunder(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
560 Season = self.Season |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
561 self.assertTrue(getattr(Season, '__eq__')) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
562 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
563 def test_iteration_order(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
564 class Season(Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
565 SUMMER = 2 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
566 WINTER = 4 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
567 AUTUMN = 3 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
568 SPRING = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
569 self.assertEqual( |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
570 list(Season), |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
571 [Season.SUMMER, Season.WINTER, Season.AUTUMN, Season.SPRING], |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
572 ) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
573 |
|
85707
24aa6f98fe25
Add __reversed__ to Enum. Minor code reorg (moved __members__ to be in alpha order).
Ethan Furman <ethan@stoneleaf.us>
parents:
85574
diff
changeset
|
574 def test_reversed_iteration_order(self): |
|
24aa6f98fe25
Add __reversed__ to Enum. Minor code reorg (moved __members__ to be in alpha order).
Ethan Furman <ethan@stoneleaf.us>
parents:
85574
diff
changeset
|
575 self.assertEqual( |
|
24aa6f98fe25
Add __reversed__ to Enum. Minor code reorg (moved __members__ to be in alpha order).
Ethan Furman <ethan@stoneleaf.us>
parents:
85574
diff
changeset
|
576 list(reversed(self.Season)), |
|
24aa6f98fe25
Add __reversed__ to Enum. Minor code reorg (moved __members__ to be in alpha order).
Ethan Furman <ethan@stoneleaf.us>
parents:
85574
diff
changeset
|
577 [self.Season.WINTER, self.Season.AUTUMN, self.Season.SUMMER, |
|
24aa6f98fe25
Add __reversed__ to Enum. Minor code reorg (moved __members__ to be in alpha order).
Ethan Furman <ethan@stoneleaf.us>
parents:
85574
diff
changeset
|
578 self.Season.SPRING] |
|
24aa6f98fe25
Add __reversed__ to Enum. Minor code reorg (moved __members__ to be in alpha order).
Ethan Furman <ethan@stoneleaf.us>
parents:
85574
diff
changeset
|
579 ) |
|
24aa6f98fe25
Add __reversed__ to Enum. Minor code reorg (moved __members__ to be in alpha order).
Ethan Furman <ethan@stoneleaf.us>
parents:
85574
diff
changeset
|
580 |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
581 def test_programatic_function_string(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
582 SummerMonth = Enum('SummerMonth', 'june july august') |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
583 lst = list(SummerMonth) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
584 self.assertEqual(len(lst), len(SummerMonth)) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
585 self.assertEqual(len(SummerMonth), 3, SummerMonth) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
586 self.assertEqual( |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
587 [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
588 lst, |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
589 ) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
590 for i, month in enumerate('june july august'.split(), 1): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
591 e = SummerMonth(i) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
592 self.assertEqual(int(e.value), i) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
593 self.assertNotEqual(e, i) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
594 self.assertEqual(e.name, month) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
595 self.assertIn(e, SummerMonth) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
596 self.assertIs(type(e), SummerMonth) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
597 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
598 def test_programatic_function_string_list(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
599 SummerMonth = Enum('SummerMonth', ['june', 'july', 'august']) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
600 lst = list(SummerMonth) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
601 self.assertEqual(len(lst), len(SummerMonth)) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
602 self.assertEqual(len(SummerMonth), 3, SummerMonth) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
603 self.assertEqual( |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
604 [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
605 lst, |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
606 ) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
607 for i, month in enumerate('june july august'.split(), 1): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
608 e = SummerMonth(i) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
609 self.assertEqual(int(e.value), i) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
610 self.assertNotEqual(e, i) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
611 self.assertEqual(e.name, month) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
612 self.assertIn(e, SummerMonth) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
613 self.assertIs(type(e), SummerMonth) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
614 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
615 def test_programatic_function_iterable(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
616 SummerMonth = Enum( |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
617 'SummerMonth', |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
618 (('june', 1), ('july', 2), ('august', 3)) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
619 ) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
620 lst = list(SummerMonth) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
621 self.assertEqual(len(lst), len(SummerMonth)) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
622 self.assertEqual(len(SummerMonth), 3, SummerMonth) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
623 self.assertEqual( |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
624 [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
625 lst, |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
626 ) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
627 for i, month in enumerate('june july august'.split(), 1): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
628 e = SummerMonth(i) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
629 self.assertEqual(int(e.value), i) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
630 self.assertNotEqual(e, i) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
631 self.assertEqual(e.name, month) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
632 self.assertIn(e, SummerMonth) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
633 self.assertIs(type(e), SummerMonth) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
634 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
635 def test_programatic_function_from_dict(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
636 SummerMonth = Enum( |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
637 'SummerMonth', |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
638 OrderedDict((('june', 1), ('july', 2), ('august', 3))) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
639 ) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
640 lst = list(SummerMonth) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
641 self.assertEqual(len(lst), len(SummerMonth)) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
642 self.assertEqual(len(SummerMonth), 3, SummerMonth) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
643 self.assertEqual( |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
644 [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
645 lst, |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
646 ) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
647 for i, month in enumerate('june july august'.split(), 1): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
648 e = SummerMonth(i) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
649 self.assertEqual(int(e.value), i) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
650 self.assertNotEqual(e, i) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
651 self.assertEqual(e.name, month) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
652 self.assertIn(e, SummerMonth) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
653 self.assertIs(type(e), SummerMonth) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
654 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
655 def test_programatic_function_type(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
656 SummerMonth = Enum('SummerMonth', 'june july august', type=int) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
657 lst = list(SummerMonth) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
658 self.assertEqual(len(lst), len(SummerMonth)) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
659 self.assertEqual(len(SummerMonth), 3, SummerMonth) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
660 self.assertEqual( |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
661 [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
662 lst, |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
663 ) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
664 for i, month in enumerate('june july august'.split(), 1): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
665 e = SummerMonth(i) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
666 self.assertEqual(e, i) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
667 self.assertEqual(e.name, month) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
668 self.assertIn(e, SummerMonth) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
669 self.assertIs(type(e), SummerMonth) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
670 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
671 def test_programatic_function_type_from_subclass(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
672 SummerMonth = IntEnum('SummerMonth', 'june july august') |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
673 lst = list(SummerMonth) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
674 self.assertEqual(len(lst), len(SummerMonth)) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
675 self.assertEqual(len(SummerMonth), 3, SummerMonth) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
676 self.assertEqual( |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
677 [SummerMonth.june, SummerMonth.july, SummerMonth.august], |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
678 lst, |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
679 ) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
680 for i, month in enumerate('june july august'.split(), 1): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
681 e = SummerMonth(i) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
682 self.assertEqual(e, i) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
683 self.assertEqual(e.name, month) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
684 self.assertIn(e, SummerMonth) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
685 self.assertIs(type(e), SummerMonth) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
686 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
687 def test_subclassing(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
688 if isinstance(Name, Exception): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
689 raise Name |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
690 self.assertEqual(Name.BDFL, 'Guido van Rossum') |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
691 self.assertTrue(Name.BDFL, Name('Guido van Rossum')) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
692 self.assertIs(Name.BDFL, getattr(Name, 'BDFL')) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
693 self.assertIs(Name.BDFL, loads(dumps(Name.BDFL))) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
694 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
695 def test_extending(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
696 class Color(Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
697 red = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
698 green = 2 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
699 blue = 3 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
700 with self.assertRaises(TypeError): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
701 class MoreColor(Color): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
702 cyan = 4 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
703 magenta = 5 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
704 yellow = 6 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
705 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
706 def test_exclude_methods(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
707 class whatever(Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
708 this = 'that' |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
709 these = 'those' |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
710 def really(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
711 return 'no, not %s' % self.value |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
712 self.assertIsNot(type(whatever.really), whatever) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
713 self.assertEqual(whatever.this.really(), 'no, not that') |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
714 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
715 def test_wrong_inheritance_order(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
716 with self.assertRaises(TypeError): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
717 class Wrong(Enum, str): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
718 NotHere = 'error before this point' |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
719 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
720 def test_intenum_transitivity(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
721 class number(IntEnum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
722 one = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
723 two = 2 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
724 three = 3 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
725 class numero(IntEnum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
726 uno = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
727 dos = 2 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
728 tres = 3 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
729 self.assertEqual(number.one, numero.uno) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
730 self.assertEqual(number.two, numero.dos) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
731 self.assertEqual(number.three, numero.tres) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
732 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
733 def test_wrong_enum_in_call(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
734 class Monochrome(Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
735 black = 0 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
736 white = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
737 class Gender(Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
738 male = 0 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
739 female = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
740 self.assertRaises(ValueError, Monochrome, Gender.male) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
741 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
742 def test_wrong_enum_in_mixed_call(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
743 class Monochrome(IntEnum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
744 black = 0 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
745 white = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
746 class Gender(Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
747 male = 0 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
748 female = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
749 self.assertRaises(ValueError, Monochrome, Gender.male) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
750 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
751 def test_mixed_enum_in_call_1(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
752 class Monochrome(IntEnum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
753 black = 0 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
754 white = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
755 class Gender(IntEnum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
756 male = 0 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
757 female = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
758 self.assertIs(Monochrome(Gender.female), Monochrome.white) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
759 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
760 def test_mixed_enum_in_call_2(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
761 class Monochrome(Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
762 black = 0 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
763 white = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
764 class Gender(IntEnum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
765 male = 0 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
766 female = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
767 self.assertIs(Monochrome(Gender.male), Monochrome.black) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
768 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
769 def test_flufl_enum(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
770 class Fluflnum(Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
771 def __int__(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
772 return int(self.value) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
773 class MailManOptions(Fluflnum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
774 option1 = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
775 option2 = 2 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
776 option3 = 3 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
777 self.assertEqual(int(MailManOptions.option1), 1) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
778 |
|
85021
b034418e840b
Close #18635: Move class level private attribute from instance to class.
Ethan Furman <ethan@stoneleaf.us>
parents:
84826
diff
changeset
|
779 def test_introspection(self): |
|
b034418e840b
Close #18635: Move class level private attribute from instance to class.
Ethan Furman <ethan@stoneleaf.us>
parents:
84826
diff
changeset
|
780 class Number(IntEnum): |
|
b034418e840b
Close #18635: Move class level private attribute from instance to class.
Ethan Furman <ethan@stoneleaf.us>
parents:
84826
diff
changeset
|
781 one = 100 |
|
b034418e840b
Close #18635: Move class level private attribute from instance to class.
Ethan Furman <ethan@stoneleaf.us>
parents:
84826
diff
changeset
|
782 two = 200 |
|
b034418e840b
Close #18635: Move class level private attribute from instance to class.
Ethan Furman <ethan@stoneleaf.us>
parents:
84826
diff
changeset
|
783 self.assertIs(Number.one._member_type_, int) |
|
b034418e840b
Close #18635: Move class level private attribute from instance to class.
Ethan Furman <ethan@stoneleaf.us>
parents:
84826
diff
changeset
|
784 self.assertIs(Number._member_type_, int) |
|
b034418e840b
Close #18635: Move class level private attribute from instance to class.
Ethan Furman <ethan@stoneleaf.us>
parents:
84826
diff
changeset
|
785 class String(str, Enum): |
|
b034418e840b
Close #18635: Move class level private attribute from instance to class.
Ethan Furman <ethan@stoneleaf.us>
parents:
84826
diff
changeset
|
786 yarn = 'soft' |
|
b034418e840b
Close #18635: Move class level private attribute from instance to class.
Ethan Furman <ethan@stoneleaf.us>
parents:
84826
diff
changeset
|
787 rope = 'rough' |
|
b034418e840b
Close #18635: Move class level private attribute from instance to class.
Ethan Furman <ethan@stoneleaf.us>
parents:
84826
diff
changeset
|
788 wire = 'hard' |
|
b034418e840b
Close #18635: Move class level private attribute from instance to class.
Ethan Furman <ethan@stoneleaf.us>
parents:
84826
diff
changeset
|
789 self.assertIs(String.yarn._member_type_, str) |
|
b034418e840b
Close #18635: Move class level private attribute from instance to class.
Ethan Furman <ethan@stoneleaf.us>
parents:
84826
diff
changeset
|
790 self.assertIs(String._member_type_, str) |
|
b034418e840b
Close #18635: Move class level private attribute from instance to class.
Ethan Furman <ethan@stoneleaf.us>
parents:
84826
diff
changeset
|
791 class Plain(Enum): |
|
b034418e840b
Close #18635: Move class level private attribute from instance to class.
Ethan Furman <ethan@stoneleaf.us>
parents:
84826
diff
changeset
|
792 vanilla = 'white' |
|
b034418e840b
Close #18635: Move class level private attribute from instance to class.
Ethan Furman <ethan@stoneleaf.us>
parents:
84826
diff
changeset
|
793 one = 1 |
|
b034418e840b
Close #18635: Move class level private attribute from instance to class.
Ethan Furman <ethan@stoneleaf.us>
parents:
84826
diff
changeset
|
794 self.assertIs(Plain.vanilla._member_type_, object) |
|
b034418e840b
Close #18635: Move class level private attribute from instance to class.
Ethan Furman <ethan@stoneleaf.us>
parents:
84826
diff
changeset
|
795 self.assertIs(Plain._member_type_, object) |
|
b034418e840b
Close #18635: Move class level private attribute from instance to class.
Ethan Furman <ethan@stoneleaf.us>
parents:
84826
diff
changeset
|
796 |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
797 def test_no_such_enum_member(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
798 class Color(Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
799 red = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
800 green = 2 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
801 blue = 3 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
802 with self.assertRaises(ValueError): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
803 Color(4) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
804 with self.assertRaises(KeyError): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
805 Color['chartreuse'] |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
806 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
807 def test_new_repr(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
808 class Color(Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
809 red = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
810 green = 2 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
811 blue = 3 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
812 def __repr__(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
813 return "don't you just love shades of %s?" % self.name |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
814 self.assertEqual( |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
815 repr(Color.blue), |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
816 "don't you just love shades of blue?", |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
817 ) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
818 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
819 def test_inherited_repr(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
820 class MyEnum(Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
821 def __repr__(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
822 return "My name is %s." % self.name |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
823 class MyIntEnum(int, MyEnum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
824 this = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
825 that = 2 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
826 theother = 3 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
827 self.assertEqual(repr(MyIntEnum.that), "My name is that.") |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
828 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
829 def test_multiple_mixin_mro(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
830 class auto_enum(type(Enum)): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
831 def __new__(metacls, cls, bases, classdict): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
832 temp = type(classdict)() |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
833 names = set(classdict._member_names) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
834 i = 0 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
835 for k in classdict._member_names: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
836 v = classdict[k] |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
837 if v is Ellipsis: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
838 v = i |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
839 else: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
840 i = v |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
841 i += 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
842 temp[k] = v |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
843 for k, v in classdict.items(): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
844 if k not in names: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
845 temp[k] = v |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
846 return super(auto_enum, metacls).__new__( |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
847 metacls, cls, bases, temp) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
848 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
849 class AutoNumberedEnum(Enum, metaclass=auto_enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
850 pass |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
851 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
852 class AutoIntEnum(IntEnum, metaclass=auto_enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
853 pass |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
854 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
855 class TestAutoNumber(AutoNumberedEnum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
856 a = ... |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
857 b = 3 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
858 c = ... |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
859 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
860 class TestAutoInt(AutoIntEnum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
861 a = ... |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
862 b = 3 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
863 c = ... |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
864 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
865 def test_subclasses_with_getnewargs(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
866 class NamedInt(int): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
867 def __new__(cls, *args): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
868 _args = args |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
869 name, *args = args |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
870 if len(args) == 0: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
871 raise TypeError("name and value must be specified") |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
872 self = int.__new__(cls, *args) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
873 self._intname = name |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
874 self._args = _args |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
875 return self |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
876 def __getnewargs__(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
877 return self._args |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
878 @property |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
879 def __name__(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
880 return self._intname |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
881 def __repr__(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
882 # repr() is updated to include the name and type info |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
883 return "{}({!r}, {})".format(type(self).__name__, |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
884 self.__name__, |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
885 int.__repr__(self)) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
886 def __str__(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
887 # str() is unchanged, even if it relies on the repr() fallback |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
888 base = int |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
889 base_str = base.__str__ |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
890 if base_str.__objclass__ is object: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
891 return base.__repr__(self) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
892 return base_str(self) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
893 # for simplicity, we only define one operator that |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
894 # propagates expressions |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
895 def __add__(self, other): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
896 temp = int(self) + int( other) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
897 if isinstance(self, NamedInt) and isinstance(other, NamedInt): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
898 return NamedInt( |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
899 '({0} + {1})'.format(self.__name__, other.__name__), |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
900 temp ) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
901 else: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
902 return temp |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
903 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
904 class NEI(NamedInt, Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
905 x = ('the-x', 1) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
906 y = ('the-y', 2) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
907 |
|
84720
37c427e3eb94
Close #18508 -- fix _value2member_map to always have the member's value
Ethan Furman <ethan@stoneleaf.us>
parents:
84713
diff
changeset
|
908 |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
909 self.assertIs(NEI.__new__, Enum.__new__) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
910 self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)") |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
911 globals()['NamedInt'] = NamedInt |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
912 globals()['NEI'] = NEI |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
913 NI5 = NamedInt('test', 5) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
914 self.assertEqual(NI5, 5) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
915 self.assertEqual(loads(dumps(NI5)), 5) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
916 self.assertEqual(NEI.y.value, 2) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
917 self.assertIs(loads(dumps(NEI.y)), NEI.y) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
918 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
919 def test_subclasses_without_getnewargs(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
920 class NamedInt(int): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
921 def __new__(cls, *args): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
922 _args = args |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
923 name, *args = args |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
924 if len(args) == 0: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
925 raise TypeError("name and value must be specified") |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
926 self = int.__new__(cls, *args) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
927 self._intname = name |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
928 self._args = _args |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
929 return self |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
930 @property |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
931 def __name__(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
932 return self._intname |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
933 def __repr__(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
934 # repr() is updated to include the name and type info |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
935 return "{}({!r}, {})".format(type(self).__name__, |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
936 self.__name__, |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
937 int.__repr__(self)) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
938 def __str__(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
939 # str() is unchanged, even if it relies on the repr() fallback |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
940 base = int |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
941 base_str = base.__str__ |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
942 if base_str.__objclass__ is object: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
943 return base.__repr__(self) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
944 return base_str(self) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
945 # for simplicity, we only define one operator that |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
946 # propagates expressions |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
947 def __add__(self, other): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
948 temp = int(self) + int( other) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
949 if isinstance(self, NamedInt) and isinstance(other, NamedInt): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
950 return NamedInt( |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
951 '({0} + {1})'.format(self.__name__, other.__name__), |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
952 temp ) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
953 else: |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
954 return temp |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
955 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
956 class NEI(NamedInt, Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
957 x = ('the-x', 1) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
958 y = ('the-y', 2) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
959 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
960 self.assertIs(NEI.__new__, Enum.__new__) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
961 self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)") |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
962 globals()['NamedInt'] = NamedInt |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
963 globals()['NEI'] = NEI |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
964 NI5 = NamedInt('test', 5) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
965 self.assertEqual(NI5, 5) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
966 self.assertEqual(NEI.y.value, 2) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
967 with self.assertRaises(TypeError): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
968 dumps(NEI.x) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
969 with self.assertRaises(PicklingError): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
970 dumps(NEI) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
971 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
972 def test_tuple_subclass(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
973 class SomeTuple(tuple, Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
974 first = (1, 'for the money') |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
975 second = (2, 'for the show') |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
976 third = (3, 'for the music') |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
977 self.assertIs(type(SomeTuple.first), SomeTuple) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
978 self.assertIsInstance(SomeTuple.second, tuple) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
979 self.assertEqual(SomeTuple.third, (3, 'for the music')) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
980 globals()['SomeTuple'] = SomeTuple |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
981 self.assertIs(loads(dumps(SomeTuple.first)), SomeTuple.first) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
982 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
983 def test_duplicate_values_give_unique_enum_items(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
984 class AutoNumber(Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
985 first = () |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
986 second = () |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
987 third = () |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
988 def __new__(cls): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
989 value = len(cls.__members__) + 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
990 obj = object.__new__(cls) |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
991 obj._value_ = value |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
992 return obj |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
993 def __int__(self): |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
994 return int(self._value_) |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
995 self.assertEqual( |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
996 list(AutoNumber), |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
997 [AutoNumber.first, AutoNumber.second, AutoNumber.third], |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
998 ) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
999 self.assertEqual(int(AutoNumber.second), 2) |
|
84720
37c427e3eb94
Close #18508 -- fix _value2member_map to always have the member's value
Ethan Furman <ethan@stoneleaf.us>
parents:
84713
diff
changeset
|
1000 self.assertEqual(AutoNumber.third.value, 3) |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1001 self.assertIs(AutoNumber(1), AutoNumber.first) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1002 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1003 def test_inherited_new_from_enhanced_enum(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1004 class AutoNumber(Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1005 def __new__(cls): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1006 value = len(cls.__members__) + 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1007 obj = object.__new__(cls) |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
1008 obj._value_ = value |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1009 return obj |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1010 def __int__(self): |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
1011 return int(self._value_) |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1012 class Color(AutoNumber): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1013 red = () |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1014 green = () |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1015 blue = () |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1016 self.assertEqual(list(Color), [Color.red, Color.green, Color.blue]) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1017 self.assertEqual(list(map(int, Color)), [1, 2, 3]) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1018 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1019 def test_inherited_new_from_mixed_enum(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1020 class AutoNumber(IntEnum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1021 def __new__(cls): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1022 value = len(cls.__members__) + 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1023 obj = int.__new__(cls, value) |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
1024 obj._value_ = value |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1025 return obj |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1026 class Color(AutoNumber): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1027 red = () |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1028 green = () |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1029 blue = () |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1030 self.assertEqual(list(Color), [Color.red, Color.green, Color.blue]) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1031 self.assertEqual(list(map(int, Color)), [1, 2, 3]) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1032 |
|
87087
ca909a3728d3
removed Enum.__eq__ as it added nothing
Ethan Furman <ethan@stoneleaf.us>
parents:
86558
diff
changeset
|
1033 def test_equality(self): |
|
ca909a3728d3
removed Enum.__eq__ as it added nothing
Ethan Furman <ethan@stoneleaf.us>
parents:
86558
diff
changeset
|
1034 class AlwaysEqual: |
|
ca909a3728d3
removed Enum.__eq__ as it added nothing
Ethan Furman <ethan@stoneleaf.us>
parents:
86558
diff
changeset
|
1035 def __eq__(self, other): |
|
ca909a3728d3
removed Enum.__eq__ as it added nothing
Ethan Furman <ethan@stoneleaf.us>
parents:
86558
diff
changeset
|
1036 return True |
|
ca909a3728d3
removed Enum.__eq__ as it added nothing
Ethan Furman <ethan@stoneleaf.us>
parents:
86558
diff
changeset
|
1037 class OrdinaryEnum(Enum): |
|
ca909a3728d3
removed Enum.__eq__ as it added nothing
Ethan Furman <ethan@stoneleaf.us>
parents:
86558
diff
changeset
|
1038 a = 1 |
|
ca909a3728d3
removed Enum.__eq__ as it added nothing
Ethan Furman <ethan@stoneleaf.us>
parents:
86558
diff
changeset
|
1039 self.assertEqual(AlwaysEqual(), OrdinaryEnum.a) |
|
ca909a3728d3
removed Enum.__eq__ as it added nothing
Ethan Furman <ethan@stoneleaf.us>
parents:
86558
diff
changeset
|
1040 self.assertEqual(OrdinaryEnum.a, AlwaysEqual()) |
|
ca909a3728d3
removed Enum.__eq__ as it added nothing
Ethan Furman <ethan@stoneleaf.us>
parents:
86558
diff
changeset
|
1041 |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1042 def test_ordered_mixin(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1043 class OrderedEnum(Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1044 def __ge__(self, other): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1045 if self.__class__ is other.__class__: |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
1046 return self._value_ >= other._value_ |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1047 return NotImplemented |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1048 def __gt__(self, other): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1049 if self.__class__ is other.__class__: |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
1050 return self._value_ > other._value_ |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1051 return NotImplemented |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1052 def __le__(self, other): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1053 if self.__class__ is other.__class__: |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
1054 return self._value_ <= other._value_ |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1055 return NotImplemented |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1056 def __lt__(self, other): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1057 if self.__class__ is other.__class__: |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
1058 return self._value_ < other._value_ |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1059 return NotImplemented |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1060 class Grade(OrderedEnum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1061 A = 5 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1062 B = 4 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1063 C = 3 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1064 D = 2 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1065 F = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1066 self.assertGreater(Grade.A, Grade.B) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1067 self.assertLessEqual(Grade.F, Grade.C) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1068 self.assertLess(Grade.D, Grade.A) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1069 self.assertGreaterEqual(Grade.B, Grade.B) |
|
87087
ca909a3728d3
removed Enum.__eq__ as it added nothing
Ethan Furman <ethan@stoneleaf.us>
parents:
86558
diff
changeset
|
1070 self.assertEqual(Grade.B, Grade.B) |
|
ca909a3728d3
removed Enum.__eq__ as it added nothing
Ethan Furman <ethan@stoneleaf.us>
parents:
86558
diff
changeset
|
1071 self.assertNotEqual(Grade.C, Grade.D) |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
1072 |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1073 def test_extending2(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1074 class Shade(Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1075 def shade(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1076 print(self.name) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1077 class Color(Shade): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1078 red = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1079 green = 2 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1080 blue = 3 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1081 with self.assertRaises(TypeError): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1082 class MoreColor(Color): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1083 cyan = 4 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1084 magenta = 5 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1085 yellow = 6 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1086 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1087 def test_extending3(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1088 class Shade(Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1089 def shade(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1090 return self.name |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1091 class Color(Shade): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1092 def hex(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1093 return '%s hexlified!' % self.value |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1094 class MoreColor(Color): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1095 cyan = 4 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1096 magenta = 5 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1097 yellow = 6 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1098 self.assertEqual(MoreColor.magenta.hex(), '5 hexlified!') |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1099 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1100 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1101 def test_no_duplicates(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1102 class UniqueEnum(Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1103 def __init__(self, *args): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1104 cls = self.__class__ |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1105 if any(self.value == e.value for e in cls): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1106 a = self.name |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1107 e = cls(self.value).name |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1108 raise ValueError( |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1109 "aliases not allowed in UniqueEnum: %r --> %r" |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1110 % (a, e) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1111 ) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1112 class Color(UniqueEnum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1113 red = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1114 green = 2 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1115 blue = 3 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1116 with self.assertRaises(ValueError): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1117 class Color(UniqueEnum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1118 red = 1 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1119 green = 2 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1120 blue = 3 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1121 grene = 2 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1122 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1123 def test_init(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1124 class Planet(Enum): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1125 MERCURY = (3.303e+23, 2.4397e6) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1126 VENUS = (4.869e+24, 6.0518e6) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1127 EARTH = (5.976e+24, 6.37814e6) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1128 MARS = (6.421e+23, 3.3972e6) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1129 JUPITER = (1.9e+27, 7.1492e7) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1130 SATURN = (5.688e+26, 6.0268e7) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1131 URANUS = (8.686e+25, 2.5559e7) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1132 NEPTUNE = (1.024e+26, 2.4746e7) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1133 def __init__(self, mass, radius): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1134 self.mass = mass # in kilograms |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1135 self.radius = radius # in meters |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1136 @property |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1137 def surface_gravity(self): |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1138 # universal gravitational constant (m3 kg-1 s-2) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1139 G = 6.67300E-11 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1140 return G * self.mass / (self.radius * self.radius) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1141 self.assertEqual(round(Planet.EARTH.surface_gravity, 2), 9.80) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1142 self.assertEqual(Planet.EARTH.value, (5.976e+24, 6.37814e6)) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1143 |
|
84720
37c427e3eb94
Close #18508 -- fix _value2member_map to always have the member's value
Ethan Furman <ethan@stoneleaf.us>
parents:
84713
diff
changeset
|
1144 def test_nonhash_value(self): |
|
37c427e3eb94
Close #18508 -- fix _value2member_map to always have the member's value
Ethan Furman <ethan@stoneleaf.us>
parents:
84713
diff
changeset
|
1145 class AutoNumberInAList(Enum): |
|
37c427e3eb94
Close #18508 -- fix _value2member_map to always have the member's value
Ethan Furman <ethan@stoneleaf.us>
parents:
84713
diff
changeset
|
1146 def __new__(cls): |
|
37c427e3eb94
Close #18508 -- fix _value2member_map to always have the member's value
Ethan Furman <ethan@stoneleaf.us>
parents:
84713
diff
changeset
|
1147 value = [len(cls.__members__) + 1] |
|
37c427e3eb94
Close #18508 -- fix _value2member_map to always have the member's value
Ethan Furman <ethan@stoneleaf.us>
parents:
84713
diff
changeset
|
1148 obj = object.__new__(cls) |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
1149 obj._value_ = value |
|
84720
37c427e3eb94
Close #18508 -- fix _value2member_map to always have the member's value
Ethan Furman <ethan@stoneleaf.us>
parents:
84713
diff
changeset
|
1150 return obj |
|
37c427e3eb94
Close #18508 -- fix _value2member_map to always have the member's value
Ethan Furman <ethan@stoneleaf.us>
parents:
84713
diff
changeset
|
1151 class ColorInAList(AutoNumberInAList): |
|
37c427e3eb94
Close #18508 -- fix _value2member_map to always have the member's value
Ethan Furman <ethan@stoneleaf.us>
parents:
84713
diff
changeset
|
1152 red = () |
|
37c427e3eb94
Close #18508 -- fix _value2member_map to always have the member's value
Ethan Furman <ethan@stoneleaf.us>
parents:
84713
diff
changeset
|
1153 green = () |
|
37c427e3eb94
Close #18508 -- fix _value2member_map to always have the member's value
Ethan Furman <ethan@stoneleaf.us>
parents:
84713
diff
changeset
|
1154 blue = () |
|
37c427e3eb94
Close #18508 -- fix _value2member_map to always have the member's value
Ethan Furman <ethan@stoneleaf.us>
parents:
84713
diff
changeset
|
1155 self.assertEqual(list(ColorInAList), [ColorInAList.red, ColorInAList.green, ColorInAList.blue]) |
|
86389
89f6abc2e115
Close #19252: better test coverage for Enum. Thanks, CliffM
Ethan Furman <ethan@stoneleaf.us>
parents:
86139
diff
changeset
|
1156 for enum, value in zip(ColorInAList, range(3)): |
|
89f6abc2e115
Close #19252: better test coverage for Enum. Thanks, CliffM
Ethan Furman <ethan@stoneleaf.us>
parents:
86139
diff
changeset
|
1157 value += 1 |
|
89f6abc2e115
Close #19252: better test coverage for Enum. Thanks, CliffM
Ethan Furman <ethan@stoneleaf.us>
parents:
86139
diff
changeset
|
1158 self.assertEqual(enum.value, [value]) |
|
89f6abc2e115
Close #19252: better test coverage for Enum. Thanks, CliffM
Ethan Furman <ethan@stoneleaf.us>
parents:
86139
diff
changeset
|
1159 self.assertIs(ColorInAList([value]), enum) |
|
84720
37c427e3eb94
Close #18508 -- fix _value2member_map to always have the member's value
Ethan Furman <ethan@stoneleaf.us>
parents:
84713
diff
changeset
|
1160 |
|
84826
95e1d0efd896
Close #18545: now only executes member_type if no _value_ is assigned in __new__.
Ethan Furman <ethan@stoneleaf.us>
parents:
84721
diff
changeset
|
1161 def test_conflicting_types_resolved_in_new(self): |
|
95e1d0efd896
Close #18545: now only executes member_type if no _value_ is assigned in __new__.
Ethan Furman <ethan@stoneleaf.us>
parents:
84721
diff
changeset
|
1162 class LabelledIntEnum(int, Enum): |
|
95e1d0efd896
Close #18545: now only executes member_type if no _value_ is assigned in __new__.
Ethan Furman <ethan@stoneleaf.us>
parents:
84721
diff
changeset
|
1163 def __new__(cls, *args): |
|
95e1d0efd896
Close #18545: now only executes member_type if no _value_ is assigned in __new__.
Ethan Furman <ethan@stoneleaf.us>
parents:
84721
diff
changeset
|
1164 value, label = args |
|
95e1d0efd896
Close #18545: now only executes member_type if no _value_ is assigned in __new__.
Ethan Furman <ethan@stoneleaf.us>
parents:
84721
diff
changeset
|
1165 obj = int.__new__(cls, value) |
|
95e1d0efd896
Close #18545: now only executes member_type if no _value_ is assigned in __new__.
Ethan Furman <ethan@stoneleaf.us>
parents:
84721
diff
changeset
|
1166 obj.label = label |
|
95e1d0efd896
Close #18545: now only executes member_type if no _value_ is assigned in __new__.
Ethan Furman <ethan@stoneleaf.us>
parents:
84721
diff
changeset
|
1167 obj._value_ = value |
|
95e1d0efd896
Close #18545: now only executes member_type if no _value_ is assigned in __new__.
Ethan Furman <ethan@stoneleaf.us>
parents:
84721
diff
changeset
|
1168 return obj |
|
95e1d0efd896
Close #18545: now only executes member_type if no _value_ is assigned in __new__.
Ethan Furman <ethan@stoneleaf.us>
parents:
84721
diff
changeset
|
1169 |
|
95e1d0efd896
Close #18545: now only executes member_type if no _value_ is assigned in __new__.
Ethan Furman <ethan@stoneleaf.us>
parents:
84721
diff
changeset
|
1170 class LabelledList(LabelledIntEnum): |
|
95e1d0efd896
Close #18545: now only executes member_type if no _value_ is assigned in __new__.
Ethan Furman <ethan@stoneleaf.us>
parents:
84721
diff
changeset
|
1171 unprocessed = (1, "Unprocessed") |
|
95e1d0efd896
Close #18545: now only executes member_type if no _value_ is assigned in __new__.
Ethan Furman <ethan@stoneleaf.us>
parents:
84721
diff
changeset
|
1172 payment_complete = (2, "Payment Complete") |
|
95e1d0efd896
Close #18545: now only executes member_type if no _value_ is assigned in __new__.
Ethan Furman <ethan@stoneleaf.us>
parents:
84721
diff
changeset
|
1173 |
|
95e1d0efd896
Close #18545: now only executes member_type if no _value_ is assigned in __new__.
Ethan Furman <ethan@stoneleaf.us>
parents:
84721
diff
changeset
|
1174 self.assertEqual(list(LabelledList), [LabelledList.unprocessed, LabelledList.payment_complete]) |
|
95e1d0efd896
Close #18545: now only executes member_type if no _value_ is assigned in __new__.
Ethan Furman <ethan@stoneleaf.us>
parents:
84721
diff
changeset
|
1175 self.assertEqual(LabelledList.unprocessed, 1) |
|
95e1d0efd896
Close #18545: now only executes member_type if no _value_ is assigned in __new__.
Ethan Furman <ethan@stoneleaf.us>
parents:
84721
diff
changeset
|
1176 self.assertEqual(LabelledList(1), LabelledList.unprocessed) |
|
84720
37c427e3eb94
Close #18508 -- fix _value2member_map to always have the member's value
Ethan Furman <ethan@stoneleaf.us>
parents:
84713
diff
changeset
|
1177 |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1178 |
|
84713
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1179 class TestUnique(unittest.TestCase): |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1180 |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1181 def test_unique_clean(self): |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1182 @unique |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1183 class Clean(Enum): |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1184 one = 1 |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1185 two = 'dos' |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1186 tres = 4.0 |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1187 @unique |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1188 class Cleaner(IntEnum): |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1189 single = 1 |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1190 double = 2 |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1191 triple = 3 |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1192 |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1193 def test_unique_dirty(self): |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1194 with self.assertRaisesRegex(ValueError, 'tres.*one'): |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1195 @unique |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1196 class Dirty(Enum): |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1197 one = 1 |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1198 two = 'dos' |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1199 tres = 1 |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1200 with self.assertRaisesRegex( |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1201 ValueError, |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1202 'double.*single.*turkey.*triple', |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1203 ): |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1204 @unique |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1205 class Dirtier(IntEnum): |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1206 single = 1 |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1207 double = 1 |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1208 triple = 3 |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1209 turkey = 3 |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1210 |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1211 |
|
86558
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1212 expected_help_output = """ |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1213 Help on class Color in module %s: |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1214 |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1215 class Color(enum.Enum) |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1216 | Method resolution order: |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1217 | Color |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1218 | enum.Enum |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1219 | builtins.object |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1220 |\x20\x20 |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1221 | Data and other attributes defined here: |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1222 |\x20\x20 |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1223 | blue = <Color.blue: 3> |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1224 |\x20\x20 |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1225 | green = <Color.green: 2> |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1226 |\x20\x20 |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1227 | red = <Color.red: 1> |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1228 |\x20\x20 |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1229 | ---------------------------------------------------------------------- |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1230 | Data descriptors inherited from enum.Enum: |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1231 |\x20\x20 |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1232 | name |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1233 | The name of the Enum member. |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1234 |\x20\x20 |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1235 | value |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1236 | The value of the Enum member. |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1237 |\x20\x20 |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1238 | ---------------------------------------------------------------------- |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1239 | Data descriptors inherited from enum.EnumMeta: |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1240 |\x20\x20 |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1241 | __members__ |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1242 | Returns a mapping of member name->value. |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1243 |\x20\x20\x20\x20\x20\x20 |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1244 | This mapping lists all enum members, including aliases. Note that this |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1245 | is a read-only view of the internal mapping. |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1246 """.strip() |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1247 |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1248 class TestStdLib(unittest.TestCase): |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1249 |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1250 class Color(Enum): |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1251 red = 1 |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1252 green = 2 |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1253 blue = 3 |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1254 |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1255 def test_pydoc(self): |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1256 # indirectly test __objclass__ |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1257 expected_text = expected_help_output % __name__ |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1258 output = StringIO() |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1259 helper = pydoc.Helper(output=output) |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1260 helper(self.Color) |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1261 result = output.getvalue().strip() |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1262 if result != expected_text: |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1263 print_diffs(expected_text, result) |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1264 self.fail("outputs are not equal, see diff above") |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1265 |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1266 def test_inspect_getmembers(self): |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1267 values = dict(( |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1268 ('__class__', EnumMeta), |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1269 ('__doc__', None), |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1270 ('__members__', self.Color.__members__), |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1271 ('__module__', __name__), |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1272 ('blue', self.Color.blue), |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1273 ('green', self.Color.green), |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1274 ('name', Enum.__dict__['name']), |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1275 ('red', self.Color.red), |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1276 ('value', Enum.__dict__['value']), |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1277 )) |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1278 result = dict(inspect.getmembers(self.Color)) |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1279 self.assertEqual(values.keys(), result.keys()) |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1280 failed = False |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1281 for k in values.keys(): |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1282 if result[k] != values[k]: |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1283 print() |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1284 print('\n%s\n key: %s\n result: %s\nexpected: %s\n%s\n' % |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1285 ('=' * 75, k, result[k], values[k], '=' * 75), sep='') |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1286 failed = True |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1287 if failed: |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1288 self.fail("result does not equal expected, see print above") |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1289 |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1290 def test_inspect_classify_class_attrs(self): |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1291 # indirectly test __objclass__ |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1292 from inspect import Attribute |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1293 values = [ |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1294 Attribute(name='__class__', kind='data', |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1295 defining_class=object, object=EnumMeta), |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1296 Attribute(name='__doc__', kind='data', |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1297 defining_class=self.Color, object=None), |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1298 Attribute(name='__members__', kind='property', |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1299 defining_class=EnumMeta, object=EnumMeta.__members__), |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1300 Attribute(name='__module__', kind='data', |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1301 defining_class=self.Color, object=__name__), |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1302 Attribute(name='blue', kind='data', |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1303 defining_class=self.Color, object=self.Color.blue), |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1304 Attribute(name='green', kind='data', |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1305 defining_class=self.Color, object=self.Color.green), |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1306 Attribute(name='red', kind='data', |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1307 defining_class=self.Color, object=self.Color.red), |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1308 Attribute(name='name', kind='data', |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1309 defining_class=Enum, object=Enum.__dict__['name']), |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1310 Attribute(name='value', kind='data', |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1311 defining_class=Enum, object=Enum.__dict__['value']), |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1312 ] |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1313 values.sort(key=lambda item: item.name) |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1314 result = list(inspect.classify_class_attrs(self.Color)) |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1315 result.sort(key=lambda item: item.name) |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1316 failed = False |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1317 for v, r in zip(values, result): |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1318 if r != v: |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1319 print('\n%s\n%s\n%s\n%s\n' % ('=' * 75, r, v, '=' * 75), sep='') |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1320 failed = True |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1321 if failed: |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1322 self.fail("result does not equal expected, see print above") |
|
b9019b942435
Close #19263: add tests to ensure __objclass__ correctly set.
Ethan Furman <ethan@stoneleaf.us>
parents:
86389
diff
changeset
|
1323 |
|
84130
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1324 if __name__ == '__main__': |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1325 unittest.main() |
