22import math
33from datetime import date , datetime , timedelta
44
5+ ONE_DAY = timedelta (days = 1 )
6+
57
68ONE_DAY = timedelta (days = 1 )
79
@@ -123,9 +125,7 @@ def unit_register(self):
123125 result = TimeunitKindMeta ._registered
124126 if result is None :
125127 result = {
126- k .kind_int : k
127- for k in TimeunitKindMeta ._pre_registered
128- if k .kind_int is not None
128+ k .kind_int : k for k in TimeunitKindMeta ._pre_registered if k .kind_int is not None
129129 }
130130 TimeunitKindMeta ._registered = result
131131 return result
@@ -167,18 +167,22 @@ def __int__(self):
167167 """
168168 return self .kind_int
169169
170- def __index__ ( self ):
171- return int ( self )
170+ def __int__ ( cls ):
171+ return cls . kind_int
172172
173- def __hash__ (self ):
173+ def __index__ (cls ):
174+ return int (cls )
175+
176+ def __hash__ (cls ):
174177 """
175178 Return the hash value of the time unit, based on its integer encoding.
176179 """
177- return hash (int (self ))
180+ return hash (int (cls ))
178181
179- def __eq__ (self , other ):
182+ def __eq__ (cls , other ):
180183 """
181- Return True if this time unit kind is the same as another kind or matches the kind registered for the given integer.
184+ Return True if this time unit kind is the same as another kind or matches the
185+ kind registered for the given integer.
182186
183187 Parameters:
184188 other: Another kind instance or an integer representing a registered kind.
@@ -188,7 +192,7 @@ def __eq__(self, other):
188192 """
189193 if isinstance (other , int ):
190194 other = TimeunitKind .unit_register [other ]
191- return self is other
195+ return cls is other
192196
193197 def __call__ (cls , dt ):
194198 """
@@ -200,8 +204,8 @@ def __call__(cls, dt):
200204 dt = dt .dt
201205 return Timeunit (cls , dt )
202206
203- def __lt__ (self , other ):
204- return self .kind_int < other .kind_int
207+ def __lt__ (cls , other ):
208+ return cls .kind_int < other .kind_int
205209
206210 def from_int (cls , val ):
207211 mul = cls .multiplier
@@ -241,7 +245,8 @@ def get_next(cls, dt):
241245 """
242246 Return the next time unit instance of this kind after the given date.
243247
244- If a `Timeunit` is provided, its date is used. The returned instance represents the time unit immediately following the one containing `dt`.
248+ If a `Timeunit` is provided, its date is used. The returned instance
249+ represents the time unit immediately following the one containing `dt`.
245250 """
246251 if isinstance (dt , Timeunit ):
247252 dt = dt .dt
@@ -315,11 +320,11 @@ def _shift(cls, cur, dt, amount):
315320 if new_dt is not None :
316321 return cls (new_dt )
317322 if amount > 0 :
318- for i in range (amount ):
323+ for _ in range (amount ):
319324 cur = cur .next
320325 return cur
321326 elif amount < 0 :
322- for i in range (- amount ):
327+ for _ in range (- amount ):
323328 cur = cur .previous
324329 return cur
325330 else :
@@ -464,9 +469,7 @@ def truncate(cls, dt):
464469 @classmethod
465470 def _inner_shift (cls , cur , dt , amount ):
466471 q_new = dt .year * 4 + amount + (dt .month - 1 ) // 3
467- y = q_new // 4
468- q = q_new % 4
469- return date (q_new // 4 , 3 * q + 1 , 1 )
472+ return date (q_new // 4 , 3 * (q_new % 4 ) + 1 , 1 )
470473
471474 @classmethod
472475 def _next (cls , dt ):
@@ -701,7 +704,8 @@ def date_range(self):
701704 @property
702705 def ancestors (self ):
703706 """
704- Yields an infinite sequence of preceding time units, starting from the previous unit of this instance.
707+ Yields an infinite sequence of preceding time units, starting from the
708+ previous unit of this instance.
705709
706710 Each iteration yields the next earlier time unit of the same kind.
707711 """
@@ -782,7 +786,8 @@ def __eq__(self, other):
782786 """
783787 Return True if this Timeunit is equal to another Timeunit or an integer representation.
784788
785- Equality is determined by matching both the kind and the truncated date. If `other` is an integer, it is first converted to a Timeunit instance.
789+ Equality is determined by matching both the kind and the truncated date.
790+ If `other` is an integer, it is first converted to a Timeunit instance.
786791 """
787792 if isinstance (other , int ):
788793 other = TimeunitKind .from_int (other )
@@ -840,6 +845,7 @@ def _get_range(cls, item):
840845 dt0 , dt1 = item
841846 if isinstance (dt0 , date ) and isinstance (dt1 , date ):
842847 return item
848+ raise TypeError (f"Cannot interpret date range of type { type (item )} " )
843849 except TypeError :
844850 pass
845851 raise TypeError (f"Item { item !r} has no date range." )
@@ -852,7 +858,8 @@ def overlaps_with(self, item):
852858 item: A date, Timeunit, or a tuple of two dates representing a date range.
853859
854860 Returns:
855- bool: True if there is any overlap between this time unit and the specified range or unit; otherwise, False.
861+ bool: True if there is any overlap between this time unit and the specified range
862+ or unit; otherwise, False.
856863 """
857864 frm0 , to0 = self ._get_range (item )
858865 frm , to = self .date_range
0 commit comments