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

64 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-01-29 16:15 +0100

1""" 

2Stuff to make mypy happy. 

3""" 

4 

5import typing 

6from datetime import datetime 

7from typing import Any, Optional, TypedDict 

8 

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 

18 

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

20 

21 

22class Query(_Query): # type: ignore 

23 """ 

24 Pydal Query object. 

25 

26 Makes mypy happy. 

27 """ 

28 

29 

30class Expression(_Expression): # type: ignore 

31 """ 

32 Pydal Expression object. 

33 

34 Make mypy happy. 

35 """ 

36 

37 

38class Set(_Set): # type: ignore 

39 """ 

40 Pydal Set object. 

41 

42 Make mypy happy. 

43 """ 

44 

45 

46class OpRow(_OpRow): # type: ignore 

47 """ 

48 Pydal OpRow object. 

49 

50 Make mypy happy. 

51 """ 

52 

53 

54class Reference(_Reference): # type: ignore 

55 """ 

56 Pydal Reference object. 

57 

58 Make mypy happy. 

59 """ 

60 

61 

62class Field(_Field): # type: ignore 

63 """ 

64 Pydal Field object. 

65 

66 Make mypy happy. 

67 """ 

68 

69 

70class Rows(_Rows): # type: ignore 

71 """ 

72 Pydal Rows object. 

73 

74 Make mypy happy. 

75 """ 

76 

77 

78class Validator(_Validator): # type: ignore 

79 """ 

80 Pydal Validator object. 

81 

82 Make mypy happy. 

83 """ 

84 

85 

86class _Types: 

87 """ 

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

89 """ 

90 

91 NONETYPE = type(None) 

92 

93 

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] 

100 

101 

102class 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 

117class PaginateDict(TypedDict): 

118 """ 

119 Result of PaginatedRows.as_dict(). 

120 """ 

121 

122 data: dict[int, AnyDict] 

123 pagination: Pagination 

124 

125 

126class 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 

139class 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 

151class 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]