Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_enum.py @ 86127:a3d09c19bb93
merge with 3.3
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Sun, 06 Oct 2013 19:24:00 +0200 |
| parents | ed011b0d7daf |
| children | 516576f5f9dc |
| 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 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
2 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
|
3 from collections import OrderedDict |
|
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 from pickle import dumps, loads, PicklingError |
|
84713
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
5 from enum import Enum, IntEnum, unique |
|
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
|
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
|
7 # 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
|
8 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
|
9 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
|
10 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
|
11 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
|
12 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
|
13 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
|
14 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
|
15 |
|
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 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
|
17 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
|
18 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
|
19 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
|
20 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
|
21 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
|
22 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
|
23 |
|
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 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
|
25 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
|
26 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
|
27 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
|
28 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
|
29 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
|
30 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
|
31 |
|
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 # 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
|
33 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
|
34 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
|
35 '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
|
36 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
|
37 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
|
38 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
|
39 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
|
40 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
|
41 |
|
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 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
|
43 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
|
44 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
|
45 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
|
46 |
|
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 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
|
48 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
|
49 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
|
50 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
|
51 |
|
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 # 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
|
53 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
|
54 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
|
55 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
|
56 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
|
57 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
|
58 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
|
59 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
|
60 |
|
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 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
|
62 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
|
63 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
|
64 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
|
65 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
|
66 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
|
67 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
|
68 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
|
69 |
|
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
|
70 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
|
71 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
|
72 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
|
73 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
|
74 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
|
75 |
|
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
|
76 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
|
77 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
|
78 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
|
79 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
|
80 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
|
81 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
|
82 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
|
83 |
|
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
|
84 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
|
85 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
|
86 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
|
87 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
|
88 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
|
89 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
|
90 |
|
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
|
91 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
|
92 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
|
93 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
|
94 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
|
95 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
|
96 |
|
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
|
97 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
|
98 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
|
99 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
|
100 set(dir(Season)), |
|
85724
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
101 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
|
102 '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
|
103 ) |
|
39697dcd97e3
Issue 18693: Put custom __dir__ back in place. Will instead look at fixing `help()`.
Ethan Furman <ethan@stoneleaf.us>
parents:
85104
diff
changeset
|
104 |
|
39697dcd97e3
Issue 18693: Put custom __dir__ back in place. Will instead look at fixing `help()`.
Ethan Furman <ethan@stoneleaf.us>
parents:
85104
diff
changeset
|
105 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
|
106 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
|
107 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
|
108 set(dir(Season.WINTER)), |
|
85724
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
109 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
|
110 ) |
|
39697dcd97e3
Issue 18693: Put custom __dir__ back in place. Will instead look at fixing `help()`.
Ethan Furman <ethan@stoneleaf.us>
parents:
85104
diff
changeset
|
111 |
|
85724
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
112 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
|
113 class Test(Enum): |
|
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
114 this = 'that' |
|
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
115 these = 'those' |
|
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
116 def wowser(self): |
|
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
117 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
|
118 self.assertEqual( |
|
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
119 set(dir(Test)), |
|
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
120 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
|
121 ) |
|
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
122 self.assertEqual( |
|
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
123 set(dir(Test.this)), |
|
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
124 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
|
125 ) |
|
353ced6ae182
Close #18693: Enum is now more help() friendly.
Ethan Furman <ethan@stoneleaf.us>
parents:
85717
diff
changeset
|
126 |
|
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
|
127 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
|
128 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
|
129 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
|
130 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
131 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
|
132 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
|
133 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
|
134 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
135 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
|
136 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
|
137 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
138 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
|
139 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
|
140 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
|
141 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
|
142 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
|
143 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
|
144 [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
|
145 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
146 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
|
147 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
|
148 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
|
149 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
|
150 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
|
151 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
|
152 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
|
153 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
|
154 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
|
155 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
|
156 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
|
157 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
|
158 '<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
|
159 ) |
|
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 |
|
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 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
|
162 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
|
163 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
|
164 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
|
165 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
|
166 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
|
167 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
|
168 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
|
169 |
|
85574
1d88d04aade2
Close #18924: Block naive attempts to change an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85485
diff
changeset
|
170 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
|
171 Season = self.Season |
|
1d88d04aade2
Close #18924: Block naive attempts to change an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85485
diff
changeset
|
172 with self.assertRaises(AttributeError): |
|
1d88d04aade2
Close #18924: Block naive attempts to change an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85485
diff
changeset
|
173 Season.WINTER = 'really cold' |
|
1d88d04aade2
Close #18924: Block naive attempts to change an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85485
diff
changeset
|
174 |
|
85790
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
175 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
|
176 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
|
177 SPRING = 1 |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
178 SUMMER = 2 |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
179 AUTUMN = 3 |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
180 WINTER = 4 |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
181 |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
182 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
|
183 pass |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
184 |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
185 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
|
186 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
|
187 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
|
188 |
|
ed011b0d7daf
Close #19025: Better error message when trying to delete an Enum member.
Ethan Furman <ethan@stoneleaf.us>
parents:
85724
diff
changeset
|
189 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
|
190 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
|
191 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
|
192 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
|
193 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
|
194 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
|
195 |
|
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
|
196 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
|
197 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
|
198 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
|
199 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
|
200 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
|
201 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
|
202 _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
|
203 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
|
204 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
|
205 _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
|
206 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
|
207 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
|
208 _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
|
209 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
|
210 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
|
211 _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
|
212 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
213 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
|
214 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
|
215 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
|
216 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
|
217 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
218 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
|
219 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
|
220 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
221 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
|
222 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
|
223 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
|
224 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
225 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
|
226 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
|
227 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
|
228 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
|
229 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
|
230 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
|
231 |
|
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 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
|
233 |
|
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 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
|
235 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
|
236 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
|
237 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
|
238 |
|
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 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
|
240 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
|
241 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
|
242 |
|
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 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
|
244 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
|
245 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
|
246 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
|
247 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
|
248 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
|
249 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
|
250 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
|
251 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
|
252 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
|
253 [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
|
254 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
|
255 ]) |
|
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 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
|
257 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
|
258 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
|
259 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
|
260 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
|
261 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
|
262 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
|
263 [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
|
264 ['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
|
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 |
|
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
|
267 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
|
268 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
|
269 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
|
270 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
|
271 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
|
272 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
|
273 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
|
274 |
|
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
|
275 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
|
276 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
|
277 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
|
278 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
|
279 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
|
280 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
|
281 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
|
282 |
|
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
|
283 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
|
284 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
|
285 @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
|
286 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
|
287 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
|
288 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
|
289 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
|
290 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
|
291 |
|
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
|
292 |
|
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
|
293 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
|
294 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
|
295 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
|
296 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
|
297 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
|
298 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
|
299 [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
|
300 ) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
301 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
|
302 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
|
303 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
|
304 |
|
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
|
305 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
|
306 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
|
307 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
|
308 '{}'.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
|
309 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
|
310 '{:}'.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
|
311 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
|
312 '{: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
|
313 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
|
314 '{:^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
|
315 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
|
316 '{:>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
|
317 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
|
318 '{:<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
|
319 |
|
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
|
320 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
|
321 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
|
322 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
|
323 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
|
324 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
|
325 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
|
326 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
|
327 |
|
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
|
328 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
|
329 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
|
330 |
|
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
|
331 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
|
332 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
|
333 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
|
334 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
|
335 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
|
336 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
|
337 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
|
338 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
|
339 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
|
340 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
|
341 |
|
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 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
|
343 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
|
344 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
|
345 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
|
346 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
|
347 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
|
348 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
|
349 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
|
350 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
|
351 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
|
352 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
|
353 |
|
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 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
|
355 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
|
356 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
|
357 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
|
358 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
|
359 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
|
360 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
|
361 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
|
362 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
|
363 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
|
364 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
|
365 |
|
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 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
|
367 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
|
368 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
|
369 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
|
370 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
|
371 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
|
372 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
|
373 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
|
374 |
|
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
|
375 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
|
376 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
|
377 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
|
378 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
|
379 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
|
380 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
|
381 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
|
382 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
|
383 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
384 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
|
385 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
|
386 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
|
387 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
|
388 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
|
389 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
390 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
|
391 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
|
392 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
|
393 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
|
394 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
|
395 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
|
396 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
|
397 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
398 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
|
399 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
|
400 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
|
401 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
|
402 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
|
403 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
404 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
|
405 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
|
406 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
|
407 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
|
408 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
|
409 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
|
410 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
|
411 |
|
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 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
|
413 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
|
414 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
|
415 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
|
416 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
|
417 |
|
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 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
|
419 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
|
420 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
|
421 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
|
422 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
|
423 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
|
424 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
|
425 |
|
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 |
|
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 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
|
428 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
|
429 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
|
430 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
|
431 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
|
432 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
|
433 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
|
434 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
|
435 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
|
436 |
|
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 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
|
438 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
|
439 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
440 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
|
441 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
|
442 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
|
443 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
|
444 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
|
445 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
|
446 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
|
447 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
|
448 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
|
449 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
|
450 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
|
451 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
|
452 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
|
453 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
|
454 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
|
455 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
|
456 |
|
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 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
|
458 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
|
459 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
|
460 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
|
461 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
|
462 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
|
463 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
|
464 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
|
465 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
|
466 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
|
467 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
|
468 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
|
469 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
|
470 |
|
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 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
|
472 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
|
473 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
|
474 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
|
475 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
|
476 |
|
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 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
|
478 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
|
479 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
|
480 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
|
481 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
|
482 |
|
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 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
|
484 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
|
485 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
|
486 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
|
487 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
|
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_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
|
490 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
|
491 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
|
492 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
|
493 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
|
494 |
|
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 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
|
496 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
|
497 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
|
498 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
|
499 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
|
500 |
|
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 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
|
502 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
|
503 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
|
504 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
|
505 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
|
506 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
|
507 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
|
508 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
|
509 |
|
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 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
|
511 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
|
512 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
|
513 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
|
514 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
|
515 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
|
516 |
|
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 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
|
518 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
|
519 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
|
520 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
|
521 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
|
522 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
|
523 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
|
524 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
|
525 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
|
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_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
|
528 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
|
529 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
|
530 |
|
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 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
|
532 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
|
533 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
|
534 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
|
535 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
|
536 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
|
537 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
|
538 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
|
539 [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
|
540 ) |
|
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 |
|
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
|
542 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
|
543 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
|
544 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
|
545 [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
|
546 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
|
547 ) |
|
24aa6f98fe25
Add __reversed__ to Enum. Minor code reorg (moved __members__ to be in alpha order).
Ethan Furman <ethan@stoneleaf.us>
parents:
85574
diff
changeset
|
548 |
|
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
|
549 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
|
550 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
|
551 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
|
552 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
|
553 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
|
554 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
|
555 [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
|
556 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
|
557 ) |
|
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 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
|
559 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
|
560 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
|
561 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
|
562 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
|
563 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
|
564 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
|
565 |
|
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 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
|
567 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
|
568 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
|
569 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
|
570 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
|
571 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
|
572 [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
|
573 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
|
574 ) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
575 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
|
576 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
|
577 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
|
578 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
|
579 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
|
580 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
|
581 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
|
582 |
|
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 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
|
584 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
|
585 '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 (('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
|
587 ) |
|
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 = 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
|
589 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
|
590 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
|
591 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
|
592 [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
|
593 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
|
594 ) |
|
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 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
|
596 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
|
597 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
|
598 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
|
599 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
|
600 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
|
601 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
|
602 |
|
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 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
|
604 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
|
605 '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
|
606 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
|
607 ) |
|
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 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
|
609 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
|
610 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
|
611 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
|
612 [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
|
613 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
|
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 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
|
616 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
|
617 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
|
618 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
|
619 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
|
620 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
|
621 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
|
622 |
|
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 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
|
624 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
|
625 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
|
626 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
|
627 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
|
628 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
|
629 [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
|
630 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
|
631 ) |
|
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 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
|
633 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
|
634 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
|
635 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
|
636 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
|
637 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
|
638 |
|
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 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
|
640 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
|
641 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
|
642 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
|
643 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
|
644 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
|
645 [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
|
646 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
|
647 ) |
|
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 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
|
649 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
|
650 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
|
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_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
|
656 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
|
657 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
|
658 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
|
659 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
|
660 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
|
661 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
|
662 |
|
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 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
|
664 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
|
665 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
|
666 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
|
667 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
|
668 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
|
669 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
|
670 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
|
671 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
|
672 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
|
673 |
|
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 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
|
675 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
|
676 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
|
677 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
|
678 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
|
679 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
|
680 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
|
681 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
|
682 |
|
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 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
|
684 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
|
685 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
|
686 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
|
687 |
|
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 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
|
689 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
|
690 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
|
691 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
|
692 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
|
693 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
|
694 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
|
695 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
|
696 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
|
697 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
|
698 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
|
699 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
|
700 |
|
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 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
|
702 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
|
703 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
|
704 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
|
705 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
|
706 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
|
707 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
|
708 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
|
709 |
|
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 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
|
711 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
|
712 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
|
713 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
|
714 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
|
715 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
|
716 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
|
717 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
|
718 |
|
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 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
|
720 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
|
721 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
|
722 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
|
723 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
|
724 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
|
725 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
|
726 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
|
727 |
|
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 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
|
729 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
|
730 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
|
731 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
|
732 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
|
733 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
|
734 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
|
735 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
|
736 |
|
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 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
|
738 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
|
739 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
|
740 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
|
741 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
|
742 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
|
743 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
|
744 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
|
745 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
|
746 |
|
85021
b034418e840b
Close #18635: Move class level private attribute from instance to class.
Ethan Furman <ethan@stoneleaf.us>
parents:
84826
diff
changeset
|
747 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
|
748 class Number(IntEnum): |
|
b034418e840b
Close #18635: Move class level private attribute from instance to class.
Ethan Furman <ethan@stoneleaf.us>
parents:
84826
diff
changeset
|
749 one = 100 |
|
b034418e840b
Close #18635: Move class level private attribute from instance to class.
Ethan Furman <ethan@stoneleaf.us>
parents:
84826
diff
changeset
|
750 two = 200 |
|
b034418e840b
Close #18635: Move class level private attribute from instance to class.
Ethan Furman <ethan@stoneleaf.us>
parents:
84826
diff
changeset
|
751 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
|
752 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
|
753 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
|
754 yarn = 'soft' |
|
b034418e840b
Close #18635: Move class level private attribute from instance to class.
Ethan Furman <ethan@stoneleaf.us>
parents:
84826
diff
changeset
|
755 rope = 'rough' |
|
b034418e840b
Close #18635: Move class level private attribute from instance to class.
Ethan Furman <ethan@stoneleaf.us>
parents:
84826
diff
changeset
|
756 wire = 'hard' |
|
b034418e840b
Close #18635: Move class level private attribute from instance to class.
Ethan Furman <ethan@stoneleaf.us>
parents:
84826
diff
changeset
|
757 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
|
758 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
|
759 class Plain(Enum): |
|
b034418e840b
Close #18635: Move class level private attribute from instance to class.
Ethan Furman <ethan@stoneleaf.us>
parents:
84826
diff
changeset
|
760 vanilla = 'white' |
|
b034418e840b
Close #18635: Move class level private attribute from instance to class.
Ethan Furman <ethan@stoneleaf.us>
parents:
84826
diff
changeset
|
761 one = 1 |
|
b034418e840b
Close #18635: Move class level private attribute from instance to class.
Ethan Furman <ethan@stoneleaf.us>
parents:
84826
diff
changeset
|
762 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
|
763 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
|
764 |
|
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
|
765 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
|
766 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
|
767 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
|
768 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
|
769 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
|
770 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
|
771 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
|
772 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
|
773 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
|
774 |
|
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 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
|
776 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
|
777 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
|
778 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
|
779 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
|
780 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
|
781 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
|
782 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
|
783 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
|
784 "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
|
785 ) |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
786 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
787 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
|
788 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
|
789 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
|
790 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
|
791 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
|
792 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
|
793 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
|
794 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
|
795 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
|
796 |
|
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_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
|
798 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
|
799 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
|
800 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
|
801 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
|
802 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
|
803 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
|
804 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
|
805 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
|
806 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
|
807 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
|
808 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
|
809 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
|
810 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
|
811 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
|
812 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
|
813 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
|
814 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
|
815 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
|
816 |
|
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 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
|
818 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
|
819 |
|
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 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
|
821 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
|
822 |
|
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 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
|
824 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
|
825 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
|
826 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
|
827 |
|
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 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
|
829 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
|
830 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
|
831 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
|
832 |
|
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 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
|
834 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
|
835 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
|
836 _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
|
837 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
|
838 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
|
839 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
|
840 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
|
841 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
|
842 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
|
843 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
|
844 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
|
845 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
|
846 @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
|
847 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
|
848 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
|
849 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
|
850 # 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
|
851 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
|
852 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
|
853 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
|
854 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
|
855 # 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
|
856 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
|
857 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
|
858 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
|
859 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
|
860 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
|
861 # 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
|
862 # 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
|
863 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
|
864 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
|
865 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
|
866 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
|
867 '({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
|
868 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
|
869 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
|
870 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
|
871 |
|
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 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
|
873 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
|
874 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
|
875 |
|
84720
37c427e3eb94
Close #18508 -- fix _value2member_map to always have the member's value
Ethan Furman <ethan@stoneleaf.us>
parents:
84713
diff
changeset
|
876 |
|
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
|
877 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
|
878 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
|
879 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
|
880 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
|
881 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
|
882 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
|
883 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
|
884 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
|
885 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
|
886 |
|
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 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
|
888 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
|
889 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
|
890 _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
|
891 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
|
892 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
|
893 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
|
894 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
|
895 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
|
896 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
|
897 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
|
898 @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
|
899 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
|
900 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
|
901 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
|
902 # 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
|
903 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
|
904 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
|
905 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
|
906 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
|
907 # 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
|
908 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
|
909 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
|
910 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
|
911 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
|
912 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
|
913 # 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
|
914 # 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
|
915 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
|
916 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
|
917 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
|
918 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
|
919 '({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
|
920 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
|
921 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
|
922 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
|
923 |
|
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 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
|
925 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
|
926 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
|
927 |
|
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.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
|
929 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
|
930 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
|
931 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
|
932 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
|
933 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
|
934 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
|
935 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
|
936 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
|
937 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
|
938 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
|
939 |
|
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 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
|
941 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
|
942 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
|
943 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
|
944 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
|
945 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
|
946 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
|
947 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
|
948 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
|
949 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
|
950 |
|
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 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
|
952 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
|
953 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
|
954 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
|
955 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
|
956 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
|
957 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
|
958 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
|
959 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
|
960 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
|
961 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
|
962 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
|
963 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
|
964 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
|
965 [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
|
966 ) |
|
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 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
|
968 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
|
969 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
|
970 |
|
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 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
|
972 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
|
973 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
|
974 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
|
975 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
|
976 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
|
977 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
|
978 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
|
979 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
|
980 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
|
981 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
|
982 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
|
983 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
|
984 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
|
985 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
|
986 |
|
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 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
|
988 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
|
989 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
|
990 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
|
991 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
|
992 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
|
993 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
|
994 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
|
995 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
|
996 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
|
997 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
|
998 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
|
999 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
|
1000 |
|
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 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
|
1002 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
|
1003 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
|
1004 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
|
1005 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
|
1006 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
|
1007 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
|
1008 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
|
1009 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
|
1010 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
|
1011 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
|
1012 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
|
1013 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
|
1014 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
|
1015 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
|
1016 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
|
1017 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
|
1018 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
|
1019 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
|
1020 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
|
1021 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
|
1022 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
|
1023 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
|
1024 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
|
1025 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
|
1026 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
|
1027 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
|
1028 self.assertGreaterEqual(Grade.B, Grade.B) |
|
84721
511c4daac102
Change _names to _names_ since the latter is reserved for Enum use.
Ethan Furman <ethan@stoneleaf.us>
parents:
84720
diff
changeset
|
1029 |
|
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
|
1030 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
|
1031 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
|
1032 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
|
1033 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
|
1034 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
|
1035 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
|
1036 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
|
1037 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
|
1038 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
|
1039 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
|
1040 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
|
1041 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
|
1042 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
|
1043 |
|
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 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
|
1045 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
|
1046 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
|
1047 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
|
1048 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
|
1049 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
|
1050 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
|
1051 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
|
1052 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
|
1053 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
|
1054 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
|
1055 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
|
1056 |
|
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 |
|
e7a01c7f69fe
Closes issue 17947. Adds PEP-0435 (Adding an Enum type to the Python standard library).
Ethan Furman <ethan@stoneleaf.us>
parents:
diff
changeset
|
1058 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
|
1059 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
|
1060 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
|
1061 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
|
1062 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
|
1063 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
|
1064 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
|
1065 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
|
1066 "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
|
1067 % (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
|
1068 ) |
|
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 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
|
1070 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
|
1071 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
|
1072 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
|
1073 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
|
1074 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
|
1075 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
|
1076 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
|
1077 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
|
1078 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
|
1079 |
|
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 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
|
1081 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
|
1082 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
|
1083 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
|
1084 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
|
1085 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
|
1086 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
|
1087 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
|
1088 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
|
1089 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
|
1090 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
|
1091 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
|
1092 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
|
1093 @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
|
1094 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
|
1095 # 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
|
1096 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
|
1097 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
|
1098 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
|
1099 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
|
1100 |
|
84720
37c427e3eb94
Close #18508 -- fix _value2member_map to always have the member's value
Ethan Furman <ethan@stoneleaf.us>
parents:
84713
diff
changeset
|
1101 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
|
1102 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
|
1103 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
|
1104 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
|
1105 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
|
1106 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
|
1107 return obj |
|
37c427e3eb94
Close #18508 -- fix _value2member_map to always have the member's value
Ethan Furman <ethan@stoneleaf.us>
parents:
84713
diff
changeset
|
1108 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
|
1109 red = () |
|
37c427e3eb94
Close #18508 -- fix _value2member_map to always have the member's value
Ethan Furman <ethan@stoneleaf.us>
parents:
84713
diff
changeset
|
1110 green = () |
|
37c427e3eb94
Close #18508 -- fix _value2member_map to always have the member's value
Ethan Furman <ethan@stoneleaf.us>
parents:
84713
diff
changeset
|
1111 blue = () |
|
37c427e3eb94
Close #18508 -- fix _value2member_map to always have the member's value
Ethan Furman <ethan@stoneleaf.us>
parents:
84713
diff
changeset
|
1112 self.assertEqual(list(ColorInAList), [ColorInAList.red, ColorInAList.green, ColorInAList.blue]) |
|
37c427e3eb94
Close #18508 -- fix _value2member_map to always have the member's value
Ethan Furman <ethan@stoneleaf.us>
parents:
84713
diff
changeset
|
1113 self.assertEqual(ColorInAList.red.value, [1]) |
|
37c427e3eb94
Close #18508 -- fix _value2member_map to always have the member's value
Ethan Furman <ethan@stoneleaf.us>
parents:
84713
diff
changeset
|
1114 self.assertEqual(ColorInAList([1]), ColorInAList.red) |
|
37c427e3eb94
Close #18508 -- fix _value2member_map to always have the member's value
Ethan Furman <ethan@stoneleaf.us>
parents:
84713
diff
changeset
|
1115 |
|
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
|
1116 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
|
1117 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
|
1118 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
|
1119 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
|
1120 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
|
1121 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
|
1122 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
|
1123 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
|
1124 |
|
95e1d0efd896
Close #18545: now only executes member_type if no _value_ is assigned in __new__.
Ethan Furman <ethan@stoneleaf.us>
parents:
84721
diff
changeset
|
1125 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
|
1126 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
|
1127 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
|
1128 |
|
95e1d0efd896
Close #18545: now only executes member_type if no _value_ is assigned in __new__.
Ethan Furman <ethan@stoneleaf.us>
parents:
84721
diff
changeset
|
1129 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
|
1130 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
|
1131 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
|
1132 |
|
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
|
1133 |
|
84713
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1134 class TestUnique(unittest.TestCase): |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1135 |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1136 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
|
1137 @unique |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1138 class Clean(Enum): |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1139 one = 1 |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1140 two = 'dos' |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1141 tres = 4.0 |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1142 @unique |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1143 class Cleaner(IntEnum): |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1144 single = 1 |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1145 double = 2 |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1146 triple = 3 |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1147 |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1148 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
|
1149 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
|
1150 @unique |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1151 class Dirty(Enum): |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1152 one = 1 |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1153 two = 'dos' |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1154 tres = 1 |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1155 with self.assertRaisesRegex( |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1156 ValueError, |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1157 'double.*single.*turkey.*triple', |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1158 ): |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1159 @unique |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1160 class Dirtier(IntEnum): |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1161 single = 1 |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1162 double = 1 |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1163 triple = 3 |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1164 turkey = 3 |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1165 |
|
2079a517193b
closes issue18042 -- a `unique` decorator is added to enum.py
Ethan Furman <ethan@stoneleaf.us>
parents:
84130
diff
changeset
|
1166 |
|
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
|
1167 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
|
1168 unittest.main() |
