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

63 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-12-05 14:43 +0100

1""" 

2Stuff to make mypy happy. 

3""" 

4import typing 

5from datetime import datetime 

6from typing import Any, Optional, TypedDict 

7 

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 Rows as _Rows 

14from pydal.objects import Set as _Set 

15from pydal.validators import Validator as _Validator 

16from typing_extensions import NotRequired 

17 

18 

19class Query(_Query): # type: ignore 

20 """ 

21 Pydal Query object. 

22 

23 Makes mypy happy. 

24 """ 

25 

26 

27class Expression(_Expression): # type: ignore 

28 """ 

29 Pydal Expression object. 

30 

31 Make mypy happy. 

32 """ 

33 

34 

35class Set(_Set): # type: ignore 

36 """ 

37 Pydal Set object. 

38 

39 Make mypy happy. 

40 """ 

41 

42 

43class OpRow(_OpRow): # type: ignore 

44 """ 

45 Pydal OpRow object. 

46 

47 Make mypy happy. 

48 """ 

49 

50 

51class Reference(_Reference): # type: ignore 

52 """ 

53 Pydal Reference object. 

54 

55 Make mypy happy. 

56 """ 

57 

58 

59class Field(_Field): # type: ignore 

60 """ 

61 Pydal Field object. 

62 

63 Make mypy happy. 

64 """ 

65 

66 

67class Rows(_Rows): # type: ignore 

68 """ 

69 Pydal Rows object. 

70 

71 Make mypy happy. 

72 """ 

73 

74 

75class Validator(_Validator): # type: ignore 

76 """ 

77 Pydal Validator object. 

78 

79 Make mypy happy. 

80 """ 

81 

82 

83class _Types: 

84 """ 

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

86 """ 

87 

88 NONETYPE = type(None) 

89 

90 

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

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

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

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

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

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

97 

98 

99class Pagination(TypedDict): 

100 """ 

101 Pagination key of a paginate dict has these items. 

102 """ 

103 

104 total_items: int 

105 current_page: int 

106 per_page: int 

107 total_pages: int 

108 has_next_page: bool 

109 has_prev_page: bool 

110 next_page: Optional[int] 

111 prev_page: Optional[int] 

112 

113 

114class PaginateDict(TypedDict): 

115 """ 

116 Result of PaginatedRows.as_dict(). 

117 """ 

118 

119 data: dict[int, dict[str, Any]] 

120 pagination: Pagination 

121 

122 

123class CacheMetadata(TypedDict): 

124 """ 

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

126 """ 

127 

128 enabled: bool 

129 depends_on: list[Any] 

130 key: NotRequired[str | None] 

131 status: NotRequired[str | None] 

132 expires_at: NotRequired[datetime | None] 

133 cached_at: NotRequired[datetime | None] 

134 

135 

136class PaginationMetadata(TypedDict): 

137 """ 

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

139 """ 

140 

141 limit: int 

142 current_page: int 

143 max_page: int 

144 rows: int 

145 min_max: tuple[int, int] 

146 

147 

148class Metadata(TypedDict): 

149 """ 

150 Loosely structured metadata used by Query Builder. 

151 """ 

152 

153 cache: NotRequired[CacheMetadata] 

154 pagination: NotRequired[PaginationMetadata] 

155 

156 query: NotRequired[Query | str | None] 

157 ids: NotRequired[str] 

158 

159 final_query: NotRequired[Query | str | None] 

160 final_args: NotRequired[list[Any]] 

161 final_kwargs: NotRequired[dict[str, Any]] 

162 relationships: NotRequired[set[str]] 

163 

164 sql: NotRequired[str]