Skip to content

Commit

Permalink
jsondiff: fix symbol equality
Browse files Browse the repository at this point in the history
In xlwings#55 an equality method was introduced without a corresponding hash
method. Hashable objects that implement __eq__ must also implement
__hash__ if the type is immutable [1]. Implement hash using the label
field which is now a property in order to convey the intent of
immutability.

Fixes xlwings#58

[1] https://docs.python.org/3/reference/datamodel.html#object.__hash__

Signed-off-by: Cory Todd <cory.todd@canonical.com>
  • Loading branch information
Cory Todd committed Jun 2, 2023
1 parent 610689a commit 68a9b41
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion jsondiff/symbols.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@

class Symbol(object):
def __init__(self, label):
self.label = label
self._label = label

@property
def label(self):
return self._label

def __repr__(self):
return self.label
Expand All @@ -11,6 +15,9 @@ def __str__(self):

def __eq__(self, other):
return self.label == other.label

def __hash__(self) -> int:
return hash(self.label)

missing = Symbol('missing')
identical = Symbol('identical')
Expand Down

0 comments on commit 68a9b41

Please sign in to comment.