99"""
1010from __future__ import annotations
1111
12- import datetime
1312import enum
1413import logging
1514from dataclasses import dataclass , field
15+ from datetime import datetime , timedelta , timezone
1616from typing import Collection , Dict , Generic , Optional , Set , TypeVar
1717
18- import pytz
19-
2018from .formula import Formula
2119
2220Key = TypeVar ("Key" )
@@ -104,13 +102,13 @@ class Status(enum.Enum):
104102 UNKNOWN = "unknown"
105103 ERROR = "error"
106104
107- timestamp : datetime . datetime
105+ timestamp : datetime
108106 value : Optional [Value ] = None
109107 status : Status = Status .VALID
110108 broken_component_ids : Set [int ] = field (default_factory = set )
111109
112110 @staticmethod
113- def create_error (timestamp : datetime . datetime ) -> TimeSeriesEntry [Value ]:
111+ def create_error (timestamp : datetime ) -> TimeSeriesEntry [Value ]:
114112 """Create a `TimeSeriesEntry` that contains an error.
115113
116114 This can happen when the value would be NaN, e.g.
@@ -128,7 +126,7 @@ def create_error(timestamp: datetime.datetime) -> TimeSeriesEntry[Value]:
128126
129127 @staticmethod
130128 def create_unknown (
131- timestamp : datetime . datetime , broken_component_ids : Optional [Set [int ]] = None
129+ timestamp : datetime , broken_component_ids : Optional [Set [int ]] = None
132130 ) -> TimeSeriesEntry [Value ]:
133131 """Create a `TimeSeriesEntry` that contains an unknown value.
134132
@@ -174,11 +172,11 @@ class LatestEntryCache(Generic[Key, Value]):
174172
175173 def __init__ (self ) -> None :
176174 """Initialize the class."""
177- self ._latest_timestamp = pytz . utc . localize ( datetime . datetime . min )
175+ self ._latest_timestamp = datetime . min . replace ( tzinfo = timezone . utc )
178176 self ._entries : Dict [Key , TimeSeriesEntry [Value ]] = {}
179177
180178 @property
181- def latest_timestamp (self ) -> datetime . datetime :
179+ def latest_timestamp (self ) -> datetime :
182180 """Get the most recently observed timestamp across all keys in the cache.
183181
184182 Returns:
@@ -209,7 +207,7 @@ def __contains__(self, key: Key) -> bool:
209207 def get (
210208 self ,
211209 key : Key ,
212- timedelta_tolerance : datetime . timedelta = datetime . timedelta .max ,
210+ timedelta_tolerance : timedelta = timedelta .max ,
213211 default : Optional [TimeSeriesEntry [Value ]] = None ,
214212 ) -> CacheEntryLookupResult [Value ]:
215213 """Get the cached entry for the specified key, if any.
@@ -232,7 +230,7 @@ def get(
232230 retrieved from the cache has a timestamp greater than the latest saved
233231 timestamp across all cache keys.
234232 """
235- if timedelta_tolerance < datetime . timedelta (0 ):
233+ if timedelta_tolerance < timedelta (0 ):
236234 raise ValueError (
237235 f"timedelta_tolerance cannot be less than 0, but "
238236 f"{ timedelta_tolerance } was provided"
@@ -320,7 +318,7 @@ def reset(self) -> None:
320318 slightly more efficient.
321319 """
322320 self .clear ()
323- self ._latest_timestamp = pytz . utc . localize ( datetime . datetime . min )
321+ self ._latest_timestamp = datetime . min . replace ( tzinfo = timezone . utc )
324322
325323 def reset_latest_timestamp (self ) -> bool :
326324 """Reset the `latest_timestamp` property to the lowest possible value.
@@ -342,7 +340,7 @@ def reset_latest_timestamp(self) -> bool:
342340
343341 self ._latest_timestamp = max (
344342 map (lambda x : x .timestamp , self ._entries .values ()),
345- default = pytz . utc . localize ( datetime . datetime . min ),
343+ default = datetime . min . replace ( tzinfo = timezone . utc ),
346344 )
347345
348346 if self ._latest_timestamp > previous :
@@ -423,7 +421,7 @@ def evaluate(
423421 cache : LatestEntryCache [str , Value ],
424422 formula_name : str = "" ,
425423 symbol_to_symbol_mapping : Optional [Dict [str , SymbolMapping ]] = None ,
426- timedelta_tolerance : datetime . timedelta = datetime . timedelta .max ,
424+ timedelta_tolerance : timedelta = timedelta .max ,
427425 default_entry : Optional [TimeSeriesEntry [Value ]] = None ,
428426 ) -> Optional [TimeSeriesEntry [Value ]]:
429427 """Evaluate the formula using time-series values from the provided cache.
@@ -463,7 +461,7 @@ def evaluate(
463461 `timedelta_tolerance`
464462 """
465463 kwargs : Dict [str , Optional [Value ]] = {}
466- timestamp = pytz . utc . localize ( datetime . datetime . min )
464+ timestamp = datetime . min . replace ( tzinfo = timezone . utc )
467465
468466 symbol_to_symbol_mapping = symbol_to_symbol_mapping or {}
469467 formula_broken_component_ids : Set [int ] = set ()
0 commit comments