Skip to content

Commit

Permalink
P helper
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalik committed Sep 29, 2023
1 parent ea3e14f commit c6f1602
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
26 changes: 25 additions & 1 deletion ninja/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,23 @@
from ninja.main import NinjaAPI
from ninja.openapi.docs import Redoc, Swagger
from ninja.orm import ModelSchema
from ninja.params import Body, Cookie, File, Form, Header, Path, Query
from ninja.params import (
Body,
BodyEx,
Cookie,
CookieEx,
File,
FileEx,
Form,
FormEx,
Header,
HeaderEx,
P,
Path,
PathEx,
Query,
QueryEx,
)
from ninja.router import Router
from ninja.schema import Schema

Expand All @@ -25,7 +41,15 @@
"Header",
"Path",
"Query",
"BodyEx",
"CookieEx",
"FileEx",
"FormEx",
"HeaderEx",
"PathEx",
"QueryEx",
"Router",
"P",
"Schema",
"ModelSchema",
"FilterSchema",
Expand Down
42 changes: 40 additions & 2 deletions ninja/params/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import TYPE_CHECKING, Any, Callable, TypeVar
from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, TypeVar

from typing_extensions import Annotated
from typing_extensions import Annotated, TypedDict

from ninja.params import functions as param_functions

Expand Down Expand Up @@ -54,3 +54,41 @@ def __getitem__(self, args: Any) -> Any:
HeaderEx = Header
PathEx = Path
QueryEx = Query


def P(
*,
alias: Optional[str] = None,
title: Optional[str] = None,
description: Optional[str] = None,
gt: Optional[float] = None,
ge: Optional[float] = None,
lt: Optional[float] = None,
le: Optional[float] = None,
min_length: Optional[int] = None,
max_length: Optional[int] = None,
regex: Optional[str] = None,
example: Any = None,
examples: Optional[Dict[str, Any]] = None,
deprecated: Optional[bool] = None,
include_in_schema: bool = True,
**extra: Any,
) -> Dict[str, Any]:
"Arguments for BodyEx, QueryEx, etc."
return dict(
alias=alias,
title=title,
description=description,
gt=gt,
ge=ge,
lt=lt,
le=le,
min_length=min_length,
max_length=max_length,
regex=regex,
example=example,
examples=examples,
deprecated=deprecated,
include_in_schema=include_in_schema,
**extra,
)
9 changes: 6 additions & 3 deletions tests/mypy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.http import HttpRequest
from typing_extensions import Annotated

from ninja import Body, NinjaAPI, Schema
from ninja import Body, NinjaAPI, Schema, BodyEx, P


class Payload(Schema):
Expand All @@ -24,10 +24,13 @@ def old_way(request: HttpRequest, data: Payload = Body()) -> Any:
@api.post("/annotated_way")
def annotated_way(request: HttpRequest, data: Annotated[Payload, Body()]) -> Any:
data.s.capitalize()
pass


@api.post("/new_way")
def new_way(request: HttpRequest, data: Body[Payload]) -> Any:
data.s.capitalize()
pass


@api.post("/new_way_ex")
def new_way(request: HttpRequest, data: BodyEx[Payload, P(title="A title")]) -> Any:
data.s.find("", "")

0 comments on commit c6f1602

Please sign in to comment.