Skip to content

Commit fae7887

Browse files
author
Robert Schindler
committed
[schedy] Use NotImplemented in rich comparator methods
1 parent 1093e83 commit fae7887

File tree

2 files changed

+9
-16
lines changed

2 files changed

+9
-16
lines changed

hass_apps/schedy/actor/thermostat.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,17 @@ def __add__(self, other: T.Any) -> "Temp":
6767
if isinstance(other, (float, int)):
6868
other = type(self)(other)
6969
elif not isinstance(other, type(self)):
70-
raise TypeError(
71-
"can't add {} and {}".format(repr(type(self)), repr(type(other)))
72-
)
70+
return NotImplemented
7371

7472
# OFF + something is OFF
7573
if self.is_off or other.is_off:
7674
return type(self)(OFF)
77-
7875
return type(self)(self.value + other.value)
7976

8077
def __eq__(self, other: T.Any) -> bool:
81-
return isinstance(other, Temp) and self.value == other.value
78+
if not isinstance(other, type(self)):
79+
return NotImplemented
80+
return self.value == other.value
8281

8382
def __float__(self) -> float:
8483
if isinstance(self.value, float):
@@ -90,21 +89,17 @@ def __hash__(self) -> int:
9089

9190
def __lt__(self, other: T.Any) -> bool:
9291
if isinstance(other, (float, int)):
93-
other = Temp(other)
94-
92+
other = type(self)(other)
9593
if type(self) is not type(other):
96-
raise TypeError(
97-
"can't compare {} and {}".format(repr(type(self)), repr(type(other)))
98-
)
99-
94+
return NotImplemented
10095
if not self.is_off and other.is_off:
10196
return False
10297
if self.is_off and not other.is_off or self.value < other.value:
10398
return True
10499
return False
105100

106101
def __neg__(self) -> "Temp":
107-
return Temp(-self.value) # pylint: disable=invalid-unary-operand-type
102+
return type(self)(-self.value) # pylint: disable=invalid-unary-operand-type
108103

109104
def __repr__(self) -> str:
110105
if isinstance(self.value, (float, int)):

hass_apps/schedy/schedule.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -416,10 +416,8 @@ def __init__(self, name: str = None, rules: T.Iterable[Rule] = None) -> None:
416416
self.rules.extend(rules)
417417

418418
def __add__(self, other: "Schedule") -> "Schedule":
419-
if not isinstance(other, type(self)):
420-
raise ValueError(
421-
"{} objects may not be added to {}.".format(type(other), self)
422-
)
419+
if not isinstance(other, Schedule):
420+
return NotImplemented
423421
return Schedule(name=self.name, rules=self.rules + other.rules)
424422

425423
def __repr__(self) -> str:

0 commit comments

Comments
 (0)