Skip to content

Commit

Permalink
refactor: update generics
Browse files Browse the repository at this point in the history
  • Loading branch information
zumuta committed Nov 10, 2024
1 parent 5626c64 commit 1c5f2d1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
6 changes: 2 additions & 4 deletions backend/src/kwai/core/db/table.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
"""Module for the table decorator."""

from dataclasses import fields, is_dataclass
from typing import Any, Callable, Generic, TypeVar
from typing import Any, Callable

from sql_smith.functions import alias
from sql_smith.functions import field as sql_field

T = TypeVar("T", bound=Callable)


class Table(Generic[T]):
class Table[T: Callable]:
"""Represent a table in the database.
With this class a table row can be transformed into a dataclass. It can also
Expand Down
12 changes: 5 additions & 7 deletions backend/src/kwai/core/domain/use_case.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Module for defining common classes, functions, ... for use cases."""

from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import AsyncIterator, Generic, NamedTuple, TypeVar
from typing import AsyncIterator, NamedTuple


class UseCaseResult(ABC):
Expand All @@ -13,11 +14,8 @@ def to_message(self) -> str:
raise NotImplementedError


T = TypeVar("T")


@dataclass(kw_only=True, frozen=True, slots=True)
class NotFoundResult(UseCaseResult, Generic[T]):
class NotFoundResult[T](UseCaseResult):
"""A result that indicates that an entity was not found."""

entity_name: str
Expand All @@ -28,7 +26,7 @@ def to_message(self) -> str:


@dataclass(kw_only=True, frozen=True, slots=True)
class EntitiesResult(UseCaseResult, Generic[T]):
class EntitiesResult[T](UseCaseResult):
"""A result that returns an iterator for entities and the number of entities."""

count: int
Expand All @@ -39,7 +37,7 @@ def to_message(self) -> str:


@dataclass(kw_only=True, frozen=True, slots=True)
class EntityResult(UseCaseResult, Generic[T]):
class EntityResult[T](UseCaseResult):
"""A result that returns an entity."""

entity: T
Expand Down
6 changes: 2 additions & 4 deletions backend/src/kwai/core/domain/value_objects/identifier.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
"""Module that defines identifiers."""
from abc import ABC, abstractmethod
from typing import Generic, TypeVar

T = TypeVar("T")
from abc import ABC, abstractmethod


class Identifier(ABC, Generic[T]):
class Identifier[T](ABC):
"""Abstract and generic class for an identifier."""

def __init__(self, id_: T):
Expand Down

0 comments on commit 1c5f2d1

Please sign in to comment.