MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_subclasses_with_getnewargs_ex

Method test_subclasses_with_getnewargs_ex

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

Source from the content-addressed store, hash-verified

2108 test_pickle_dump_load(self.assertIs, NEI)
2109
2110 def test_subclasses_with_getnewargs_ex(self):
2111 class NamedInt(int):
2112 __qualname__ = 'NamedInt' # needed for pickle protocol 4
2113 def __new__(cls, *args):
2114 _args = args
2115 name, *args = args
2116 if len(args) == 0:
2117 raise TypeError("name and value must be specified")
2118 self = int.__new__(cls, *args)
2119 self._intname = name
2120 self._args = _args
2121 return self
2122 def __getnewargs_ex__(self):
2123 return self._args, {}
2124 @bltns.property
2125 def __name__(self):
2126 return self._intname
2127 def __repr__(self):
2128 # repr() is updated to include the name and type info
2129 return "{}({!r}, {})".format(
2130 type(self).__name__,
2131 self.__name__,
2132 int.__repr__(self),
2133 )
2134 def __str__(self):
2135 # str() is unchanged, even if it relies on the repr() fallback
2136 base = int
2137 base_str = base.__str__
2138 if base_str.__objclass__ is object:
2139 return base.__repr__(self)
2140 return base_str(self)
2141 # for simplicity, we only define one operator that
2142 # propagates expressions
2143 def __add__(self, other):
2144 temp = int(self) + int( other)
2145 if isinstance(self, NamedInt) and isinstance(other, NamedInt):
2146 return NamedInt(
2147 '({0} + {1})'.format(self.__name__, other.__name__),
2148 temp,
2149 )
2150 else:
2151 return temp
2152
2153 class NEI(NamedInt, Enum):
2154 __qualname__ = 'NEI' # needed for pickle protocol 4
2155 x = ('the-x', 1)
2156 y = ('the-y', 2)
2157
2158
2159 self.assertIs(NEI.__new__, Enum.__new__)
2160 self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)")
2161 globals()['NamedInt'] = NamedInt
2162 globals()['NEI'] = NEI
2163 NI5 = NamedInt('test', 5)
2164 self.assertEqual(NI5, 5)
2165 test_pickle_dump_load(self.assertEqual, NI5, 5)
2166 self.assertEqual(NEI.y.value, 2)
2167 test_pickle_dump_load(self.assertIs, NEI.y)

Callers

nothing calls this directly

Calls 6

reprFunction · 0.85
globalsFunction · 0.85
NamedIntClass · 0.85
test_pickle_dump_loadFunction · 0.85
assertIsMethod · 0.45
assertEqualMethod · 0.45

Tested by

no test coverage detected