Coverage for src/typedal/types.py: 100%
61 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-21 18:27 +0100
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-21 18:27 +0100
1"""
2Stuff to make mypy happy.
3"""
4import typing
5from datetime import datetime
6from typing import Any, Optional, TypedDict
8from pydal.helpers.classes import OpRow as _OpRow
9from pydal.helpers.classes import Reference as _Reference
10from pydal.objects import Expression as _Expression
11from pydal.objects import Field as _Field
12from pydal.objects import Query as _Query
13from pydal.objects import Rows as _Rows
14from pydal.objects import Set as _Set
15from typing_extensions import NotRequired
18class Query(_Query): # type: ignore
19 """
20 Pydal Query object.
22 Makes mypy happy.
23 """
26class Expression(_Expression): # type: ignore
27 """
28 Pydal Expression object.
30 Make mypy happy.
31 """
34class Set(_Set): # type: ignore
35 """
36 Pydal Set object.
38 Make mypy happy.
39 """
42class OpRow(_OpRow): # type: ignore
43 """
44 Pydal OpRow object.
46 Make mypy happy.
47 """
50class Reference(_Reference): # type: ignore
51 """
52 Pydal Reference object.
54 Make mypy happy.
55 """
58class Field(_Field): # type: ignore
59 """
60 Pydal Field object.
62 Make mypy happy.
63 """
66class Rows(_Rows): # type: ignore
67 """
68 Pydal Rows object.
70 Make mypy happy.
71 """
74class _Types:
75 """
76 Internal type storage for stuff that mypy otherwise won't understand.
77 """
79 NONETYPE = type(None)
82BeforeInsertCallable: typing.TypeAlias = typing.Callable[[OpRow], Any]
83AfterInsertCallable: typing.TypeAlias = typing.Callable[[OpRow, Reference], Any]
84BeforeUpdateCallable: typing.TypeAlias = typing.Callable[[Set, OpRow], Any]
85AfterUpdateCallable: typing.TypeAlias = typing.Callable[[Set, OpRow], Any]
86BeforeDeleteCallable: typing.TypeAlias = typing.Callable[[Set], Any]
87AfterDeleteCallable: typing.TypeAlias = typing.Callable[[Set], Any]
90class Pagination(TypedDict):
91 """
92 Pagination key of a paginate dict has these items.
93 """
95 total_items: int
96 current_page: int
97 per_page: int
98 total_pages: int
99 has_next_page: bool
100 has_prev_page: bool
101 next_page: Optional[int]
102 prev_page: Optional[int]
105class PaginateDict(TypedDict):
106 """
107 Result of PaginatedRows.as_dict().
108 """
110 data: dict[int, dict[str, Any]]
111 pagination: Pagination
114class CacheMetadata(TypedDict):
115 """
116 Used by query builder metadata in the 'cache' key.
117 """
119 enabled: bool
120 depends_on: list[Any]
121 key: NotRequired[str | None]
122 status: NotRequired[str | None]
123 expires_at: NotRequired[datetime | None]
124 cached_at: NotRequired[datetime | None]
127class PaginationMetadata(TypedDict):
128 """
129 Used by query builder metadata in the 'pagination' key.
130 """
132 limit: int
133 current_page: int
134 max_page: int
135 rows: int
136 min_max: tuple[int, int]
139class Metadata(TypedDict):
140 """
141 Loosely structured metadata used by Query Builder.
142 """
144 cache: NotRequired[CacheMetadata]
145 pagination: NotRequired[PaginationMetadata]
147 query: NotRequired[Query | str | None]
148 ids: NotRequired[str]
150 final_query: NotRequired[Query | str | None]
151 final_args: NotRequired[list[Any]]
152 final_kwargs: NotRequired[dict[str, Any]]
153 relationships: NotRequired[set[str]]
155 sql: NotRequired[str]