11from math import ceil
2- from typing import Collection , List , Union
2+ from typing import List , Union
33
44from sqlalchemy import inspect
55
@@ -56,18 +56,11 @@ def build_result(
5656 index = - 1
5757 reference_column = cursor_reference .column
5858 last_found_cursor_value = getattr (result_items [index ], reference_column )
59- """
60- Currently we support only numeric or string model values for cursors,
61- but pydantic models (cursor) coerce always the value as string.
62- This mean if the value is not actually string we need to cast to
63- ensure correct ordering is evaluated.
64- e.g.
65- 9 < 10 but '9' > '10'
66- """
67- if isinstance (last_found_cursor_value , str ):
68- has_next_page = last_found_cursor_value >= cursor_reference .value
69- else :
70- has_next_page = last_found_cursor_value >= float (cursor_reference .value )
59+ if not isinstance (last_found_cursor_value , type (cursor_reference .value )):
60+ raise TypeError (
61+ "Values from CursorReference and results must be of the same type"
62+ )
63+ has_next_page = last_found_cursor_value >= cursor_reference .value
7164 if has_next_page :
7265 result_items .pop (index )
7366 has_previous_page = len (result_items ) > items_per_page
@@ -77,20 +70,11 @@ def build_result(
7770 index = 0
7871 reference_column = cursor_reference .column
7972 first_found_cursor_value = getattr (result_items [index ], reference_column )
80- """
81- Currently we support only numeric or string model values for cursors,
82- but pydantic models (cursor) coerce always the value as string.
83- This mean if the value is not actually string we need to cast to
84- ensure correct ordering is evaluated.
85- e.g.
86- 9 < 10 but '9' > '10'
87- """
88- if isinstance (first_found_cursor_value , str ):
89- has_previous_page = first_found_cursor_value <= cursor_reference .value
90- else :
91- has_previous_page = first_found_cursor_value <= float (
92- cursor_reference .value
73+ if not isinstance (first_found_cursor_value , type (cursor_reference .value )):
74+ raise TypeError (
75+ "Values from CursorReference and results must be of the same type"
9376 )
77+ has_previous_page = first_found_cursor_value <= cursor_reference .value
9478 if has_previous_page :
9579 result_items .pop (index )
9680 has_next_page = len (result_items ) > items_per_page
@@ -117,21 +101,20 @@ def build_result(
117101class PaginatedResultPresenter :
118102 @staticmethod
119103 def build_result (
120- result_items : Collection [MODEL ],
104+ result_items : List [MODEL ],
121105 total_items_count : int ,
122106 page : int ,
123107 items_per_page : int ,
124108 ) -> PaginatedResult :
125-
126109 total_pages = (
127110 0
128111 if total_items_count == 0 or total_items_count is None
129112 else ceil (total_items_count / items_per_page )
130113 )
131114
132115 _page = 0 if len (result_items ) == 0 else min (page , total_pages )
133- has_next_page = _page and _page < total_pages
134- has_previous_page = _page and _page > 1
116+ has_next_page = bool ( _page and _page < total_pages )
117+ has_previous_page = bool ( _page and _page > 1 )
135118
136119 return PaginatedResult (
137120 items = result_items ,
0 commit comments