Skip to content

Commit ba2afed

Browse files
authored
Merge pull request #127 from eadwinCode/api_controller_typing
fix mypy typing error
2 parents caaa1cd + 12fa932 commit ba2afed

File tree

8 files changed

+30
-26
lines changed

8 files changed

+30
-26
lines changed

ninja_extra/constants.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
THROTTLED_FUNCTION = "__throttled_endpoint__"
1818
ROUTE_FUNCTION = "__route_function__"
1919

20-
ROUTE_CONTEXT_VAR: contextvars.ContextVar[
21-
t.Optional["RouteContext"]
22-
] = contextvars.ContextVar("ROUTE_CONTEXT_VAR")
20+
ROUTE_CONTEXT_VAR: contextvars.ContextVar[t.Optional["RouteContext"]] = (
21+
contextvars.ContextVar("ROUTE_CONTEXT_VAR")
22+
)
2323
ROUTE_CONTEXT_VAR.set(None)

ninja_extra/controllers/base.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555

5656
from .route.context import RouteContext
5757

58+
T = TypeVar("T")
59+
5860

5961
class MissingAPIControllerDecoratorException(Exception):
6062
pass
@@ -243,6 +245,12 @@ class SomeController(ControllerBase):
243245
model_config: Optional[ModelConfig] = None
244246

245247

248+
ControllerClassType = TypeVar(
249+
"ControllerClassType",
250+
bound=Union[Type[ControllerBase], Type],
251+
)
252+
253+
246254
class APIController:
247255
_PATH_PARAMETER_COMPONENT_RE = r"{(?:(?P<converter>[^>:]+):)?(?P<parameter>[^>]+)}"
248256

@@ -339,13 +347,13 @@ def tags(self, value: Union[str, List[str], None]) -> None:
339347
tag = [value]
340348
self._tags = tag
341349

342-
def __call__(self, cls: Type) -> Type[ControllerBase]:
350+
def __call__(self, cls: ControllerClassType) -> ControllerClassType:
343351
from ninja_extra.throttling import throttle
344352

345353
self.auto_import = getattr(cls, "auto_import", self.auto_import)
346354
if not issubclass(cls, ControllerBase):
347355
# We force the cls to inherit from `ControllerBase` by creating another type.
348-
cls = type(cls.__name__, (ControllerBase, cls), {"_api_controller": self})
356+
cls = type(cls.__name__, (ControllerBase, cls), {"_api_controller": self}) # type:ignore[assignment]
349357
else:
350358
cls._api_controller = self
351359

@@ -388,7 +396,7 @@ def __call__(self, cls: Type) -> Type[ControllerBase]:
388396
fail_silently(inject, constructor_or_class=cls)
389397

390398
ControllerRegistry().add_controller(cls)
391-
return cls
399+
return cls # type:ignore[return-value]
392400

393401
@property
394402
def path_operations(self) -> Dict[str, PathView]:
@@ -502,8 +510,8 @@ def add_api_operation(
502510

503511
@overload
504512
def api_controller(
505-
prefix_or_class: Type,
506-
) -> Type[ControllerBase]: # pragma: no cover
513+
prefix_or_class: Type[T],
514+
) -> Union[Type[ControllerBase], Type[T]]: # pragma: no cover
507515
...
508516

509517

@@ -514,20 +522,17 @@ def api_controller(
514522
tags: Union[Optional[List[str]], str] = None,
515523
permissions: Optional["PermissionType"] = None,
516524
auto_import: bool = True,
517-
) -> Callable[[Type], Type[ControllerBase]]: # pragma: no cover
525+
) -> Callable[[Type[T]], Union[Type[ControllerBase], Type[T]]]: # pragma: no cover
518526
...
519527

520528

521-
InputClassType = TypeVar("InputClassType", bound=ControllerBase)
522-
523-
524529
def api_controller(
525-
prefix_or_class: Union[str, Type] = "",
530+
prefix_or_class: Union[str, ControllerClassType] = "",
526531
auth: Any = NOT_SET,
527532
tags: Union[Optional[List[str]], str] = None,
528533
permissions: Optional["PermissionType"] = None,
529534
auto_import: bool = True,
530-
) -> Union[Type[ControllerBase], Callable[[InputClassType], Type[InputClassType]]]:
535+
) -> Union[ControllerClassType, Callable[[ControllerClassType], ControllerClassType]]:
531536
if isinstance(prefix_or_class, type):
532537
return APIController(
533538
prefix="",
@@ -537,7 +542,7 @@ def api_controller(
537542
auto_import=auto_import,
538543
)(prefix_or_class)
539544

540-
def _decorator(cls: InputClassType) -> Type[InputClassType]:
545+
def _decorator(cls: ControllerClassType) -> ControllerClassType:
541546
return APIController(
542547
prefix=str(prefix_or_class),
543548
auth=auth,

ninja_extra/controllers/model/schemas.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
ModelSchemaConfigAdapter,
1616
)
1717
except Exception: # pragma: no cover
18-
ConfigError = (
19-
NinjaSchemaModelSchemaConfig
20-
) = ModelSchemaConfigAdapter = SchemaFactory = None
18+
ConfigError = NinjaSchemaModelSchemaConfig = ModelSchemaConfigAdapter = (
19+
SchemaFactory
20+
) = None
2121

2222

2323
from ...pagination import PageNumberPaginationExtra, PaginatedResponseSchema

ninja_extra/exceptions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
DRF Exceptions
33
"""
4+
45
import math
56
from typing import Any, Dict, List, Optional, Type, Union, no_type_check
67

ninja_extra/ordering.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@
4141

4242

4343
class OrderingBase(ABC):
44-
class Input(Schema):
45-
...
44+
class Input(Schema): ...
4645

4746
InputSource = Query(...)
4847

@@ -52,8 +51,7 @@ def __init__(self, *, pass_parameter: Optional[str] = None, **kwargs: Any) -> No
5251
@abstractmethod
5352
def ordering_queryset(
5453
self, items: Union[QuerySet, List], ordering_input: Any
55-
) -> Union[QuerySet, List]:
56-
...
54+
) -> Union[QuerySet, List]: ...
5755

5856

5957
class Ordering(OrderingBase):

ninja_extra/permissions/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Copied from DRF
33
Provides a set of pluggable permission policies.
44
"""
5+
56
from abc import ABC, ABCMeta, abstractmethod
67
from typing import TYPE_CHECKING, Any, Generic, Tuple, Type, TypeVar, Union
78

ninja_extra/searching.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ def _isicontains(a: str, b: str) -> bool:
5656

5757

5858
class SearchingBase(ABC):
59-
class Input(Schema):
60-
...
59+
class Input(Schema): ...
6160

6261
InputSource = Query(...)
6362

@@ -67,8 +66,7 @@ def __init__(self, *, pass_parameter: Optional[str] = None, **kwargs: Any) -> No
6766
@abstractmethod
6867
def searching_queryset(
6968
self, items: Union[QuerySet, List], searching_input: Any
70-
) -> Union[QuerySet, List]:
71-
...
69+
) -> Union[QuerySet, List]: ...
7270

7371

7472
class Searching(SearchingBase):

ninja_extra/throttling/model.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Provides various throttling policies.
33
From DjangoRestFramework - https://github.com/encode/django-rest-framework/blob/master/rest_framework/throttling.py
44
"""
5+
56
import time
67
from typing import Any, Callable, Dict, List, Optional, Tuple, cast
78

0 commit comments

Comments
 (0)