Skip to content

Commit

Permalink
Support JSONResponse[...] type check
Browse files Browse the repository at this point in the history
  • Loading branch information
abersheeran committed Mar 28, 2024
1 parent 9cfac01 commit fcc676c
Showing 1 changed file with 57 additions and 14 deletions.
71 changes: 57 additions & 14 deletions kui/responses.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import abc
import typing
from http import HTTPStatus

Expand All @@ -14,8 +15,8 @@ def _json_encoder(obj: typing.Any) -> typing.Any:
return to_jsonable_python(obj)


class JSONResponseMixin:
def __class_getitem__(
class JSONResponseDocsMetaclass(abc.ABCMeta):
def __getitem__(
cls,
parameters: typing.Tuple[
int,
Expand Down Expand Up @@ -71,8 +72,14 @@ def __class_getitem__(
return docs


class FileResponseMixin:
def __class_getitem__(
class JSONResponseMixin(metaclass=JSONResponseDocsMetaclass):
"""
JSON response with OpenAPI docs support
"""


class FileResponseDocsMetaclass(abc.ABCMeta):
def __getitem__(
cls,
parameters: typing.Tuple[str, typing.Dict[str, spec.Header | spec.Reference]]
| str,
Expand Down Expand Up @@ -104,8 +111,14 @@ def __class_getitem__(
}


class PlainTextResponseMixin:
def __class_getitem__(
class FileResponseMixin(metaclass=FileResponseDocsMetaclass):
"""
File response with OpenAPI docs support
"""


class PlainTextResponseDocsMetaclass(abc.ABCMeta):
def __getitem__(
cls,
parameters: typing.Tuple[int, typing.Dict[str, spec.Header | spec.Reference]]
| int,
Expand All @@ -128,8 +141,14 @@ def __class_getitem__(
}


class HTMLResponseMixin:
def __class_getitem__(
class PlainTextResponseMixin(metaclass=PlainTextResponseDocsMetaclass):
"""
Plain text response with OpenAPI docs support
"""


class HTMLResponseDocsMetaclass(abc.ABCMeta):
def __getitem__(
cls,
parameters: typing.Tuple[int, typing.Dict[str, spec.Header | spec.Reference]]
| int,
Expand All @@ -152,8 +171,14 @@ def __class_getitem__(
}


class RedirectResponseMixin:
def __class_getitem__(
class HTMLResponseMixin(metaclass=HTMLResponseDocsMetaclass):
"""
HTML response with OpenAPI docs support
"""


class RedirectResponseDocsMetaclass(abc.ABCMeta):
def __getitem__(
cls,
parameters: typing.Tuple[int, typing.Dict[str, spec.Header | spec.Reference]]
| int,
Expand All @@ -178,8 +203,14 @@ def __class_getitem__(
}


class SendEventResponseMixin:
def __class_getitem__(
class RedirectResponseMixin(metaclass=RedirectResponseDocsMetaclass):
"""
Redirect response with OpenAPI docs support
"""


class SendEventResponseDocsMetaclass(abc.ABCMeta):
def __getitem__(
cls,
parameters: typing.Tuple[int, typing.Dict[str, spec.Header | spec.Reference]]
| int,
Expand All @@ -204,8 +235,14 @@ def __class_getitem__(
}


class StreamResponseMixin:
def __class_getitem__(
class SendEventResponseMixin(metaclass=SendEventResponseDocsMetaclass):
"""
Send event response with OpenAPI docs support
"""


class StreamResponseDocsMetaclass(abc.ABCMeta):
def __getitem__(
cls,
parameters: typing.Tuple[int, typing.Dict[str, spec.Header | spec.Reference]]
| int,
Expand Down Expand Up @@ -233,3 +270,9 @@ def __class_getitem__(
},
}
}


class StreamResponseMixin(metaclass=StreamResponseDocsMetaclass):
"""
Stream response with OpenAPI docs support
"""

0 comments on commit fcc676c

Please sign in to comment.