Skip to content

Instantly share code, notes, and snippets.

@pschanely
Created February 7, 2020 19:37
Show Gist options
  • Save pschanely/3a4bd78de538dafd4bad1d7916a59a84 to your computer and use it in GitHub Desktop.
Save pschanely/3a4bd78de538dafd4bad1d7916a59a84 to your computer and use it in GitHub Desktop.
Shared via CrossHair Playground
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
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 __repr__(self):
return f'Apples({self.count!r}, {self.kind!r})'
def __eq__(self, other: object) -> bool:
return isinstance(other, Apples) and self.kind == other.kind
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment