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 Set as _Set

14 from typing_extensions import NotRequired

15

16

17 class Query(_Query): # type: ignore

18 """

19 Pydal Query object.

20

21 Makes mypy happy.

22 """

23

24

25 class Expression(_Expression): # type: ignore

26 """

27 Pydal Expression object.

28

29 Make mypy happy.

30 """

31

32

33 class Set(_Set):

34 """

35 Pydal Set object.

36

37 Make mypy happy.

38 """

39

40

41 class OpRow(_OpRow):

42 """

43 Pydal OpRow object.

44

45 Make mypy happy.

46 """

47

48

49 class Reference(_Reference):

50 """

51 Pydal Reference object.

52

53 Make mypy happy.

54 """

55

56

57 class Field(_Field):

58 """

59 Pydal Field object.

60

61 Make mypy happy.

62 """

63

64

65 class _Types:

66 """

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

68 """

69

70 NONETYPE = type(None)

71

72

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

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

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

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

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

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

79

80

81 class Pagination(TypedDict):

82 """

83 Pagination key of a paginate dict has these items.

84 """

85

86 total_items: int

87 current_page: int

88 per_page: int

89 total_pages: int

90 has_next_page: bool

91 has_prev_page: bool

92 next_page: Optional[int]

93 prev_page: Optional[int]

94

95

96 class PaginateDict(TypedDict):

97 """

98 Result of PaginatedRows.as_dict().

99 """

100

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

102 pagination: Pagination

103

104

105 class CacheMetadata(TypedDict):

106 """

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

108 """

109

110 enabled: bool

111 depends_on: list[Any]

112 key: NotRequired[str | None]

113 status: NotRequired[str | None]

114 expires_at: NotRequired[datetime | None]

115 cached_at: NotRequired[datetime | None]

116

117

118 class PaginationMetadata(TypedDict):

119 """

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

121 """

122

123 limit: int

124 current_page: int

125 max_page: int

126 rows: int

127 min_max: tuple[int, int]

128

129

130 class Metadata(TypedDict):

131 """

132 Loosely structured metadata used by Query Builder.

133 """

134

135 cache: NotRequired[CacheMetadata]

136 pagination: NotRequired[PaginationMetadata]

137

138 query: NotRequired[Query | str | None]

139 ids: NotRequired[str]

140

141 final_query: NotRequired[Query | str | None]

142 final_args: NotRequired[list[Any]]

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

144 relationships: NotRequired[set[str]]

145

146 sql: NotRequired[str]