Coverage for src/typedal/types.py: 100%

1"""

2 Stuff to make mypy happy.

3"""

4

5 import typing

6 from datetime import datetime

7 from typing import Any, Optional, TypedDict

8

9 from pydal.helpers.classes import OpRow as _OpRow

10 from pydal.helpers.classes import Reference as _Reference

11 from pydal.objects import Expression as _Expression

12 from pydal.objects import Field as _Field

13 from pydal.objects import Query as _Query

14 from pydal.objects import Rows as _Rows

15 from pydal.objects import Set as _Set

16 from pydal.validators import Validator as _Validator

17 from typing_extensions import NotRequired

18

19 AnyDict: typing.TypeAlias = dict[str, Any]

20

21

22 class Query(_Query): # type: ignore

23 """

24 Pydal Query object.

25

26 Makes mypy happy.

27 """

28

29

30 class Expression(_Expression): # type: ignore

31 """

32 Pydal Expression object.

33

34 Make mypy happy.

35 """

36

37

38 class Set(_Set): # type: ignore

39 """

40 Pydal Set object.

41

42 Make mypy happy.

43 """

44

45

46 class OpRow(_OpRow): # type: ignore

47 """

48 Pydal OpRow object.

49

50 Make mypy happy.

51 """

52

53

54 class Reference(_Reference): # type: ignore

55 """

56 Pydal Reference object.

57

58 Make mypy happy.

59 """

60

61

62 class Field(_Field): # type: ignore

63 """

64 Pydal Field object.

65

66 Make mypy happy.

67 """

68

69

70 class Rows(_Rows): # type: ignore

71 """

72 Pydal Rows object.

73

74 Make mypy happy.

75 """

76

77

78 class Validator(_Validator): # type: ignore

79 """

80 Pydal Validator object.

81

82 Make mypy happy.

83 """

84

85

86 class _Types:

87 """

88 Internal type storage for stuff that mypy otherwise won't understand.

89 """

90

91 NONETYPE = type(None)

92

93

94 BeforeInsertCallable: typing.TypeAlias = typing.Callable[[OpRow], Any]

95 AfterInsertCallable: typing.TypeAlias = typing.Callable[[OpRow, Reference], Any]

96 BeforeUpdateCallable: typing.TypeAlias = typing.Callable[[Set, OpRow], Any]

97 AfterUpdateCallable: typing.TypeAlias = typing.Callable[[Set, OpRow], Any]

98 BeforeDeleteCallable: typing.TypeAlias = typing.Callable[[Set], Any]

99 AfterDeleteCallable: typing.TypeAlias = typing.Callable[[Set], Any]

100

101

102 class Pagination(TypedDict):

103 """

104 Pagination key of a paginate dict has these items.

105 """

106

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]

115

116

117 class PaginateDict(TypedDict):

118 """

119 Result of PaginatedRows.as_dict().

120 """

121

122 data: dict[int, AnyDict]

123 pagination: Pagination

124

125

126 class CacheMetadata(TypedDict):

127 """

128 Used by query builder metadata in the 'cache' key.

129 """

130

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]

137

138

139 class PaginationMetadata(TypedDict):

140 """

141 Used by query builder metadata in the 'pagination' key.

142 """

143

144 limit: int

145 current_page: int

146 max_page: int

147 rows: int

148 min_max: tuple[int, int]

149

150

151 class Metadata(TypedDict):

152 """

153 Loosely structured metadata used by Query Builder.

154 """

155

156 cache: NotRequired[CacheMetadata]

157 pagination: NotRequired[PaginationMetadata]

158

159 query: NotRequired[Query | str | None]

160 ids: NotRequired[str]

161

162 final_query: NotRequired[Query | str | None]

163 final_args: NotRequired[list[Any]]

164 final_kwargs: NotRequired[AnyDict]

165 relationships: NotRequired[set[str]]

166

167 sql: NotRequired[str]