Skip to content

Commit

Permalink
Upgrade all dev dependencies (#7)
Browse files Browse the repository at this point in the history
In preperation for upgrading to Pydantic v2
  • Loading branch information
ljodal committed Jul 5, 2023
1 parent b00620f commit 977e738
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 266 deletions.
1 change: 0 additions & 1 deletion django_api_decorator/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class ApiToolsConfig(AppConfig):
name = "django_api_decorator"

def ready(self) -> None:

if not getattr(settings, "API_DECORATOR_SCHEMA_AUTOGENERATE", False):
return

Expand Down
11 changes: 0 additions & 11 deletions django_api_decorator/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ def default_auth_check(request: HttpRequest) -> bool:
)

def decorator(func: Callable[..., Any]) -> Callable[..., HttpResponse]:

signature = inspect.signature(func)

# Get a function that we can call to extract view parameters from
Expand All @@ -115,7 +114,6 @@ def decorator(func: Callable[..., Any]) -> Callable[..., HttpResponse]:
@transaction.non_atomic_requests()
@require_http_methods([method])
def inner(request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:

# Check if the view requires the user to be logged in and if so make
# sure the user is actually logged in.
if login_required and not _auth_check(request):
Expand Down Expand Up @@ -213,7 +211,6 @@ def _validate_int(value: str, *, query_param_name: str) -> int:


def _validate_bool(value: str, *, query_param_name: str) -> bool:

value = value.lower().strip()

if value in ("", "yes", "on", "true", "1"):
Expand All @@ -226,15 +223,13 @@ def _validate_bool(value: str, *, query_param_name: str) -> bool:


def _validate_date(value: str, *, query_param_name: str) -> datetime.date:

try:
return datetime.date.fromisoformat(value)
except ValueError:
raise ValidationError(f"{query_param_name} must be a valid date")


def _get_validator(parameter: inspect.Parameter) -> Validator:

annotation = parameter.annotation
if annotation is inspect.Parameter.empty:
raise ValueError(
Expand Down Expand Up @@ -276,7 +271,6 @@ def _get_query_param_parser(
parameters: Mapping[str, inspect.Parameter],
query_params: list[str],
) -> Callable[[HttpRequest], Mapping[str, Any]]:

query_param_mapping = (
{query_param.replace("_", "-"): query_param for query_param in query_params}
if query_params
Expand All @@ -293,7 +287,6 @@ def _get_query_param_parser(
}

def parser(request: HttpRequest) -> Mapping[str, Any]:

validated = {}

for query_param, arg_name in query_param_mapping.items():
Expand Down Expand Up @@ -331,7 +324,6 @@ def _can_have_body(method: str | None) -> bool:


def _get_body_parser(*, parameter: inspect.Parameter) -> BodyParser:

annotation = parameter.annotation
if annotation is inspect.Parameter.empty:
raise TypeError("The body parameter must have a type annotation")
Expand Down Expand Up @@ -396,15 +388,13 @@ def __call__(self, *, payload: Any, status: int) -> HttpResponse:


def _is_class(*, type_annotation: Annotation) -> bool:

return inspect.isclass(type_annotation) and (
type(type_annotation)
is not types.GenericAlias # type: ignore[comparison-overlap]
)


def _get_response_encoder(*, type_annotation: Annotation) -> ResponseEncoder:

type_is_class = _is_class(type_annotation=type_annotation)

if type_is_class and issubclass(type_annotation, HttpResponse):
Expand Down Expand Up @@ -480,7 +470,6 @@ def handle_validation_error(
*,
exception: (ValidationError | pydantic.ValidationError),
) -> HttpResponse:

errors: list[str]
field_errors: Mapping[str, FieldError]

Expand Down
10 changes: 3 additions & 7 deletions django_api_decorator/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ def get_schema_for_type_annotation(
def paths_and_types_for_view(
*, view_name: str, callback: Callable[..., HttpResponse], resolved_url: str
) -> tuple[dict[str, Any], list[type]]:

api_meta: ApiMeta | None = getattr(callback, "_api_meta", None)

assert api_meta is not None
Expand Down Expand Up @@ -254,7 +253,7 @@ def paths_and_types_for_view(
"responses": {
api_meta.response_status: {
"description": "",
**api_response, # type: ignore
**api_response,
}
},
}
Expand Down Expand Up @@ -320,7 +319,6 @@ def openapi_query_parameters(


def schemas_for_types(api_types: list[type]) -> dict[str, Any]:

# This only supports Pydantic models for now.
assert all(
hasattr(t, "__pydantic_model__") or issubclass(t, BaseModel) for t in api_types
Expand Down Expand Up @@ -363,17 +361,16 @@ class OpenApiOperation:
# same URL
for method, callback in cast(
dict[str, Callable[..., HttpResponse]],
pattern.callback._method_router_views, # type: ignore
pattern.callback._method_router_views,
).items():

if not hasattr(callback, "_api_meta"):
logger.debug(
"Skipping view %s because it is not using @api decorator",
pattern.name,
)
continue

api_meta: ApiMeta = callback._api_meta # type: ignore
api_meta: ApiMeta = callback._api_meta
assert method == api_meta.method

operations.append(
Expand Down Expand Up @@ -406,7 +403,6 @@ class OpenApiOperation:
api_types = []

for operation in operations:

paths, types = paths_and_types_for_view(
view_name=operation.name,
callback=operation.callback,
Expand Down
2 changes: 1 addition & 1 deletion django_api_decorator/type_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ def get_inner_list_type(type_annotation: type) -> tuple[type, bool]:
def is_pydantic_model(t: type) -> bool:
return issubclass(t, pydantic.BaseModel) or (
hasattr(t, "__pydantic_model__")
and issubclass(t.__pydantic_model__, pydantic.BaseModel) # type: ignore
and issubclass(t.__pydantic_model__, pydantic.BaseModel)
)
Loading

0 comments on commit 977e738

Please sign in to comment.