MCPcopy Index your code
hub / github.com/python/cpython / _test_simple_enum

Function _test_simple_enum

Lib/enum.py:2047–2167  ·  view source on GitHub ↗

A function that can be used to test an enum created with :func:`_simple_enum` against the version created by subclassing :class:`Enum`:: >>> from enum import Enum, _simple_enum, _test_simple_enum >>> @_simple_enum(Enum) ... class Color: ... RED = auto()

(checked_enum, simple_enum)

Source from the content-addressed store, hash-verified

2045 return enumeration
2046
2047def _test_simple_enum(checked_enum, simple_enum):
2048 """
2049 A function that can be used to test an enum created with :func:`_simple_enum`
2050 against the version created by subclassing :class:`Enum`::
2051
2052 >>> from enum import Enum, _simple_enum, _test_simple_enum
2053 >>> @_simple_enum(Enum)
2054 ... class Color:
2055 ... RED = auto()
2056 ... GREEN = auto()
2057 ... BLUE = auto()
2058 >>> class CheckedColor(Enum):
2059 ... RED = auto()
2060 ... GREEN = auto()
2061 ... BLUE = auto()
2062 >>> _test_simple_enum(CheckedColor, Color)
2063
2064 If differences are found, a :exc:`TypeError` is raised.
2065 """
2066 failed = []
2067 if checked_enum.__dict__ != simple_enum.__dict__:
2068 checked_dict = checked_enum.__dict__
2069 checked_keys = list(checked_dict.keys())
2070 simple_dict = simple_enum.__dict__
2071 simple_keys = list(simple_dict.keys())
2072 member_names = set(
2073 list(checked_enum._member_map_.keys())
2074 + list(simple_enum._member_map_.keys())
2075 )
2076 for key in set(checked_keys + simple_keys):
2077 if key in ('__module__', '_member_map_', '_value2member_map_', '__doc__',
2078 '__static_attributes__', '__firstlineno__'):
2079 # keys known to be different, or very long
2080 continue
2081 elif key in member_names:
2082 # members are checked below
2083 continue
2084 elif key not in simple_keys:
2085 failed.append("missing key: %r" % (key, ))
2086 elif key not in checked_keys:
2087 failed.append("extra key: %r" % (key, ))
2088 else:
2089 checked_value = checked_dict[key]
2090 simple_value = simple_dict[key]
2091 if callable(checked_value) or isinstance(checked_value, bltns.property):
2092 continue
2093 if key == '__doc__':
2094 # remove all spaces/tabs
2095 compressed_checked_value = checked_value.replace(' ','').replace('\t','')
2096 compressed_simple_value = simple_value.replace(' ','').replace('\t','')
2097 if compressed_checked_value != compressed_simple_value:
2098 failed.append("%r:\n %s\n %s" % (
2099 key,
2100 "checked -> %r" % (checked_value, ),
2101 "simple -> %r" % (simple_value, ),
2102 ))
2103 elif checked_value != simple_value:
2104 failed.append("%r:\n %s\n %s" % (

Callers 2

test_sort_stats_enumMethod · 0.90
test_test_simple_enumMethod · 0.90

Calls 7

listClass · 0.85
setFunction · 0.85
keysMethod · 0.45
appendMethod · 0.45
replaceMethod · 0.45
sortMethod · 0.45
joinMethod · 0.45

Tested by 2

test_sort_stats_enumMethod · 0.72
test_test_simple_enumMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…