Skip to content

Commit

Permalink
#177 Add ParamNone.__hash__()
Browse files Browse the repository at this point in the history
  • Loading branch information
alexhad6 committed May 2, 2024
1 parent 9f1109b commit 8cb21c5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
5 changes: 4 additions & 1 deletion paramdb/_param_data/_primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,10 @@ def __bool__(self) -> bool:
return False

def __eq__(self, other: object) -> bool:
return isinstance(other, ParamNone)
return other is None or isinstance(other, ParamNone)

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

def __repr__(self) -> str:
# Show empty parentheses
Expand Down
38 changes: 29 additions & 9 deletions tests/_param_data/test_primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pytest
from paramdb import ParamInt, ParamFloat, ParamBool, ParamStr, ParamNone
from tests.helpers import (
SimpleParam,
CustomParamInt,
CustomParamFloat,
CustomParamBool,
Expand Down Expand Up @@ -174,19 +175,38 @@ def test_param_primitive_eq(
) -> None:
"""
Parameter primitive objects are equal to themselves, their vaues, and custom
parameter primitive objects, and are not equal to other objects.
parameter primitive objects.
"""
# pylint: disable=comparison-with-itself
assert param_primitive == param_primitive
assert param_primitive == deepcopy(param_primitive)
assert param_primitive == custom_param_primitive
assert custom_param_primitive == custom_param_primitive
assert custom_param_primitive == param_primitive
if isinstance(param_primitive, ParamNone):
assert param_primitive != param_primitive.value
assert custom_param_primitive != custom_param_primitive.value
else:
assert param_primitive == param_primitive.value
assert custom_param_primitive == custom_param_primitive.value
assert param_primitive == param_primitive.value


def test_param_primitive_ne(
simple_param: SimpleParam,
param_primitive: ParamPrimitive,
custom_param_primitive: CustomParamPrimitive,
) -> None:
"""
Parameter primitive objects are not equal to other objects or parameter primitives
with different values.
"""
assert param_primitive != simple_param
assert custom_param_primitive != simple_param
if not isinstance(param_primitive, ParamNone):
assert param_primitive != type(param_primitive)()
assert custom_param_primitive != type(custom_param_primitive)()


def test_param_primitive_hash(
param_primitive: ParamPrimitive, custom_param_primitive: CustomParamPrimitive
) -> None:
"""Parameter primitive objects has the same hash as objects they are equal to."""
assert hash(param_primitive) == hash(deepcopy(param_primitive))
assert hash(param_primitive) == hash(custom_param_primitive)
assert hash(param_primitive) == hash(param_primitive.value)


def test_param_int_methods_return_int(param_int: ParamInt) -> None:
Expand Down

0 comments on commit 8cb21c5

Please sign in to comment.