Verified Commit 78d1bec2 authored by Jakob Moser's avatar Jakob Moser
Browse files

Correctly annotate return type of getter methods

parent 7af2ce3c
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -22,7 +22,7 @@ class Base(Retrievable):
    uuid: Mapped[UUID] = mapped_column(Uuid(), primary_key=True, default=uuid4)

    @classmethod
    def get_only(cls, uuid: str | UUID) -> Base | None:
    def get_only[T](cls: type[T], uuid: str | UUID) -> T | None:
        if isinstance(uuid, str):
            try:
                # Manually convert the uuid to a UUID object (even though sqlalchemy-uuid would do that
+3 −3
Original line number Diff line number Diff line
from __future__ import annotations

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

from sqlalchemy import select
from sqlalchemy.orm import DeclarativeBase, MappedColumn
@@ -30,7 +30,7 @@ class Retrievable(DeclarativeBase):
        return None

    @classmethod
    def get_all(cls) -> Sequence[Self]:
    def get_all[T](cls: type[T]) -> Sequence[T]:
        """
        Return all instances of this type.
        """
@@ -44,7 +44,7 @@ class Retrievable(DeclarativeBase):
        )

    @classmethod
    def get_only(cls, primary_key: Any) -> Self | None:
    def get_only[T](cls: type[T], primary_key: Any) -> T | None:
        """
        Return the instance of this type with the given primary key or None if it doesn't exist.
        """