From db4f4498dc6583e6a1eb9e3183f6b11c6232de96 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 20 Feb 2023 20:00:01 +0100 Subject: [PATCH] Experimentally type query property ... --- freqtrade/persistence/models.py | 3 ++- freqtrade/persistence/pairlock.py | 6 ++++-- freqtrade/persistence/trade_model.py | 12 +++++++----- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/freqtrade/persistence/models.py b/freqtrade/persistence/models.py index f4058b4eb..b94be950a 100644 --- a/freqtrade/persistence/models.py +++ b/freqtrade/persistence/models.py @@ -6,7 +6,7 @@ from typing import Any, Dict from sqlalchemy import create_engine, inspect from sqlalchemy.exc import NoSuchModuleError -from sqlalchemy.orm import scoped_session, sessionmaker +from sqlalchemy.orm import Session, scoped_session, sessionmaker from sqlalchemy.pool import StaticPool from freqtrade.exceptions import OperationalException @@ -20,6 +20,7 @@ logger = logging.getLogger(__name__) _SQL_DOCS_URL = 'http://docs.sqlalchemy.org/en/latest/core/engines.html#database-urls' +SessionType = scoped_session[Session] def init_db(db_url: str) -> None: diff --git a/freqtrade/persistence/pairlock.py b/freqtrade/persistence/pairlock.py index f8c0586a4..b10d74693 100644 --- a/freqtrade/persistence/pairlock.py +++ b/freqtrade/persistence/pairlock.py @@ -3,9 +3,11 @@ from typing import Any, ClassVar, Dict, Optional from sqlalchemy import String, or_ from sqlalchemy.orm import Mapped, Query, mapped_column +from sqlalchemy.orm.scoping import _QueryDescriptorType from freqtrade.constants import DATETIME_PRINT_FORMAT from freqtrade.persistence.base import ModelBase +from freqtrade.persistence.models import SessionType class PairLock(ModelBase): @@ -13,8 +15,8 @@ class PairLock(ModelBase): Pair Locks database model. """ __tablename__ = 'pairlocks' - # TODO: Properly type query. - query: ClassVar[Any] + query: ClassVar[_QueryDescriptorType] + session: ClassVar[SessionType] id: Mapped[int] = mapped_column(primary_key=True) diff --git a/freqtrade/persistence/trade_model.py b/freqtrade/persistence/trade_model.py index abb989940..10643db17 100644 --- a/freqtrade/persistence/trade_model.py +++ b/freqtrade/persistence/trade_model.py @@ -9,6 +9,7 @@ from typing import Any, ClassVar, Dict, List, Optional from sqlalchemy import Enum, Float, ForeignKey, Integer, String, UniqueConstraint, desc, func from sqlalchemy.orm import Mapped, Query, lazyload, mapped_column, relationship +from sqlalchemy.orm.scoping import _QueryDescriptorType from freqtrade.constants import (DATETIME_PRINT_FORMAT, MATH_CLOSE_PREC, NON_OPEN_EXCHANGE_STATES, BuySell, LongShort) @@ -17,6 +18,7 @@ from freqtrade.exceptions import DependencyException, OperationalException from freqtrade.exchange import amount_to_contract_precision, price_to_precision from freqtrade.leverage import interest from freqtrade.persistence.base import ModelBase +from freqtrade.persistence.models import SessionType from freqtrade.util import FtPrecise @@ -35,8 +37,9 @@ class Order(ModelBase): Mirrors CCXT Order structure """ __tablename__ = 'orders' - # TODO: Properly type query. - query: ClassVar[Any] + query: ClassVar[_QueryDescriptorType] + session: ClassVar[SessionType] + # Uniqueness should be ensured over pair, order_id # its likely that order_id is unique per Pair on some exchanges. __table_args__ = (UniqueConstraint('ft_pair', 'order_id', name="_order_pair_order_id"),) @@ -1172,9 +1175,8 @@ class Trade(ModelBase, LocalTrade): Note: Fields must be aligned with LocalTrade class """ __tablename__ = 'trades' - # TODO: Type query type throughout. - query: ClassVar[Any] - session: ClassVar[Any] = None + query: ClassVar[_QueryDescriptorType] + session: ClassVar[SessionType] = None use_db: bool = True