Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed coverage on Python>=3.11 #1276

Merged
merged 9 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading