@@ -122,6 +122,22 @@ def test_is_dunder(self):
122
122
'__' , '___' , '____' , '_____' ,):
123
123
self .assertFalse (enum ._is_dunder (s ))
124
124
125
+ # for subclassing tests
126
+
127
+ class classproperty :
128
+
129
+ def __init__ (self , fget = None , fset = None , fdel = None , doc = None ):
130
+ self .fget = fget
131
+ self .fset = fset
132
+ self .fdel = fdel
133
+ if doc is None and fget is not None :
134
+ doc = fget .__doc__
135
+ self .__doc__ = doc
136
+
137
+ def __get__ (self , instance , ownerclass ):
138
+ return self .fget (ownerclass )
139
+
140
+
125
141
# tests
126
142
127
143
class TestEnum (unittest .TestCase ):
@@ -1730,6 +1746,102 @@ def _missing_(cls, item):
1730
1746
else :
1731
1747
raise Exception ('Exception not raised.' )
1732
1748
1749
+ def test_multiple_mixin (self ):
1750
+ class MaxMixin :
1751
+ @classproperty
1752
+ def MAX (cls ):
1753
+ max = len (cls )
1754
+ cls .MAX = max
1755
+ return max
1756
+ class StrMixin :
1757
+ def __str__ (self ):
1758
+ return self ._name_ .lower ()
1759
+ class SomeEnum (Enum ):
1760
+ def behavior (self ):
1761
+ return 'booyah'
1762
+ class AnotherEnum (Enum ):
1763
+ def behavior (self ):
1764
+ return 'nuhuh!'
1765
+ def social (self ):
1766
+ return "what's up?"
1767
+ class Color (MaxMixin , Enum ):
1768
+ RED = auto ()
1769
+ GREEN = auto ()
1770
+ BLUE = auto ()
1771
+ self .assertEqual (Color .RED .value , 1 )
1772
+ self .assertEqual (Color .GREEN .value , 2 )
1773
+ self .assertEqual (Color .BLUE .value , 3 )
1774
+ self .assertEqual (Color .MAX , 3 )
1775
+ self .assertEqual (str (Color .BLUE ), 'Color.BLUE' )
1776
+ class Color (MaxMixin , StrMixin , Enum ):
1777
+ RED = auto ()
1778
+ GREEN = auto ()
1779
+ BLUE = auto ()
1780
+ self .assertEqual (Color .RED .value , 1 )
1781
+ self .assertEqual (Color .GREEN .value , 2 )
1782
+ self .assertEqual (Color .BLUE .value , 3 )
1783
+ self .assertEqual (Color .MAX , 3 )
1784
+ self .assertEqual (str (Color .BLUE ), 'blue' )
1785
+ class Color (StrMixin , MaxMixin , Enum ):
1786
+ RED = auto ()
1787
+ GREEN = auto ()
1788
+ BLUE = auto ()
1789
+ self .assertEqual (Color .RED .value , 1 )
1790
+ self .assertEqual (Color .GREEN .value , 2 )
1791
+ self .assertEqual (Color .BLUE .value , 3 )
1792
+ self .assertEqual (Color .MAX , 3 )
1793
+ self .assertEqual (str (Color .BLUE ), 'blue' )
1794
+ class CoolColor (StrMixin , SomeEnum , Enum ):
1795
+ RED = auto ()
1796
+ GREEN = auto ()
1797
+ BLUE = auto ()
1798
+ self .assertEqual (CoolColor .RED .value , 1 )
1799
+ self .assertEqual (CoolColor .GREEN .value , 2 )
1800
+ self .assertEqual (CoolColor .BLUE .value , 3 )
1801
+ self .assertEqual (str (CoolColor .BLUE ), 'blue' )
1802
+ self .assertEqual (CoolColor .RED .behavior (), 'booyah' )
1803
+ class CoolerColor (StrMixin , AnotherEnum , Enum ):
1804
+ RED = auto ()
1805
+ GREEN = auto ()
1806
+ BLUE = auto ()
1807
+ self .assertEqual (CoolerColor .RED .value , 1 )
1808
+ self .assertEqual (CoolerColor .GREEN .value , 2 )
1809
+ self .assertEqual (CoolerColor .BLUE .value , 3 )
1810
+ self .assertEqual (str (CoolerColor .BLUE ), 'blue' )
1811
+ self .assertEqual (CoolerColor .RED .behavior (), 'nuhuh!' )
1812
+ self .assertEqual (CoolerColor .RED .social (), "what's up?" )
1813
+ class CoolestColor (StrMixin , SomeEnum , AnotherEnum ):
1814
+ RED = auto ()
1815
+ GREEN = auto ()
1816
+ BLUE = auto ()
1817
+ self .assertEqual (CoolestColor .RED .value , 1 )
1818
+ self .assertEqual (CoolestColor .GREEN .value , 2 )
1819
+ self .assertEqual (CoolestColor .BLUE .value , 3 )
1820
+ self .assertEqual (str (CoolestColor .BLUE ), 'blue' )
1821
+ self .assertEqual (CoolestColor .RED .behavior (), 'booyah' )
1822
+ self .assertEqual (CoolestColor .RED .social (), "what's up?" )
1823
+ class ConfusedColor (StrMixin , AnotherEnum , SomeEnum ):
1824
+ RED = auto ()
1825
+ GREEN = auto ()
1826
+ BLUE = auto ()
1827
+ self .assertEqual (ConfusedColor .RED .value , 1 )
1828
+ self .assertEqual (ConfusedColor .GREEN .value , 2 )
1829
+ self .assertEqual (ConfusedColor .BLUE .value , 3 )
1830
+ self .assertEqual (str (ConfusedColor .BLUE ), 'blue' )
1831
+ self .assertEqual (ConfusedColor .RED .behavior (), 'nuhuh!' )
1832
+ self .assertEqual (ConfusedColor .RED .social (), "what's up?" )
1833
+ class ReformedColor (StrMixin , IntEnum , SomeEnum , AnotherEnum ):
1834
+ RED = auto ()
1835
+ GREEN = auto ()
1836
+ BLUE = auto ()
1837
+ self .assertEqual (ReformedColor .RED .value , 1 )
1838
+ self .assertEqual (ReformedColor .GREEN .value , 2 )
1839
+ self .assertEqual (ReformedColor .BLUE .value , 3 )
1840
+ self .assertEqual (str (ReformedColor .BLUE ), 'blue' )
1841
+ self .assertEqual (ReformedColor .RED .behavior (), 'booyah' )
1842
+ self .assertEqual (ConfusedColor .RED .social (), "what's up?" )
1843
+ self .assertTrue (issubclass (ReformedColor , int ))
1844
+
1733
1845
1734
1846
class TestOrder (unittest .TestCase ):
1735
1847
@@ -2093,6 +2205,49 @@ class Bizarre(Flag):
2093
2205
d = 6
2094
2206
self .assertEqual (repr (Bizarre (7 )), '<Bizarre.d|c|b: 7>' )
2095
2207
2208
+ def test_multiple_mixin (self ):
2209
+ class AllMixin :
2210
+ @classproperty
2211
+ def ALL (cls ):
2212
+ members = list (cls )
2213
+ all_value = None
2214
+ if members :
2215
+ all_value = members [0 ]
2216
+ for member in members [1 :]:
2217
+ all_value |= member
2218
+ cls .ALL = all_value
2219
+ return all_value
2220
+ class StrMixin :
2221
+ def __str__ (self ):
2222
+ return self ._name_ .lower ()
2223
+ class Color (AllMixin , Flag ):
2224
+ RED = auto ()
2225
+ GREEN = auto ()
2226
+ BLUE = auto ()
2227
+ self .assertEqual (Color .RED .value , 1 )
2228
+ self .assertEqual (Color .GREEN .value , 2 )
2229
+ self .assertEqual (Color .BLUE .value , 4 )
2230
+ self .assertEqual (Color .ALL .value , 7 )
2231
+ self .assertEqual (str (Color .BLUE ), 'Color.BLUE' )
2232
+ class Color (AllMixin , StrMixin , Flag ):
2233
+ RED = auto ()
2234
+ GREEN = auto ()
2235
+ BLUE = auto ()
2236
+ self .assertEqual (Color .RED .value , 1 )
2237
+ self .assertEqual (Color .GREEN .value , 2 )
2238
+ self .assertEqual (Color .BLUE .value , 4 )
2239
+ self .assertEqual (Color .ALL .value , 7 )
2240
+ self .assertEqual (str (Color .BLUE ), 'blue' )
2241
+ class Color (StrMixin , AllMixin , Flag ):
2242
+ RED = auto ()
2243
+ GREEN = auto ()
2244
+ BLUE = auto ()
2245
+ self .assertEqual (Color .RED .value , 1 )
2246
+ self .assertEqual (Color .GREEN .value , 2 )
2247
+ self .assertEqual (Color .BLUE .value , 4 )
2248
+ self .assertEqual (Color .ALL .value , 7 )
2249
+ self .assertEqual (str (Color .BLUE ), 'blue' )
2250
+
2096
2251
@support .reap_threads
2097
2252
def test_unique_composite (self ):
2098
2253
# override __eq__ to be identity only
@@ -2468,6 +2623,49 @@ def test_bool(self):
2468
2623
for f in Open :
2469
2624
self .assertEqual (bool (f .value ), bool (f ))
2470
2625
2626
+ def test_multiple_mixin (self ):
2627
+ class AllMixin :
2628
+ @classproperty
2629
+ def ALL (cls ):
2630
+ members = list (cls )
2631
+ all_value = None
2632
+ if members :
2633
+ all_value = members [0 ]
2634
+ for member in members [1 :]:
2635
+ all_value |= member
2636
+ cls .ALL = all_value
2637
+ return all_value
2638
+ class StrMixin :
2639
+ def __str__ (self ):
2640
+ return self ._name_ .lower ()
2641
+ class Color (AllMixin , IntFlag ):
2642
+ RED = auto ()
2643
+ GREEN = auto ()
2644
+ BLUE = auto ()
2645
+ self .assertEqual (Color .RED .value , 1 )
2646
+ self .assertEqual (Color .GREEN .value , 2 )
2647
+ self .assertEqual (Color .BLUE .value , 4 )
2648
+ self .assertEqual (Color .ALL .value , 7 )
2649
+ self .assertEqual (str (Color .BLUE ), 'Color.BLUE' )
2650
+ class Color (AllMixin , StrMixin , IntFlag ):
2651
+ RED = auto ()
2652
+ GREEN = auto ()
2653
+ BLUE = auto ()
2654
+ self .assertEqual (Color .RED .value , 1 )
2655
+ self .assertEqual (Color .GREEN .value , 2 )
2656
+ self .assertEqual (Color .BLUE .value , 4 )
2657
+ self .assertEqual (Color .ALL .value , 7 )
2658
+ self .assertEqual (str (Color .BLUE ), 'blue' )
2659
+ class Color (StrMixin , AllMixin , IntFlag ):
2660
+ RED = auto ()
2661
+ GREEN = auto ()
2662
+ BLUE = auto ()
2663
+ self .assertEqual (Color .RED .value , 1 )
2664
+ self .assertEqual (Color .GREEN .value , 2 )
2665
+ self .assertEqual (Color .BLUE .value , 4 )
2666
+ self .assertEqual (Color .ALL .value , 7 )
2667
+ self .assertEqual (str (Color .BLUE ), 'blue' )
2668
+
2471
2669
@support .reap_threads
2472
2670
def test_unique_composite (self ):
2473
2671
# override __eq__ to be identity only
@@ -2553,6 +2751,7 @@ class Sillier(IntEnum):
2553
2751
value = 4
2554
2752
2555
2753
2754
+
2556
2755
expected_help_output_with_docs = """\
2557
2756
Help on class Color in module %s:
2558
2757
0 commit comments