From 6e1461156b2263172f404c8e65ef3beb791d6da8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@henrietta.com.pl> Date: Mon, 4 Mar 2024 08:39:23 +0100 Subject: [PATCH] fix codeclimate --- satella/coding/structures/mixins/enums.py | 17 +++--- satella/coding/structures/mixins/hashable.py | 59 ++++++++++---------- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/satella/coding/structures/mixins/enums.py b/satella/coding/structures/mixins/enums.py index 4ebe734e..51201ea9 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 b53be462..9ff9742e 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) -- GitLab