(self)
| 175 | x: int = 0 |
| 176 | |
| 177 | def test_overwrite_hash(self): |
| 178 | # Test that declaring this class isn't an error. It should |
| 179 | # use the user-provided __hash__. |
| 180 | @dataclass(frozen=True) |
| 181 | class C: |
| 182 | x: int |
| 183 | def __hash__(self): |
| 184 | return 301 |
| 185 | self.assertEqual(hash(C(100)), 301) |
| 186 | |
| 187 | # Test that declaring this class isn't an error. It should |
| 188 | # use the generated __hash__. |
| 189 | @dataclass(frozen=True) |
| 190 | class C: |
| 191 | x: int |
| 192 | def __eq__(self, other): |
| 193 | return False |
| 194 | self.assertEqual(hash(C(100)), hash((100,))) |
| 195 | |
| 196 | # But this one should generate an exception, because with |
| 197 | # unsafe_hash=True, it's an error to have a __hash__ defined. |
| 198 | with self.assertRaisesRegex(TypeError, |
| 199 | 'Cannot overwrite attribute __hash__'): |
| 200 | @dataclass(unsafe_hash=True) |
| 201 | class C: |
| 202 | def __hash__(self): |
| 203 | pass |
| 204 | |
| 205 | # Creating this class should not generate an exception, |
| 206 | # because even though __hash__ exists before @dataclass is |
| 207 | # called, (due to __eq__ being defined), since it's None |
| 208 | # that's okay. |
| 209 | @dataclass(unsafe_hash=True) |
| 210 | class C: |
| 211 | x: int |
| 212 | def __eq__(self): |
| 213 | pass |
| 214 | # The generated hash function works as we'd expect. |
| 215 | self.assertEqual(hash(C(10)), hash((10,))) |
| 216 | |
| 217 | # Creating this class should generate an exception, because |
| 218 | # __hash__ exists and is not None, which it would be if it |
| 219 | # had been auto-generated due to __eq__ being defined. |
| 220 | with self.assertRaisesRegex(TypeError, |
| 221 | 'Cannot overwrite attribute __hash__'): |
| 222 | @dataclass(unsafe_hash=True) |
| 223 | class C: |
| 224 | x: int |
| 225 | def __eq__(self): |
| 226 | pass |
| 227 | def __hash__(self): |
| 228 | pass |
| 229 | |
| 230 | def test_overwrite_fields_in_derived_class(self): |
| 231 | # Note that x from C1 replaces x in Base, but the order remains |
nothing calls this directly
no test coverage detected