Skip to content

Commit 98dd599

Browse files
committed
downgrade type annotations
1 parent 1bddcaa commit 98dd599

File tree

2 files changed

+37
-41
lines changed

2 files changed

+37
-41
lines changed

ninja/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from math import inf
2-
from typing import Dict, Optional, Set
2+
from typing import Dict, Optional, Set, Tuple
33

44
from django.conf import settings as django_settings
55
from pydantic import BaseModel, ConfigDict, Field
@@ -11,7 +11,7 @@ class Settings(BaseModel):
1111
PAGINATION_CLASS: str = Field(
1212
"ninja.pagination.LimitOffsetPagination", alias="NINJA_PAGINATION_CLASS"
1313
)
14-
PAGINATION_DEFAULT_ORDERING: tuple[str, ...] = Field(
14+
PAGINATION_DEFAULT_ORDERING: Tuple[str, ...] = Field(
1515
("-pk",), alias="NINJA_PAGINATION_DEFAULT_ORDERING"
1616
)
1717
PAGINATION_MAX_OFFSET: int = Field(100, alias="NINJA_PAGINATION_MAX_OFFSET")

ninja/pagination.py

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from functools import partial, wraps
66
from math import inf
77
from typing import (
8-
Annotated,
98
Any,
109
AsyncGenerator,
1110
Callable,
@@ -202,7 +201,7 @@ class CursorPagination(AsyncPaginationBase):
202201
def __init__(
203202
self,
204203
*,
205-
ordering: tuple[str, ...] = settings.PAGINATION_DEFAULT_ORDERING,
204+
ordering: Tuple[str, ...] = settings.PAGINATION_DEFAULT_ORDERING,
206205
page_size: int = settings.PAGINATION_PER_PAGE,
207206
max_page_size: int = settings.PAGINATION_MAX_PER_PAGE_SIZE,
208207
**kwargs: Any,
@@ -221,13 +220,13 @@ def __init__(
221220
super().__init__(**kwargs)
222221

223222
class Input(Schema):
224-
page_size: int | None = None
225-
cursor: str | None = None
223+
page_size: Optional[int] = None
224+
cursor: Optional[str] = None
226225

227226
class Output(Schema):
228-
previous: str | None
229-
next: str | None
230-
results: list[Any]
227+
previous: Optional[str]
228+
next: Optional[str]
229+
results: List[Any]
231230

232231
class Cursor(BaseModel):
233232
"""
@@ -237,31 +236,28 @@ class Cursor(BaseModel):
237236
238237
"""
239238

240-
p: Annotated[
241-
str | None,
242-
Field(
243-
title="position",
244-
description="String identifier for the current position in the dataset",
245-
),
246-
] = None
247-
r: Annotated[
248-
bool,
249-
Field(
250-
title="reverse", description="Whether to reverse the ordering direction"
251-
),
252-
] = False
239+
p: Optional[str] = Field(
240+
None,
241+
title="position",
242+
description="String identifier for the current position in the dataset",
243+
)
244+
245+
r: bool = Field(
246+
False,
247+
title="reverse",
248+
description="Whether to reverse the ordering direction",
249+
)
250+
253251
# offset enables the use of a non-unique ordering field
254252
# e.g. if created time of two items is exactly the same, we can use the offset
255253
# to figure out the position exactly
256-
o: Annotated[
257-
int,
258-
Field(
259-
ge=0,
260-
lt=settings.PAGINATION_MAX_OFFSET,
261-
title="offset",
262-
description="Number of items to skip from the current position",
263-
),
264-
] = 0
254+
o: int = Field(
255+
0,
256+
ge=0,
257+
lt=settings.PAGINATION_MAX_OFFSET,
258+
title="offset",
259+
description="Number of items to skip from the current position",
260+
)
265261

266262
@field_validator("*", mode="before")
267263
@classmethod
@@ -278,7 +274,7 @@ def validate_individual_queryparam(cls, value: Any) -> Any:
278274

279275
@classmethod
280276
def from_encoded_param(
281-
cls, encoded_param: str | None, context: Any = None
277+
cls, encoded_param: Optional[str], context: Any = None
282278
) -> "CursorPagination.Cursor":
283279
"""
284280
Deserialize cursor from URL-safe base64 token.
@@ -306,7 +302,7 @@ def encode_as_param(self) -> str:
306302
return b64encode(query_string.encode("ascii")).decode("ascii")
307303

308304
@staticmethod
309-
def _reverse_order(order: tuple[str, ...]) -> tuple[str, ...]:
305+
def _reverse_order(order: Tuple[str, ...]) -> Tuple[str, ...]:
310306
"""
311307
Flip ordering direction for backward pagination.
312308
@@ -326,7 +322,7 @@ def _get_position(self, item: Any) -> str:
326322
"""
327323
return str(getattr(item, self._order_attribute))
328324

329-
def _get_page_size(self, requested_page_size: int | None) -> int:
325+
def _get_page_size(self, requested_page_size: Optional[int]) -> int:
330326
"""
331327
Determine the actual page size to use, respecting configured limits.
332328
@@ -341,9 +337,9 @@ def _get_page_size(self, requested_page_size: int | None) -> int:
341337
def _build_next_cursor(
342338
self,
343339
current_cursor: Cursor,
344-
results: list[Any],
345-
additional_position: str | None = None,
346-
) -> Cursor | None:
340+
results: List[Any],
341+
additional_position: Optional[str] = None,
342+
) -> Optional[Cursor]:
347343
"""
348344
Build cursor for next page
349345
"""
@@ -376,9 +372,9 @@ def _build_next_cursor(
376372
def _build_previous_cursor(
377373
self,
378374
current_cursor: Cursor,
379-
results: list[Any],
380-
additional_position: str | None = None,
381-
) -> Cursor | None:
375+
results: List[Any],
376+
additional_position: Optional[str] = None,
377+
) -> Optional[Cursor]:
382378
"""
383379
Build cursor for previous page
384380
"""
@@ -418,7 +414,7 @@ def _build_previous_cursor(
418414
return self.Cursor(o=offset, r=True, p=previous_position)
419415

420416
@staticmethod
421-
def _add_cursor_to_URL(url: str, cursor: Cursor | None) -> str | None:
417+
def _add_cursor_to_URL(url: str, cursor: Optional[Cursor]) -> Optional[str]:
422418
"""
423419
Build pagination URLs with an encoded cursor.
424420

0 commit comments

Comments
 (0)