55import unittest
66import threading
77from collections import OrderedDict
8- from enum import Enum , IntEnum , EnumMeta , Flag , IntFlag , unique , auto
8+ from enum import Enum , IntEnum , StrEnum , EnumMeta , Flag , IntFlag , unique , auto
99from io import StringIO
1010from pickle import dumps , loads , PicklingError , HIGHEST_PROTOCOL
1111from test import support
@@ -48,14 +48,9 @@ class FlagStooges(Flag):
4848 FlagStooges = exc
4949
5050# for pickle test and subclass tests
51- try :
52- class StrEnum (str , Enum ):
53- 'accepts only string values'
54- class Name (StrEnum ):
55- BDFL = 'Guido van Rossum'
56- FLUFL = 'Barry Warsaw'
57- except Exception as exc :
58- Name = exc
51+ class Name (StrEnum ):
52+ BDFL = 'Guido van Rossum'
53+ FLUFL = 'Barry Warsaw'
5954
6055try :
6156 Question = Enum ('Question' , 'who what when where why' , module = __name__ )
@@ -665,14 +660,13 @@ class phy(str, Enum):
665660 tau = 'Tau'
666661 self .assertTrue (phy .pi < phy .tau )
667662
668- def test_strenum_inherited (self ):
669- class StrEnum (str , Enum ):
670- pass
663+ def test_strenum_inherited_methods (self ):
671664 class phy (StrEnum ):
672665 pi = 'Pi'
673666 tau = 'Tau'
674667 self .assertTrue (phy .pi < phy .tau )
675-
668+ self .assertEqual (phy .pi .upper (), 'PI' )
669+ self .assertEqual (phy .tau .count ('a' ), 1 )
676670
677671 def test_intenum (self ):
678672 class WeekDay (IntEnum ):
@@ -2014,13 +2008,6 @@ class ReformedColor(StrMixin, IntEnum, SomeEnum, AnotherEnum):
20142008 self .assertTrue (issubclass (ReformedColor , int ))
20152009
20162010 def test_multiple_inherited_mixin (self ):
2017- class StrEnum (str , Enum ):
2018- def __new__ (cls , * args , ** kwargs ):
2019- for a in args :
2020- if not isinstance (a , str ):
2021- raise TypeError ("Enumeration '%s' (%s) is not"
2022- " a string" % (a , type (a ).__name__ ))
2023- return str .__new__ (cls , * args , ** kwargs )
20242011 @unique
20252012 class Decision1 (StrEnum ):
20262013 REVERT = "REVERT"
@@ -2043,6 +2030,33 @@ def test_empty_globals(self):
20432030 local_ls = {}
20442031 exec (code , global_ns , local_ls )
20452032
2033+ def test_strenum (self ):
2034+ class GoodStrEnum (StrEnum ):
2035+ one = '1'
2036+ two = '2'
2037+ three = b'3' , 'ascii'
2038+ four = b'4' , 'latin1' , 'strict'
2039+ with self .assertRaisesRegex (TypeError , '1 is not a string' ):
2040+ class FirstFailedStrEnum (StrEnum ):
2041+ one = 1
2042+ two = '2'
2043+ with self .assertRaisesRegex (TypeError , "2 is not a string" ):
2044+ class SecondFailedStrEnum (StrEnum ):
2045+ one = '1'
2046+ two = 2 ,
2047+ three = '3'
2048+ with self .assertRaisesRegex (TypeError , '2 is not a string' ):
2049+ class ThirdFailedStrEnum (StrEnum ):
2050+ one = '1'
2051+ two = 2
2052+ with self .assertRaisesRegex (TypeError , 'encoding must be a string, not %r' % (sys .getdefaultencoding , )):
2053+ class ThirdFailedStrEnum (StrEnum ):
2054+ one = '1'
2055+ two = b'2' , sys .getdefaultencoding
2056+ with self .assertRaisesRegex (TypeError , 'errors must be a string, not 9' ):
2057+ class ThirdFailedStrEnum (StrEnum ):
2058+ one = '1'
2059+ two = b'2' , 'ascii' , 9
20462060
20472061class TestOrder (unittest .TestCase ):
20482062
0 commit comments