Skip to content

Commit

Permalink
Increase robustness of sentinel equality checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewwardrop committed Dec 17, 2024
1 parent 5412373 commit 3d35979
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
6 changes: 6 additions & 0 deletions spec_classes/types/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ def __bool__(cls):
def __call__(cls):
return cls

def __hash__(cls):
return id(cls)

def __eq__(cls, other):
return cls is other


class MISSING(metaclass=_MissingType):
"""
Expand Down
3 changes: 2 additions & 1 deletion spec_classes/types/spec_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from cached_property import cached_property

from spec_classes.errors import NestedAttributeError
from spec_classes.types.missing import EMPTY, MISSING, UNCHANGED
from spec_classes.utils.mutation import prepare_attr_value
from spec_classes.utils.stackdepth import get_spec_classes_depth
from spec_classes.utils.type_checking import check_type, type_label
Expand Down Expand Up @@ -276,7 +277,7 @@ def __get__(self, instance, owner=None):
)

# Store value in cache is cache is enabled
if self.cache:
if self.cache and value not in (MISSING, EMPTY, UNCHANGED):
instance.__dict__[self.attr_name] = value

return value
Expand Down
2 changes: 1 addition & 1 deletion spec_classes/utils/mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def mutate_value(
# `replace`; otherwise use MISSING.
if new_value not in (MISSING, EMPTY, UNCHANGED):
value = new_value
elif new_value is UNCHANGED or not replace:
elif not replace:
value = old_value
prepare = None # Old values have already been prepared, so we suppress further preparation.
else:
Expand Down

0 comments on commit 3d35979

Please # to comment.