Skip to content

Commit 840c8f4

Browse files
committed
feat: avoid mypy cheat and tests
1 parent 4a08140 commit 840c8f4

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

ninja/params/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ def __getitem__(self, args: Any) -> Any:
4343
Cookie = Annotated[T, param_functions.Cookie()]
4444
File = Annotated[T, param_functions.File()]
4545
Form = Annotated[T, param_functions.Form()]
46-
Header = Annotated[T, param_functions.Header()]
4746
Path = Annotated[T, param_functions.Path()]
4847
Query = Annotated[T, param_functions.Query()]
4948
# mypy does not like to extend already annotated params
@@ -52,15 +51,13 @@ def __getitem__(self, args: Any) -> Any:
5251
from typing_extensions import Annotated as CookieEx
5352
from typing_extensions import Annotated as FileEx
5453
from typing_extensions import Annotated as FormEx
55-
from typing_extensions import Annotated as HeaderEx
5654
from typing_extensions import Annotated as PathEx
5755
from typing_extensions import Annotated as QueryEx
5856
else:
5957
Body = ParamShortcut(param_functions.Body)
6058
Cookie = ParamShortcut(param_functions.Cookie)
6159
File = ParamShortcut(param_functions.File)
6260
Form = ParamShortcut(param_functions.Form)
63-
Header = ParamShortcut(param_functions.Header)
6461
Path = ParamShortcut(param_functions.Path)
6562
Query = ParamShortcut(param_functions.Query)
6663
# mypy does not like to extend already annotated params
@@ -69,11 +66,14 @@ def __getitem__(self, args: Any) -> Any:
6966
CookieEx = Cookie
7067
FileEx = File
7168
FormEx = Form
72-
HeaderEx = Header
7369
PathEx = Path
7470
QueryEx = Query
7571

7672

73+
Header = ParamShortcut(param_functions.Header)
74+
HeaderEx = Header
75+
76+
7777
def P(
7878
*,
7979
alias: Optional[str] = None,

tests/test_request.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
from typing import Optional
23

34
import pytest
@@ -26,6 +27,16 @@ def headers1(request, user_agent: str = Header(...)):
2627
return user_agent
2728

2829

30+
annotated_available = sys.version_info >= (3, 9)
31+
32+
if annotated_available:
33+
from typing import Annotated
34+
35+
@router.get("/headers1_annotated")
36+
def headers1_annotated(request, user_agent: Annotated[str, Header(...)]):
37+
return user_agent
38+
39+
2940
@router.get("/headers2")
3041
def headers2(request, ua: str = Header(..., alias="User-Agent")):
3142
return ua
@@ -68,6 +79,7 @@ def schema(request, payload: ExtraForbidSchema = Body(...)):
6879
"path,expected_status,expected_response",
6980
[
7081
("/headers1", 200, "Ninja"),
82+
*([("/headers1_annotated", 200, "Ninja")] if annotated_available else []),
7183
("/headers2", 200, "Ninja"),
7284
("/headers3", 200, 10),
7385
("/headers4", 200, 10),

0 commit comments

Comments
 (0)