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 typing_extensions import NotRequired

16

17

18 class Query(_Query): # type: ignore

19 """

20 Pydal Query object.

21

22 Makes mypy happy.

23 """

24

25

26 class Expression(_Expression): # type: ignore

27 """

28 Pydal Expression object.

29

30 Make mypy happy.

31 """

32

33

34 class Set(_Set): # type: ignore

35 """

36 Pydal Set object.

37

38 Make mypy happy.

39 """

40

41

42 class OpRow(_OpRow): # type: ignore

43 """

44 Pydal OpRow object.

45

46 Make mypy happy.

47 """

48

49

50 class Reference(_Reference): # type: ignore

51 """

52 Pydal Reference object.

53

54 Make mypy happy.

55 """

56

57

58 class Field(_Field): # type: ignore

59 """

60 Pydal Field object.

61

62 Make mypy happy.

63 """

64

65

66 class Rows(_Rows): # type: ignore

67 """

68 Pydal Rows object.

69

70 Make mypy happy.

71 """

72

73

74 class _Types:

75 """

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

77 """

78

79 NONETYPE = type(None)

80

81

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

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

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

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

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

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

88

89

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

105 class PaginateDict(TypedDict):

106 """

107 Result of PaginatedRows.as_dict().

108 """

109

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

111 pagination: Pagination

112

113

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

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

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