Verified Commit e4b97390 authored by Jakob Moser's avatar Jakob Moser
Browse files

Fix broken impl and multiple typing issues

parent 793c774f
Loading
Loading
Loading
Loading
+16 −6
Original line number Diff line number Diff line
from __future__ import annotations

from typing import Any
from collections.abc import Sequence
from typing import Any, Self

from sqlalchemy import select
from sqlalchemy.orm import DeclarativeBase, MappedColumn

from poolpay import db
@@ -28,19 +30,27 @@ class Retrievable(DeclarativeBase):
        return None

    @classmethod
    def get_all(cls) -> list[Retrievable]:
    def get_all(cls) -> Sequence[Self]:
        """
        Return all instances of this type.
        """
        return db.session.execute(
            db.select(cls).order_by(cls.get_canonical_order_column())
        ).scalars()
        if not db.session:
            raise RuntimeError("db.session has not been initialized")

        return (
            db.session.execute(select(cls).order_by(cls.get_canonical_order_column()))
            .scalars()
            .all()
        )

    @classmethod
    def get_only(cls, primary_key: Any) -> Retrievable | None:
    def get_only(cls, primary_key: Any) -> Self | None:
        """
        Return the instance of this type with the given primary key or None if it doesn't exist.
        """
        if not db.session:
            raise RuntimeError("db.session has not been initialized")

        return db.session.get(cls, primary_key)

    @property