Created
December 11, 2025 13:52
-
-
Save pschanely/947c8bf8f0fc52c7b9d36c8e26bee523 to your computer and use it in GitHub Desktop.
Shared via CrossHair Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from dataclasses import dataclass | |
| class HasConsistentHash: | |
| ''' | |
| A mixin to enforce that classes have hash methods that are consistent | |
| with thier equality checks. | |
| ''' | |
| def __eq__(self, other: object) -> bool: | |
| ''' | |
| post: implies(__return__, hash(self) == hash(other)) | |
| ''' | |
| raise NotImplementedError | |
| @dataclass | |
| class Apples(HasConsistentHash): | |
| ''' | |
| Uses HasConsistentHash to discover that the __eq__ method is | |
| missing a test for the `count` attribute. | |
| ''' | |
| count: int | |
| kind: str | |
| def __hash__(self): | |
| return self.count + hash(self.kind) | |
| def __eq__(self, other: object) -> bool: | |
| return isinstance(other, Apples) and self.kind == other.kind | |
| def __repr__(self): | |
| return f'Apples({self.count!r}, {self.kind!r})' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment