Coverage for src/typedal/types.py: 100%
80 statements
« prev ^ index » next coverage.py v7.4.1, created at 2024-05-22 20:36 +0200
« prev ^ index » next coverage.py v7.4.1, created at 2024-05-22 20:36 +0200
1"""
2Stuff to make mypy happy.
3"""
5import typing
6from datetime import datetime
7from typing import Any, Optional, TypedDict
9from pydal.adapters.base import BaseAdapter
10from pydal.helpers.classes import OpRow as _OpRow
11from pydal.helpers.classes import Reference as _Reference
12from pydal.objects import Expression as _Expression
13from pydal.objects import Field as _Field
14from pydal.objects import Query as _Query
15from pydal.objects import Rows as _Rows
16from pydal.objects import Set as _Set
17from pydal.objects import Table as _Table
18from pydal.validators import Validator as _Validator
19from typing_extensions import NotRequired
21if typing.TYPE_CHECKING:
22 from .core import TypedField
24AnyDict: typing.TypeAlias = dict[str, Any]
27class Query(_Query): # type: ignore
28 """
29 Pydal Query object.
31 Makes mypy happy.
32 """
35class Expression(_Expression): # type: ignore
36 """
37 Pydal Expression object.
39 Make mypy happy.
40 """
43class Set(_Set): # type: ignore
44 """
45 Pydal Set object.
47 Make mypy happy.
48 """
51class OpRow(_OpRow): # type: ignore
52 """
53 Pydal OpRow object.
55 Make mypy happy.
56 """
59class Reference(_Reference): # type: ignore
60 """
61 Pydal Reference object.
63 Make mypy happy.
64 """
67class Field(_Field): # type: ignore
68 """
69 Pydal Field object.
71 Make mypy happy.
72 """
75class Rows(_Rows): # type: ignore
76 """
77 Pydal Rows object.
79 Make mypy happy.
80 """
83class Validator(_Validator): # type: ignore
84 """
85 Pydal Validator object.
87 Make mypy happy.
88 """
91class _Types:
92 """
93 Internal type storage for stuff that mypy otherwise won't understand.
94 """
96 NONETYPE = type(None)
99BeforeInsertCallable: typing.TypeAlias = typing.Callable[[OpRow], Any]
100AfterInsertCallable: typing.TypeAlias = typing.Callable[[OpRow, Reference], Any]
101BeforeUpdateCallable: typing.TypeAlias = typing.Callable[[Set, OpRow], Any]
102AfterUpdateCallable: typing.TypeAlias = typing.Callable[[Set, OpRow], Any]
103BeforeDeleteCallable: typing.TypeAlias = typing.Callable[[Set], Any]
104AfterDeleteCallable: typing.TypeAlias = typing.Callable[[Set], Any]
107class Pagination(TypedDict):
108 """
109 Pagination key of a paginate dict has these items.
110 """
112 total_items: int
113 current_page: int
114 per_page: int
115 total_pages: int
116 has_next_page: bool
117 has_prev_page: bool
118 next_page: Optional[int]
119 prev_page: Optional[int]
122class PaginateDict(TypedDict):
123 """
124 Result of PaginatedRows.as_dict().
125 """
127 data: dict[int, AnyDict]
128 pagination: Pagination
131class CacheMetadata(TypedDict):
132 """
133 Used by query builder metadata in the 'cache' key.
134 """
136 enabled: bool
137 depends_on: list[Any]
138 key: NotRequired[str | None]
139 status: NotRequired[str | None]
140 expires_at: NotRequired[datetime | None]
141 cached_at: NotRequired[datetime | None]
144class PaginationMetadata(TypedDict):
145 """
146 Used by query builder metadata in the 'pagination' key.
147 """
149 limit: int
150 current_page: int
151 max_page: int
152 rows: int
153 min_max: tuple[int, int]
156class TableProtocol(typing.Protocol): # pragma: no cover
157 """
158 Make mypy happy.
159 """
161 id: "TypedField[int]"
163 def __getitem__(self, item: str) -> Field:
164 """
165 Tell mypy a Table supports dictionary notation for columns.
166 """
169class Table(_Table, TableProtocol): # type: ignore
170 """
171 Make mypy happy.
172 """
175class CacheFn(typing.Protocol):
176 """
177 The cache model (e.g. cache.ram) accepts these parameters (all filled by dfeault).
178 """
180 def __call__(
181 self: BaseAdapter,
182 sql: str = "",
183 fields: typing.Iterable[str] = (),
184 attributes: typing.Iterable[str] = (),
185 colnames: typing.Iterable[str] = (),
186 ) -> Rows:
187 """
188 Only used for type-hinting.
189 """
192# CacheFn = typing.Callable[[], Rows]
193CacheModel = typing.Callable[[str, CacheFn, int], Rows]
194CacheTuple = tuple[CacheModel, int]
197class SelectKwargs(typing.TypedDict, total=False):
198 """
199 Possible keyword arguments for .select().
200 """
202 join: Optional[list[Expression]]
203 left: Optional[list[Expression]]
204 orderby: Optional[Expression | str | Table]
205 limitby: Optional[tuple[int, int]]
206 distinct: bool | Field | Expression
207 orderby_on_limitby: bool
208 cacheable: bool
209 cache: CacheTuple
212class Metadata(TypedDict):
213 """
214 Loosely structured metadata used by Query Builder.
215 """
217 cache: NotRequired[CacheMetadata]
218 pagination: NotRequired[PaginationMetadata]
220 query: NotRequired[Query | str | None]
221 ids: NotRequired[str]
223 final_query: NotRequired[Query | str | None]
224 final_args: NotRequired[list[Any]]
225 final_kwargs: NotRequired[SelectKwargs]
226 relationships: NotRequired[set[str]]
228 sql: NotRequired[str]