Skip to content

Commit

Permalink
fix: Variabel hash is equal for objects which __eq__ returns True.
Browse files Browse the repository at this point in the history
  • Loading branch information
NelDav committed Apr 4, 2024
1 parent 4d8d268 commit 605b5cd
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions uncertainties/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2742,7 +2742,7 @@ def __init__(self, value, std_dev, tag=None):
# differentiable functions: for instance, Variable(3, 0.1)/2
# has a nominal value of 3/2 = 1, but a "shifted" value
# of 3.1/2 = 1.55.
value = float(value)
self._nominal_value = float(value)

# If the variable changes by dx, then the value of the affine
# function that gives its value changes by 1*dx:
Expand All @@ -2752,7 +2752,7 @@ def __init__(self, value, std_dev, tag=None):
# takes much more memory. Thus, this implementation chooses
# more cycles and a smaller memory footprint instead of no
# cycles and a larger memory footprint.
super(Variable, self).__init__(value, LinearCombination({self: 1.}))
super(Variable, self).__init__(self._nominal_value, LinearCombination({self: 1.}))

self.std_dev = std_dev # Assignment through a Python property

Expand Down Expand Up @@ -2789,8 +2789,11 @@ def __hash__(self):
Returns:
int: The hash of this object
"""
# Otherwise, pickles loads does not work
if not hasattr(self, "_nominal_value"):
self._nominal_value = None

return hash(id(self))
return hash((self._nominal_value, (id(self),), (1.,)))

# The following method is overridden so that we can represent the tag:
def __repr__(self):
Expand Down

0 comments on commit 605b5cd

Please sign in to comment.