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

Method test_subclasses_with_getnewargs_ex

Lib/test/test_enum.py:2140–2198  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

2138 test_pickle_dump_load(self.assertIs, NEI)
2139
2140 def test_subclasses_with_getnewargs_ex(self):
2141 class NamedInt(int):
2142 __qualname__ = 'NamedInt' # needed for pickle protocol 4
2143 def __new__(cls, *args):
2144 _args = args
2145 name, *args = args
2146 if len(args) == 0:
2147 raise TypeError("name and value must be specified")
2148 self = int.__new__(cls, *args)
2149 self._intname = name
2150 self._args = _args
2151 return self
2152 def __getnewargs_ex__(self):
2153 return self._args, {}
2154 @bltns.property
2155 def __name__(self):
2156 return self._intname
2157 def __repr__(self):
2158 # repr() is updated to include the name and type info
2159 return "{}({!r}, {})".format(
2160 type(self).__name__,
2161 self.__name__,
2162 int.__repr__(self),
2163 )
2164 def __str__(self):
2165 # str() is unchanged, even if it relies on the repr() fallback
2166 base = int
2167 base_str = base.__str__
2168 if base_str.__objclass__ is object:
2169 return base.__repr__(self)
2170 return base_str(self)
2171 # for simplicity, we only define one operator that
2172 # propagates expressions
2173 def __add__(self, other):
2174 temp = int(self) + int( other)
2175 if isinstance(self, NamedInt) and isinstance(other, NamedInt):
2176 return NamedInt(
2177 '({0} + {1})'.format(self.__name__, other.__name__),
2178 temp,
2179 )
2180 else:
2181 return temp
2182
2183 class NEI(NamedInt, Enum):
2184 __qualname__ = 'NEI' # needed for pickle protocol 4
2185 x = ('the-x', 1)
2186 y = ('the-y', 2)
2187
2188
2189 self.assertIs(NEI.__new__, Enum.__new__)
2190 self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)")
2191 globals()['NamedInt'] = NamedInt
2192 globals()['NEI'] = NEI
2193 NI5 = NamedInt('test', 5)
2194 self.assertEqual(NI5, 5)
2195 test_pickle_dump_load(self.assertEqual, NI5, 5)
2196 self.assertEqual(NEI.y.value, 2)
2197 test_pickle_dump_load(self.assertIs, NEI.y)

Callers

nothing calls this directly

Calls 4

NamedIntClass · 0.85
test_pickle_dump_loadFunction · 0.85
assertIsMethod · 0.45
assertEqualMethod · 0.45

Tested by

no test coverage detected