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

1"""

2 Stuff to make mypy happy.

3"""

4 import typing

5 from datetime import datetime

6 from typing import Any, Optional, TypedDict

7

8 from pydal.helpers.classes import OpRow as _OpRow

9 from pydal.helpers.classes import Reference as _Reference

10 from pydal.objects import Expression as _Expression

11 from pydal.objects import Field as _Field

12 from pydal.objects import Query as _Query

13 from pydal.objects import Rows as _Rows

14 from pydal.objects import Set as _Set

15 from pydal.validators import Validator as _Validator

16 from typing_extensions import NotRequired

17

18

19 class Query(_Query): # type: ignore

20 """

21 Pydal Query object.

22

23 Makes mypy happy.

24 """

25

26

27 class Expression(_Expression): # type: ignore

28 """

29 Pydal Expression object.

30

31 Make mypy happy.

32 """

33

34

35 class Set(_Set): # type: ignore

36 """

37 Pydal Set object.

38

39 Make mypy happy.

40 """

41

42

43 class OpRow(_OpRow): # type: ignore

44 """

45 Pydal OpRow object.

46

47 Make mypy happy.

48 """

49

50

51 class Reference(_Reference): # type: ignore

52 """

53 Pydal Reference object.

54

55 Make mypy happy.

56 """

57

58

59 class Field(_Field): # type: ignore

60 """

61 Pydal Field object.

62

63 Make mypy happy.

64 """

65

66

67 class Rows(_Rows): # type: ignore

68 """

69 Pydal Rows object.

70

71 Make mypy happy.

72 """

73

74

75 class Validator(_Validator): # type: ignore

76 """

77 Pydal Validator object.

78

79 Make mypy happy.

80 """

81

82

83 class _Types:

84 """

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

86 """

87

88 NONETYPE = type(None)

89

90

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

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

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

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

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

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

97

98

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

114 class PaginateDict(TypedDict):

115 """

116 Result of PaginatedRows.as_dict().

117 """

118

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

120 pagination: Pagination

121

122

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

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

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