Coverage for src/typedal/types.py: 100%
59 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-20 17:00 +0100
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-20 17:00 +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 Set as _Set
14from typing_extensions import NotRequired
17class Query(_Query): # type: ignore
18 """
19 Pydal Query object.
21 Makes mypy happy.
22 """
25class Expression(_Expression): # type: ignore
26 """
27 Pydal Expression object.
29 Make mypy happy.
30 """
33class Set(_Set):
34 """
35 Pydal Set object.
37 Make mypy happy.
38 """
41class OpRow(_OpRow):
42 """
43 Pydal OpRow object.
45 Make mypy happy.
46 """
49class Reference(_Reference):
50 """
51 Pydal Reference object.
53 Make mypy happy.
54 """
57class Field(_Field):
58 """
59 Pydal Field object.
61 Make mypy happy.
62 """
65class _Types:
66 """
67 Internal type storage for stuff that mypy otherwise won't understand.
68 """
70 NONETYPE = type(None)
73BeforeInsertCallable: typing.TypeAlias = typing.Callable[[OpRow], Any]
74AfterInsertCallable: typing.TypeAlias = typing.Callable[[OpRow, Reference], Any]
75BeforeUpdateCallable: typing.TypeAlias = typing.Callable[[Set, OpRow], Any]
76AfterUpdateCallable: typing.TypeAlias = typing.Callable[[Set, OpRow], Any]
77BeforeDeleteCallable: typing.TypeAlias = typing.Callable[[Set], Any]
78AfterDeleteCallable: typing.TypeAlias = typing.Callable[[Set], Any]
81class Pagination(TypedDict):
82 """
83 Pagination key of a paginate dict has these items.
84 """
86 total_items: int
87 current_page: int
88 per_page: int
89 total_pages: int
90 has_next_page: bool
91 has_prev_page: bool
92 next_page: Optional[int]
93 prev_page: Optional[int]
96class PaginateDict(TypedDict):
97 """
98 Result of PaginatedRows.as_dict().
99 """
101 data: dict[int, dict[str, Any]]
102 pagination: Pagination
105class CacheMetadata(TypedDict):
106 """
107 Used by query builder metadata in the 'cache' key.
108 """
110 enabled: bool
111 depends_on: list[Any]
112 key: NotRequired[str | None]
113 status: NotRequired[str | None]
114 expires_at: NotRequired[datetime | None]
115 cached_at: NotRequired[datetime | None]
118class PaginationMetadata(TypedDict):
119 """
120 Used by query builder metadata in the 'pagination' key.
121 """
123 limit: int
124 current_page: int
125 max_page: int
126 rows: int
127 min_max: tuple[int, int]
130class Metadata(TypedDict):
131 """
132 Loosely structured metadata used by Query Builder.
133 """
135 cache: NotRequired[CacheMetadata]
136 pagination: NotRequired[PaginationMetadata]
138 query: NotRequired[Query | str | None]
139 ids: NotRequired[str]
141 final_query: NotRequired[Query | str | None]
142 final_args: NotRequired[list[Any]]
143 final_kwargs: NotRequired[dict[str, Any]]
144 relationships: NotRequired[set[str]]
146 sql: NotRequired[str]