(self)
| 2048 | c = ... |
| 2049 | |
| 2050 | def test_subclasses_with_getnewargs(self): |
| 2051 | class NamedInt(int): |
| 2052 | __qualname__ = 'NamedInt' # needed for pickle protocol 4 |
| 2053 | def __new__(cls, *args): |
| 2054 | _args = args |
| 2055 | name, *args = args |
| 2056 | if len(args) == 0: |
| 2057 | raise TypeError("name and value must be specified") |
| 2058 | self = int.__new__(cls, *args) |
| 2059 | self._intname = name |
| 2060 | self._args = _args |
| 2061 | return self |
| 2062 | def __getnewargs__(self): |
| 2063 | return self._args |
| 2064 | @bltns.property |
| 2065 | def __name__(self): |
| 2066 | return self._intname |
| 2067 | def __repr__(self): |
| 2068 | # repr() is updated to include the name and type info |
| 2069 | return "{}({!r}, {})".format( |
| 2070 | type(self).__name__, |
| 2071 | self.__name__, |
| 2072 | int.__repr__(self), |
| 2073 | ) |
| 2074 | def __str__(self): |
| 2075 | # str() is unchanged, even if it relies on the repr() fallback |
| 2076 | base = int |
| 2077 | base_str = base.__str__ |
| 2078 | if base_str.__objclass__ is object: |
| 2079 | return base.__repr__(self) |
| 2080 | return base_str(self) |
| 2081 | # for simplicity, we only define one operator that |
| 2082 | # propagates expressions |
| 2083 | def __add__(self, other): |
| 2084 | temp = int(self) + int( other) |
| 2085 | if isinstance(self, NamedInt) and isinstance(other, NamedInt): |
| 2086 | return NamedInt( |
| 2087 | '({0} + {1})'.format(self.__name__, other.__name__), |
| 2088 | temp, |
| 2089 | ) |
| 2090 | else: |
| 2091 | return temp |
| 2092 | |
| 2093 | class NEI(NamedInt, Enum): |
| 2094 | __qualname__ = 'NEI' # needed for pickle protocol 4 |
| 2095 | x = ('the-x', 1) |
| 2096 | y = ('the-y', 2) |
| 2097 | |
| 2098 | |
| 2099 | self.assertIs(NEI.__new__, Enum.__new__) |
| 2100 | self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)") |
| 2101 | globals()['NamedInt'] = NamedInt |
| 2102 | globals()['NEI'] = NEI |
| 2103 | NI5 = NamedInt('test', 5) |
| 2104 | self.assertEqual(NI5, 5) |
| 2105 | test_pickle_dump_load(self.assertEqual, NI5, 5) |
| 2106 | self.assertEqual(NEI.y.value, 2) |
| 2107 | test_pickle_dump_load(self.assertIs, NEI.y) |
nothing calls this directly
no test coverage detected