Skip to content
Snippets Groups Projects
Commit 2091aa0e authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

v2.17.23 fixed OmniHashableMixin

parent 8916d51e
No related branches found
Tags v2.17.23
No related merge requests found
# v2.17.23 # v2.17.23
* un-deprecated the `cps` metric * un-deprecated the `cps` metric
* OmniHashableMixin will check for typing as well
* fixed a bug in OmniHashableMixin regarding attribute checks
__version__ = '2.17.23a1' __version__ = '2.17.23'
...@@ -143,6 +143,8 @@ class OmniHashableMixin(metaclass=ABCMeta): ...@@ -143,6 +143,8 @@ class OmniHashableMixin(metaclass=ABCMeta):
Do everything in your power to make specified fields immutable, as mutating them will result Do everything in your power to make specified fields immutable, as mutating them will result
in a different hash. in a different hash.
This will also check if the other value is an instance of this instance's class.
_HASH_FIELDS_TO_USE can be also a single string, in this case a single field called by this _HASH_FIELDS_TO_USE can be also a single string, in this case a single field called by this
name will be taken. name will be taken.
...@@ -178,29 +180,40 @@ class OmniHashableMixin(metaclass=ABCMeta): ...@@ -178,29 +180,40 @@ class OmniHashableMixin(metaclass=ABCMeta):
""" """
Note that this will only compare _HASH_FIELDS_TO_USE Note that this will only compare _HASH_FIELDS_TO_USE
""" """
if not isinstance(other, type(self)):
return False
if isinstance(other, OmniHashableMixin): if isinstance(other, OmniHashableMixin):
cmpr_by = self._HASH_FIELDS_TO_USE cmpr_by = self._HASH_FIELDS_TO_USE
try:
if isinstance(cmpr_by, str): if isinstance(cmpr_by, str):
return getattr(self, cmpr_by) == getattr(other, cmpr_by) return getattr(self, cmpr_by) == getattr(other, cmpr_by)
for field_name in self._HASH_FIELDS_TO_USE: for field_name in self._HASH_FIELDS_TO_USE:
if getattr(self, field_name) != getattr(other, field_name): if getattr(self, field_name) != getattr(other, field_name):
return False return False
return True return True
except AttributeError:
return False
else: else:
return super().__eq__(other) return super().__eq__(other)
def __ne__(self, other) -> bool: def __ne__(self, other) -> bool:
if not isinstance(other, type(self)):
return True
if isinstance(other, OmniHashableMixin): if isinstance(other, OmniHashableMixin):
cmpr_by = self._HASH_FIELDS_TO_USE cmpr_by = self._HASH_FIELDS_TO_USE
if isinstance(cmpr_by, str): try:
return getattr(self, cmpr_by) != getattr(other, cmpr_by) if isinstance(cmpr_by, str):
return getattr(self, cmpr_by) != getattr(other, cmpr_by)
for field_name in cmpr_by: for field_name in cmpr_by:
if getattr(self, field_name) != getattr(other, field_name): if getattr(self, field_name) != getattr(other, field_name):
return True return True
return False return False
except AttributeError:
return True
else: else:
return super().__ne__(other) return super().__ne__(other)
...@@ -664,6 +664,33 @@ class TestMisc(unittest.TestCase): ...@@ -664,6 +664,33 @@ class TestMisc(unittest.TestCase):
self.assertEqual(a[e1], '1') self.assertEqual(a[e1], '1')
self.assertEqual(hash(e1), hash(2)) self.assertEqual(hash(e1), hash(2))
def test_omni_not_filled_in_fields(self):
class Omni1(OmniHashableMixin):
_HASH_FIELDS_TO_USE = 'a'
def __init__(self, a=None):
if a is not None:
self.a = a
self.assertNotEqual(Omni1(3), Omni1())
self.assertFalse(Omni1() == Omni1(3))
def test_omni_different_hierarchies(self):
class Omni1(OmniHashableMixin):
_HASH_FIELDS_TO_USE = 'a'
def __init__(self, a):
self.a = a
class Omni2(OmniHashableMixin):
_HASH_FIELDS_TO_USE = 'a'
def __init__(self, a):
self.a = a
self.assertNotEqual(Omni1(1), Omni2(1))
self.assertFalse(Omni1(1) == Omni2(1))
def test_omni_single_field(self): def test_omni_single_field(self):
class Omni(OmniHashableMixin): class Omni(OmniHashableMixin):
_HASH_FIELDS_TO_USE = 'a' _HASH_FIELDS_TO_USE = 'a'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment