88from collections .abc import Callable , Iterator
99from dataclasses import dataclass
1010from datetime import datetime , timezone
11- from typing import Any , Generic , Protocol , Self , TypeVar , overload , runtime_checkable
11+ from typing import Any , Generic , Protocol , Self , TypeVar , cast , overload
1212
1313from ._quantities import Power , QuantityT
1414
@@ -134,40 +134,22 @@ def map(
134134 )
135135
136136
137- @runtime_checkable
138137class Comparable (Protocol ):
139- """
140- A protocol that requires the implementation of comparison methods.
138+ """A protocol that requires the implementation of comparison methods.
141139
142140 This protocol is used to ensure that types can be compared using
143141 the less than or equal to (`<=`) and greater than or equal to (`>=`)
144142 operators.
145143 """
146144
147145 def __le__ (self , other : Any , / ) -> bool :
148- """
149- Return True if self is less than or equal to other, otherwise False.
150-
151- Args:
152- other: The value to compare with.
153-
154- Returns:
155- bool: True if self is less than or equal to other, otherwise False.
156- """
146+ """Return whether this instance is less than or equal to `other`."""
157147
158148 def __ge__ (self , other : Any , / ) -> bool :
159- """
160- Return True if self is greater than or equal to other, otherwise False.
161-
162- Args:
163- other: The value to compare with.
164-
165- Returns:
166- bool: True if self is greater than or equal to other, otherwise False.
167- """
149+ """Return whether this instance is greater than or equal to `other`."""
168150
169151
170- _T = TypeVar ("_T" )
152+ _T = TypeVar ("_T" , bound = Comparable | None )
171153
172154
173155@dataclass (frozen = True )
@@ -190,18 +172,14 @@ def __contains__(self, item: _T) -> bool:
190172 Returns:
191173 bool: True if value is within the range, otherwise False.
192174 """
193- if item is None :
194- return False
195-
196- assert isinstance (item , Comparable )
197-
198- if self .lower is not None and self .upper is not None :
199- return self .lower <= item <= self .upper
200- if self .lower is not None :
201- return self .lower <= item
202- if self .upper is not None :
175+ if self .lower is None and self .upper is None :
176+ return True
177+ if self .lower is None :
203178 return item <= self .upper
204- return False
179+ if self .upper is None :
180+ return self .lower <= item
181+
182+ return cast (Comparable , self .lower ) <= item <= cast (Comparable , self .upper )
205183
206184
207185@dataclass (frozen = True , kw_only = True )
@@ -238,9 +216,7 @@ def __contains__(self, item: Power) -> bool:
238216 Returns:
239217 bool: True if value is within the range, otherwise False.
240218 """
241- if not self .inclusion_bounds :
242- return False
243- if item not in self .inclusion_bounds :
219+ if not self .inclusion_bounds or item not in self .inclusion_bounds :
244220 return False
245221 if self .exclusion_bounds and item in self .exclusion_bounds :
246222 return False
0 commit comments