fix: correct type annotations in databento_source.py
- Fix return type of _load_from_cache and _df_to_daily_points to return list[DailyClosePoint] - Import DailyClosePoint from historical_provider - Use TYPE_CHECKING pattern for optional databento/pandas imports
This commit is contained in:
@@ -8,20 +8,31 @@ import logging
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from datetime import date, timedelta
|
from datetime import date, timedelta
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any
|
from typing import TYPE_CHECKING, Any
|
||||||
|
|
||||||
|
from app.services.backtesting.historical_provider import DailyClosePoint
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
import databento as db
|
||||||
|
import pandas as pd
|
||||||
|
else:
|
||||||
|
db = None
|
||||||
|
pd = None
|
||||||
|
|
||||||
|
DATABENTO_AVAILABLE = False
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
# Try to import databento, gracefully degrade if not available
|
# Try to import databento, gracefully degrade if not available
|
||||||
try:
|
try:
|
||||||
import databento as db
|
import databento as _db
|
||||||
import pandas as pd
|
import pandas as _pd
|
||||||
|
|
||||||
|
db = _db
|
||||||
|
pd = _pd
|
||||||
DATABENTO_AVAILABLE = True
|
DATABENTO_AVAILABLE = True
|
||||||
except ImportError:
|
except ImportError:
|
||||||
db = None
|
pass
|
||||||
pd = None
|
|
||||||
DATABENTO_AVAILABLE = False
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@@ -98,7 +109,7 @@ class DatabentoHistoricalPriceSource:
|
|||||||
self._client = db.Historical(key=self.config.api_key)
|
self._client = db.Historical(key=self.config.api_key)
|
||||||
return self._client
|
return self._client
|
||||||
|
|
||||||
def _load_from_cache(self, key: DatabentoCacheKey) -> list[dict[str, Any]] | None:
|
def _load_from_cache(self, key: DatabentoCacheKey) -> list[DailyClosePoint] | None:
|
||||||
"""Load cached data if available and fresh."""
|
"""Load cached data if available and fresh."""
|
||||||
cache_file = key.cache_path(self.config.cache_dir)
|
cache_file = key.cache_path(self.config.cache_dir)
|
||||||
meta_file = key.metadata_path(self.config.cache_dir)
|
meta_file = key.metadata_path(self.config.cache_dir)
|
||||||
@@ -166,7 +177,7 @@ class DatabentoHistoricalPriceSource:
|
|||||||
)
|
)
|
||||||
return data.to_df()
|
return data.to_df()
|
||||||
|
|
||||||
def _df_to_daily_points(self, df: Any) -> list[Any]:
|
def _df_to_daily_points(self, df: Any) -> list[DailyClosePoint]:
|
||||||
"""Convert DataFrame to DailyClosePoint list with OHLC data."""
|
"""Convert DataFrame to DailyClosePoint list with OHLC data."""
|
||||||
from app.services.backtesting.historical_provider import DailyClosePoint
|
from app.services.backtesting.historical_provider import DailyClosePoint
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user