Skip to content

Commit

Permalink
Fixed coverage on Python>=3.11 (#1276)
Browse files Browse the repository at this point in the history
* Fixed coverage on Python>=3.10

* Really fixed?..

* Just skipping actually

* Just skipping actually

* Import

* Skip reason

* fmt

* should include 3.10 actually

* nevermind
  • Loading branch information
c4ffein authored Sep 19, 2024
1 parent 32d5df4 commit de40a7f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
3 changes: 1 addition & 2 deletions ninja/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,7 @@ class PagedSome:
try:
new_name = f"Paged{item_schema.__name__}"
except AttributeError:
new_name = f"Paged{str(item_schema).replace('.', '_')}" # typing.Any case

new_name = f"Paged{str(item_schema).replace('.', '_')}" # typing.Any case, only for Python < 3.11
new_schema = type(
new_name,
(paginator.Output,),
Expand Down
25 changes: 24 additions & 1 deletion tests/test_pagination.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import importlib
from sys import version_info
from typing import Any, List

import pytest
from django.test import override_settings
from pydantic.errors import PydanticSchemaGenerationError

from ninja import NinjaAPI, Schema
from ninja.errors import ConfigError
from ninja.pagination import PageNumberPagination, PaginationBase, paginate
from ninja.operation import Operation
from ninja.pagination import (
LimitOffsetPagination,
PageNumberPagination,
PaginationBase,
make_response_paginated,
paginate,
)
from ninja.testing import TestClient

api = NinjaAPI()
Expand Down Expand Up @@ -417,3 +426,17 @@ def test_config_error_NOT_SET():
@paginate
def invalid2(request):
pass


@pytest.mark.skipif(version_info < (3, 11), reason="Not needed at this Python version")
def test_pagination_works_with_unnamed_classes():
"""
This test lets you check that the typing.Any case handled in `ninja.pagination.make_response_paginated`
works for Python>=3.11, as a typing.Any does possess the __name__ atribute past that version
"""
operation = Operation("/whatever", ["GET"], lambda: None, response=List[int])
operation.response_models[200].__annotations__["response"] = List[object()]
with pytest.raises(
PydanticSchemaGenerationError
): # It does fail after we passed the logic that we are testing
make_response_paginated(LimitOffsetPagination, operation)

0 comments on commit de40a7f

Please sign in to comment.