(self)
| 2078 | c = ... |
| 2079 | |
| 2080 | def test_subclasses_with_getnewargs(self): |
| 2081 | class NamedInt(int): |
| 2082 | __qualname__ = 'NamedInt' # needed for pickle protocol 4 |
| 2083 | def __new__(cls, *args): |
| 2084 | _args = args |
| 2085 | name, *args = args |
| 2086 | if len(args) == 0: |
| 2087 | raise TypeError("name and value must be specified") |
| 2088 | self = int.__new__(cls, *args) |
| 2089 | self._intname = name |
| 2090 | self._args = _args |
| 2091 | return self |
| 2092 | def __getnewargs__(self): |
| 2093 | return self._args |
| 2094 | @bltns.property |
| 2095 | def __name__(self): |
| 2096 | return self._intname |
| 2097 | def __repr__(self): |
| 2098 | # repr() is updated to include the name and type info |
| 2099 | return "{}({!r}, {})".format( |
| 2100 | type(self).__name__, |
| 2101 | self.__name__, |
| 2102 | int.__repr__(self), |
| 2103 | ) |
| 2104 | def __str__(self): |
| 2105 | # str() is unchanged, even if it relies on the repr() fallback |
| 2106 | base = int |
| 2107 | base_str = base.__str__ |
| 2108 | if base_str.__objclass__ is object: |
| 2109 | return base.__repr__(self) |
| 2110 | return base_str(self) |
| 2111 | # for simplicity, we only define one operator that |
| 2112 | # propagates expressions |
| 2113 | def __add__(self, other): |
| 2114 | temp = int(self) + int( other) |
| 2115 | if isinstance(self, NamedInt) and isinstance(other, NamedInt): |
| 2116 | return NamedInt( |
| 2117 | '({0} + {1})'.format(self.__name__, other.__name__), |
| 2118 | temp, |
| 2119 | ) |
| 2120 | else: |
| 2121 | return temp |
| 2122 | |
| 2123 | class NEI(NamedInt, Enum): |
| 2124 | __qualname__ = 'NEI' # needed for pickle protocol 4 |
| 2125 | x = ('the-x', 1) |
| 2126 | y = ('the-y', 2) |
| 2127 | |
| 2128 | |
| 2129 | self.assertIs(NEI.__new__, Enum.__new__) |
| 2130 | self.assertEqual(repr(NEI.x + NEI.y), "NamedInt('(the-x + the-y)', 3)") |
| 2131 | globals()['NamedInt'] = NamedInt |
| 2132 | globals()['NEI'] = NEI |
| 2133 | NI5 = NamedInt('test', 5) |
| 2134 | self.assertEqual(NI5, 5) |
| 2135 | test_pickle_dump_load(self.assertEqual, NI5, 5) |
| 2136 | self.assertEqual(NEI.y.value, 2) |
| 2137 | test_pickle_dump_load(self.assertIs, NEI.y) |
nothing calls this directly
no test coverage detected