Skip to content

Commit af39218

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

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
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: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional
1+
from typing import Annotated, Optional
22

33
import pytest
44
from pydantic import ConfigDict
@@ -26,26 +26,53 @@ def headers1(request, user_agent: str = Header(...)):
2626
return user_agent
2727

2828

29+
@router.get("/headers1_annotated")
30+
def headers1_annotated(request, user_agent: Annotated[str, Header(...)]):
31+
return user_agent
32+
33+
2934
@router.get("/headers2")
3035
def headers2(request, ua: str = Header(..., alias="User-Agent")):
3136
return ua
3237

3338

39+
@router.get("/headers2_annotated")
40+
def headers2_annotated(request, ua: Annotated[str, Header(..., alias="User-Agent")]):
41+
return ua
42+
43+
3444
@router.get("/headers3")
3545
def headers3(request, content_length: int = Header(...)):
3646
return content_length
3747

3848

49+
@router.get("/headers3_annotated")
50+
def headers3_annotated(request, content_length: Annotated[int, Header(...)]):
51+
return content_length
52+
53+
3954
@router.get("/headers4")
4055
def headers4(request, c_len: int = Header(..., alias="Content-length")):
4156
return c_len
4257

4358

59+
@router.get("/headers4_annotated")
60+
def headers4_annotated(
61+
request, c_len: Annotated[int, Header(..., alias="Content-length")]
62+
):
63+
return c_len
64+
65+
4466
@router.get("/headers5")
4567
def headers5(request, missing: int = Header(...)):
4668
return missing
4769

4870

71+
@router.get("/headers5_annotated")
72+
def headers5_annotated(request, missing: Annotated[int, Header(...)]):
73+
return missing
74+
75+
4976
@router.get("/cookies1")
5077
def cookies1(request, weapon: str = Cookie(...)):
5178
return weapon
@@ -68,9 +95,13 @@ def schema(request, payload: ExtraForbidSchema = Body(...)):
6895
"path,expected_status,expected_response",
6996
[
7097
("/headers1", 200, "Ninja"),
98+
("/headers1_annotated", 200, "Ninja"),
7199
("/headers2", 200, "Ninja"),
100+
("/headers2_annotated", 200, "Ninja"),
72101
("/headers3", 200, 10),
102+
("/headers3_annotated", 200, 10),
73103
("/headers4", 200, 10),
104+
("/headers4_annotated", 200, 10),
74105
(
75106
"/headers5",
76107
422,
@@ -84,6 +115,19 @@ def schema(request, payload: ExtraForbidSchema = Body(...)):
84115
]
85116
},
86117
),
118+
(
119+
"/headers5_annotated",
120+
422,
121+
{
122+
"detail": [
123+
{
124+
"type": "missing",
125+
"loc": ["header", "missing"],
126+
"msg": "Field required",
127+
}
128+
]
129+
},
130+
),
87131
("/cookies1", 200, "shuriken"),
88132
("/cookies2", 200, "shuriken"),
89133
],

0 commit comments

Comments
 (0)