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

61 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-11-21 18:27 +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 typing_extensions import NotRequired 

16 

17 

18class Query(_Query): # type: ignore 

19 """ 

20 Pydal Query object. 

21 

22 Makes mypy happy. 

23 """ 

24 

25 

26class Expression(_Expression): # type: ignore 

27 """ 

28 Pydal Expression object. 

29 

30 Make mypy happy. 

31 """ 

32 

33 

34class Set(_Set): # type: ignore 

35 """ 

36 Pydal Set object. 

37 

38 Make mypy happy. 

39 """ 

40 

41 

42class OpRow(_OpRow): # type: ignore 

43 """ 

44 Pydal OpRow object. 

45 

46 Make mypy happy. 

47 """ 

48 

49 

50class Reference(_Reference): # type: ignore 

51 """ 

52 Pydal Reference object. 

53 

54 Make mypy happy. 

55 """ 

56 

57 

58class Field(_Field): # type: ignore 

59 """ 

60 Pydal Field object. 

61 

62 Make mypy happy. 

63 """ 

64 

65 

66class Rows(_Rows): # type: ignore 

67 """ 

68 Pydal Rows object. 

69 

70 Make mypy happy. 

71 """ 

72 

73 

74class _Types: 

75 """ 

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

77 """ 

78 

79 NONETYPE = type(None) 

80 

81 

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

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

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

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

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

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

88 

89 

90class Pagination(TypedDict): 

91 """ 

92 Pagination key of a paginate dict has these items. 

93 """ 

94 

95 total_items: int 

96 current_page: int 

97 per_page: int 

98 total_pages: int 

99 has_next_page: bool 

100 has_prev_page: bool 

101 next_page: Optional[int] 

102 prev_page: Optional[int] 

103 

104 

105class PaginateDict(TypedDict): 

106 """ 

107 Result of PaginatedRows.as_dict(). 

108 """ 

109 

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

111 pagination: Pagination 

112 

113 

114class CacheMetadata(TypedDict): 

115 """ 

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

117 """ 

118 

119 enabled: bool 

120 depends_on: list[Any] 

121 key: NotRequired[str | None] 

122 status: NotRequired[str | None] 

123 expires_at: NotRequired[datetime | None] 

124 cached_at: NotRequired[datetime | None] 

125 

126 

127class PaginationMetadata(TypedDict): 

128 """ 

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

130 """ 

131 

132 limit: int 

133 current_page: int 

134 max_page: int 

135 rows: int 

136 min_max: tuple[int, int] 

137 

138 

139class Metadata(TypedDict): 

140 """ 

141 Loosely structured metadata used by Query Builder. 

142 """ 

143 

144 cache: NotRequired[CacheMetadata] 

145 pagination: NotRequired[PaginationMetadata] 

146 

147 query: NotRequired[Query | str | None] 

148 ids: NotRequired[str] 

149 

150 final_query: NotRequired[Query | str | None] 

151 final_args: NotRequired[list[Any]] 

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

153 relationships: NotRequired[set[str]] 

154 

155 sql: NotRequired[str]