5555
5656 from .route .context import RouteContext
5757
58+ T = TypeVar ("T" )
59+
5860
5961class 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+
246254class 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
504512def 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-
524529def 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 ,
0 commit comments