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

59 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-11-20 17:00 +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 Set as _Set 

14from typing_extensions import NotRequired 

15 

16 

17class Query(_Query): # type: ignore 

18 """ 

19 Pydal Query object. 

20 

21 Makes mypy happy. 

22 """ 

23 

24 

25class Expression(_Expression): # type: ignore 

26 """ 

27 Pydal Expression object. 

28 

29 Make mypy happy. 

30 """ 

31 

32 

33class Set(_Set): 

34 """ 

35 Pydal Set object. 

36 

37 Make mypy happy. 

38 """ 

39 

40 

41class OpRow(_OpRow): 

42 """ 

43 Pydal OpRow object. 

44 

45 Make mypy happy. 

46 """ 

47 

48 

49class Reference(_Reference): 

50 """ 

51 Pydal Reference object. 

52 

53 Make mypy happy. 

54 """ 

55 

56 

57class Field(_Field): 

58 """ 

59 Pydal Field object. 

60 

61 Make mypy happy. 

62 """ 

63 

64 

65class _Types: 

66 """ 

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

68 """ 

69 

70 NONETYPE = type(None) 

71 

72 

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

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

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

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

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

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

79 

80 

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

96class PaginateDict(TypedDict): 

97 """ 

98 Result of PaginatedRows.as_dict(). 

99 """ 

100 

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

102 pagination: Pagination 

103 

104 

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

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

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