diff --git a/satella/coding/structures/mixins/enums.py b/satella/coding/structures/mixins/enums.py index 4ebe734ec66980d21a4f86090dab1e605d1159c1..51201ea968b03b07290cdb9c8e4dee7fcdd2eac2 100644 --- a/satella/coding/structures/mixins/enums.py +++ b/satella/coding/structures/mixins/enums.py @@ -4,7 +4,7 @@ import enum class ComparableEnum(enum.Enum): """ An enum whose compare will try to convert value you compare it against - to it's instance. Handly for writing code like: + to its instance. Handly for writing code like: >>> a = 'test' >>> class Enum(ComparableEnum): @@ -33,18 +33,17 @@ class ComparableEnum(enum.Enum): if isinstance(other, enum.Enum) and not isinstance(other, self.__class__): other = other.value - if not isinstance(other, self.__class__): - try: - return self.__class__(other) == self - except ValueError: # other is not a correct member of this class! - return False - else: + if isinstance(other, self.__class__): return super().__eq__(other) + try: + return self.__class__(other) == self + except ValueError: # other is not a correct member of this class! + return False class HashableIntEnum(enum.IntEnum): """ - An enum.IntEnum that implements hashability, stemming from it's values, as well + An enum.IntEnum that implements hashability, stemming from its values, as well as hashability """ @@ -54,7 +53,7 @@ class HashableIntEnum(enum.IntEnum): class ComparableIntEnum(HashableIntEnum): """ - An enum.IntEnum that implements comparision, stemming from it's values, as well + An enum.IntEnum that implements comparison, stemming from its values, as well as hashability. It it has an int() method, it's fair game as comparison argument for this class. diff --git a/satella/coding/structures/mixins/hashable.py b/satella/coding/structures/mixins/hashable.py index b53be462b258a643dd370bd9757f0c37329f78e8..9ff9742e50b7c9eb55ccf17b7589ae1857425272 100644 --- a/satella/coding/structures/mixins/hashable.py +++ b/satella/coding/structures/mixins/hashable.py @@ -4,7 +4,7 @@ from abc import ABCMeta, abstractmethod class HashableMixin: """ - Make a class hashable by it's ID. + Make a class hashable by its ID. Just remember to add the following to your class definition if you're overriding __eq__: @@ -41,7 +41,7 @@ class ComparableAndHashableBy(metaclass=ABCMeta): @property @abstractmethod - def _COMPARABLE_BY(self) -> str: + def _COMPARABLE_BY(self) -> str: # pylint: disable=invalid-name """ Return the sequence of names of properties and attributes that will be used for __eq__ and __hash__ @@ -159,7 +159,7 @@ class OmniHashableMixin(metaclass=ABCMeta): @property @abstractmethod - def _HASH_FIELDS_TO_USE(self) -> tp.Union[str, tp.Sequence[str]]: + def _HASH_FIELDS_TO_USE(self) -> tp.Union[str, tp.Sequence[str]]: # pylint: disable=invalid-name """ Return the sequence of names of properties and attributes that will be used for __eq__ and __hash__ @@ -183,37 +183,38 @@ class OmniHashableMixin(metaclass=ABCMeta): if not isinstance(other, type(self)): return False - if isinstance(other, OmniHashableMixin): - cmpr_by = self._HASH_FIELDS_TO_USE - try: - if isinstance(cmpr_by, str): - return getattr(self, cmpr_by) == getattr(other, cmpr_by) - - for field_name in self._HASH_FIELDS_TO_USE: - if getattr(self, field_name) != getattr(other, field_name): - return False - return True - except AttributeError: - return False - else: + if not isinstance(other, OmniHashableMixin): return super().__eq__(other) + cmpr_by = self._HASH_FIELDS_TO_USE + try: + if isinstance(cmpr_by, str): + return getattr(self, cmpr_by) == getattr(other, cmpr_by) + + for field_name in self._HASH_FIELDS_TO_USE: + if getattr(self, field_name) != getattr(other, field_name): + return False + return True + except AttributeError: + return False + def __ne__(self, other) -> bool: if not isinstance(other, type(self)): return True - if isinstance(other, OmniHashableMixin): - cmpr_by = self._HASH_FIELDS_TO_USE + if not isinstance(other, OmniHashableMixin): + return super().__ne__(other) + + cmpr_by = self._HASH_FIELDS_TO_USE - try: - if isinstance(cmpr_by, str): - return getattr(self, cmpr_by) != getattr(other, cmpr_by) + try: + if isinstance(cmpr_by, str): + return getattr(self, cmpr_by) != getattr(other, cmpr_by) + + for field_name in cmpr_by: + if getattr(self, field_name) != getattr(other, field_name): + return True + return False + except AttributeError: + return True - for field_name in cmpr_by: - if getattr(self, field_name) != getattr(other, field_name): - return True - return False - except AttributeError: - return True - else: - return super().__ne__(other)