Skip to content

Commit 8d03fa9

Browse files
committed
inline the Decorator type alias
unfortunately the alias doesn't work see e.g. python/mypy#3924 ``` from typing import Any, Callable, TypeVar TCallable = TypeVar("TCallable", bound=Callable[..., Any]) Decorator = Callable[[TCallable], TCallable] def direct() -> Callable[[TCallable], TCallable]: ... def alias() -> Decorator[TCallable]: ... def half_alias() -> Decorator: ... reveal_type(direct()) reveal_type(alias()) reveal_type(half_alias()) ``` ``` $ mypy --strict test.py test.py:15:21: error: Missing type parameters for generic type "Decorator" test.py:19:13: note: Revealed type is "def [TCallable <: def (*Any, **Any) -> Any] (TCallable`-1) -> TCallable`-1" test.py:20:13: note: Revealed type is "def (<nothing>) -> <nothing>" test.py:21:13: note: Revealed type is "def (Any) -> Any" Found 1 error in 1 file (checked 1 source file) ```
1 parent 6e076ee commit 8d03fa9

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

ninja/main.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from ninja.parser import Parser
2424
from ninja.renderers import BaseRenderer, JSONRenderer
2525
from ninja.router import Router
26-
from ninja.types import Decorator
26+
from ninja.types import TCallable
2727

2828
if TYPE_CHECKING:
2929
from .operation import Operation # pragma: no cover
@@ -89,7 +89,7 @@ def get(
8989
exclude_none: bool = False,
9090
url_name: Optional[str] = None,
9191
include_in_schema: bool = True,
92-
) -> Decorator:
92+
) -> Callable[[TCallable], TCallable]:
9393
return self.default_router.get(
9494
path,
9595
auth=auth is NOT_SET and self.auth or auth,
@@ -124,7 +124,7 @@ def post(
124124
exclude_none: bool = False,
125125
url_name: Optional[str] = None,
126126
include_in_schema: bool = True,
127-
) -> Decorator:
127+
) -> Callable[[TCallable], TCallable]:
128128
return self.default_router.post(
129129
path,
130130
auth=auth is NOT_SET and self.auth or auth,
@@ -159,7 +159,7 @@ def delete(
159159
exclude_none: bool = False,
160160
url_name: Optional[str] = None,
161161
include_in_schema: bool = True,
162-
) -> Decorator:
162+
) -> Callable[[TCallable], TCallable]:
163163
return self.default_router.delete(
164164
path,
165165
auth=auth is NOT_SET and self.auth or auth,
@@ -194,7 +194,7 @@ def patch(
194194
exclude_none: bool = False,
195195
url_name: Optional[str] = None,
196196
include_in_schema: bool = True,
197-
) -> Decorator:
197+
) -> Callable[[TCallable], TCallable]:
198198
return self.default_router.patch(
199199
path,
200200
auth=auth is NOT_SET and self.auth or auth,
@@ -229,7 +229,7 @@ def put(
229229
exclude_none: bool = False,
230230
url_name: Optional[str] = None,
231231
include_in_schema: bool = True,
232-
) -> Decorator:
232+
) -> Callable[[TCallable], TCallable]:
233233
return self.default_router.put(
234234
path,
235235
auth=auth is NOT_SET and self.auth or auth,
@@ -265,7 +265,7 @@ def api_operation(
265265
exclude_none: bool = False,
266266
url_name: Optional[str] = None,
267267
include_in_schema: bool = True,
268-
) -> Decorator:
268+
) -> Callable[[TCallable], TCallable]:
269269
return self.default_router.api_operation(
270270
methods,
271271
path,

ninja/router.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from ninja.constants import NOT_SET
1616
from ninja.operation import PathView
17-
from ninja.types import Decorator, TCallable
17+
from ninja.types import TCallable
1818
from ninja.utils import normalize_path
1919

2020
if TYPE_CHECKING:
@@ -51,7 +51,7 @@ def get(
5151
exclude_none: bool = False,
5252
url_name: Optional[str] = None,
5353
include_in_schema: bool = True,
54-
) -> Decorator:
54+
) -> Callable[[TCallable], TCallable]:
5555
return self.api_operation(
5656
["GET"],
5757
path,
@@ -87,7 +87,7 @@ def post(
8787
exclude_none: bool = False,
8888
url_name: Optional[str] = None,
8989
include_in_schema: bool = True,
90-
) -> Decorator:
90+
) -> Callable[[TCallable], TCallable]:
9191
return self.api_operation(
9292
["POST"],
9393
path,
@@ -123,7 +123,7 @@ def delete(
123123
exclude_none: bool = False,
124124
url_name: Optional[str] = None,
125125
include_in_schema: bool = True,
126-
) -> Decorator:
126+
) -> Callable[[TCallable], TCallable]:
127127
return self.api_operation(
128128
["DELETE"],
129129
path,
@@ -159,7 +159,7 @@ def patch(
159159
exclude_none: bool = False,
160160
url_name: Optional[str] = None,
161161
include_in_schema: bool = True,
162-
) -> Decorator:
162+
) -> Callable[[TCallable], TCallable]:
163163
return self.api_operation(
164164
["PATCH"],
165165
path,
@@ -195,7 +195,7 @@ def put(
195195
exclude_none: bool = False,
196196
url_name: Optional[str] = None,
197197
include_in_schema: bool = True,
198-
) -> Decorator:
198+
) -> Callable[[TCallable], TCallable]:
199199
return self.api_operation(
200200
["PUT"],
201201
path,
@@ -232,7 +232,7 @@ def api_operation(
232232
exclude_none: bool = False,
233233
url_name: Optional[str] = None,
234234
include_in_schema: bool = True,
235-
) -> Decorator:
235+
) -> Callable[[TCallable], TCallable]:
236236
def decorator(view_func: TCallable) -> TCallable:
237237
self.add_api_operation(
238238
path,

ninja/types.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
from typing import Any, Callable, Dict, TypeVar
22

3-
__all__ = ["DictStrAny", "TCallable", "Decorator"]
3+
__all__ = ["DictStrAny", "TCallable"]
44

55
DictStrAny = Dict[str, Any]
66

7-
TCallable = TypeVar("TCallable", bound=Callable)
8-
Decorator = Callable[[TCallable], TCallable]
7+
TCallable = TypeVar("TCallable", bound=Callable[..., Any])
8+
9+
10+
# unfortunately this doesn't work yet, see
11+
# https://github.com/python/mypy/issues/3924
12+
# Decorator = Callable[[TCallable], TCallable]

0 commit comments

Comments
 (0)