Coverage for src/typedal/types.py: 100%
64 statements
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-08 16:34 +0200
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-08 16:34 +0200
1"""
2Stuff to make mypy happy.
3"""
5import typing
6from datetime import datetime
7from typing import Any, Optional, TypedDict
9from pydal.helpers.classes import OpRow as _OpRow
10from pydal.helpers.classes import Reference as _Reference
11from pydal.objects import Expression as _Expression
12from pydal.objects import Field as _Field
13from pydal.objects import Query as _Query
14from pydal.objects import Rows as _Rows
15from pydal.objects import Set as _Set
16from pydal.validators import Validator as _Validator
17from typing_extensions import NotRequired
19AnyDict: typing.TypeAlias = dict[str, Any]
22class Query(_Query): # type: ignore
23 """
24 Pydal Query object.
26 Makes mypy happy.
27 """
30class Expression(_Expression): # type: ignore
31 """
32 Pydal Expression object.
34 Make mypy happy.
35 """
38class Set(_Set): # type: ignore
39 """
40 Pydal Set object.
42 Make mypy happy.
43 """
46class OpRow(_OpRow): # type: ignore
47 """
48 Pydal OpRow object.
50 Make mypy happy.
51 """
54class Reference(_Reference): # type: ignore
55 """
56 Pydal Reference object.
58 Make mypy happy.
59 """
62class Field(_Field): # type: ignore
63 """
64 Pydal Field object.
66 Make mypy happy.
67 """
70class Rows(_Rows): # type: ignore
71 """
72 Pydal Rows object.
74 Make mypy happy.
75 """
78class Validator(_Validator): # type: ignore
79 """
80 Pydal Validator object.
82 Make mypy happy.
83 """
86class _Types:
87 """
88 Internal type storage for stuff that mypy otherwise won't understand.
89 """
91 NONETYPE = type(None)
94BeforeInsertCallable: typing.TypeAlias = typing.Callable[[OpRow], Any]
95AfterInsertCallable: typing.TypeAlias = typing.Callable[[OpRow, Reference], Any]
96BeforeUpdateCallable: typing.TypeAlias = typing.Callable[[Set, OpRow], Any]
97AfterUpdateCallable: typing.TypeAlias = typing.Callable[[Set, OpRow], Any]
98BeforeDeleteCallable: typing.TypeAlias = typing.Callable[[Set], Any]
99AfterDeleteCallable: typing.TypeAlias = typing.Callable[[Set], Any]
102class Pagination(TypedDict):
103 """
104 Pagination key of a paginate dict has these items.
105 """
107 total_items: int
108 current_page: int
109 per_page: int
110 total_pages: int
111 has_next_page: bool
112 has_prev_page: bool
113 next_page: Optional[int]
114 prev_page: Optional[int]
117class PaginateDict(TypedDict):
118 """
119 Result of PaginatedRows.as_dict().
120 """
122 data: dict[int, AnyDict]
123 pagination: Pagination
126class CacheMetadata(TypedDict):
127 """
128 Used by query builder metadata in the 'cache' key.
129 """
131 enabled: bool
132 depends_on: list[Any]
133 key: NotRequired[str | None]
134 status: NotRequired[str | None]
135 expires_at: NotRequired[datetime | None]
136 cached_at: NotRequired[datetime | None]
139class PaginationMetadata(TypedDict):
140 """
141 Used by query builder metadata in the 'pagination' key.
142 """
144 limit: int
145 current_page: int
146 max_page: int
147 rows: int
148 min_max: tuple[int, int]
151class Metadata(TypedDict):
152 """
153 Loosely structured metadata used by Query Builder.
154 """
156 cache: NotRequired[CacheMetadata]
157 pagination: NotRequired[PaginationMetadata]
159 query: NotRequired[Query | str | None]
160 ids: NotRequired[str]
162 final_query: NotRequired[Query | str | None]
163 final_args: NotRequired[list[Any]]
164 final_kwargs: NotRequired[AnyDict]
165 relationships: NotRequired[set[str]]
167 sql: NotRequired[str]