Skip to content

Commit

Permalink
feat(typing): type response (#2304)
Browse files Browse the repository at this point in the history
* typing: type app

* typing: type websocket module

* typing: type asgi.reader, asgi.structures, asgi.stream

* typing: type most of media

* typing: type multipart

* typing: type response

* style: fix spelling in multipart.py

* style(tests): explain referencing the same property multiple times

* style: fix linter errors

* chore: revert behavioral change to cors middleware.

* chore: do not build rapidjson on PyPy

---------

Co-authored-by: Vytautas Liuolia <[email protected]>
  • Loading branch information
CaselIT and vytas7 authored Sep 20, 2024
1 parent 9f47efb commit ddff2ce
Show file tree
Hide file tree
Showing 15 changed files with 558 additions and 393 deletions.
14 changes: 9 additions & 5 deletions falcon/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
ClassVar,
Dict,
FrozenSet,
IO,
Iterable,
List,
Literal,
Expand Down Expand Up @@ -62,6 +61,7 @@
from falcon.typing import ErrorSerializer
from falcon.typing import FindMethod
from falcon.typing import ProcessResponseMethod
from falcon.typing import ReadableIO
from falcon.typing import ResponderCallable
from falcon.typing import SinkCallable
from falcon.typing import SinkPrefix
Expand Down Expand Up @@ -1191,7 +1191,9 @@ def _handle_exception(
def _get_body(
self,
resp: Response,
wsgi_file_wrapper: Optional[Callable[[IO[bytes], int], Iterable[bytes]]] = None,
wsgi_file_wrapper: Optional[
Callable[[ReadableIO, int], Iterable[bytes]]
] = None,
) -> Tuple[Iterable[bytes], Optional[int]]:
"""Convert resp content into an iterable as required by PEP 333.
Expand Down Expand Up @@ -1229,11 +1231,13 @@ def _get_body(
# TODO(kgriffs): Make block size configurable at the
# global level, pending experimentation to see how
# useful that would be. See also the discussion on
# this GitHub PR: http://goo.gl/XGrtDz
iterable = wsgi_file_wrapper(stream, self._STREAM_BLOCK_SIZE)
# this GitHub PR:
# https://github.com/falconry/falcon/pull/249#discussion_r11269730
iterable = wsgi_file_wrapper(stream, self._STREAM_BLOCK_SIZE) # type: ignore[arg-type]
else:
iterable = helpers.CloseableStreamIterator(
stream, self._STREAM_BLOCK_SIZE
stream, # type: ignore[arg-type]
self._STREAM_BLOCK_SIZE,
)
else:
iterable = stream
Expand Down
12 changes: 6 additions & 6 deletions falcon/asgi/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
from falcon.asgi_spec import AsgiSendMsg
from falcon.asgi_spec import EventType
from falcon.asgi_spec import WSCloseCode
from falcon.constants import _UNSET
from falcon.constants import MEDIA_JSON
from falcon.errors import CompatibilityError
from falcon.errors import HTTPBadRequest
Expand All @@ -60,6 +59,7 @@
from falcon.typing import AsgiResponderWsCallable
from falcon.typing import AsgiSend
from falcon.typing import AsgiSinkCallable
from falcon.typing import MISSING
from falcon.typing import SinkPrefix
from falcon.util import get_argnames
from falcon.util.misc import is_python_func
Expand Down Expand Up @@ -552,9 +552,9 @@ async def __call__( # type: ignore[override] # noqa: C901
data = resp._data

if data is None and resp._media is not None:
# NOTE(kgriffs): We use a special _UNSET singleton since
# NOTE(kgriffs): We use a special MISSING singleton since
# None is ambiguous (the media handler might return None).
if resp._media_rendered is _UNSET:
if resp._media_rendered is MISSING:
opt = resp.options
if not resp.content_type:
resp.content_type = opt.default_media_type
Expand All @@ -577,7 +577,7 @@ async def __call__( # type: ignore[override] # noqa: C901
data = text.encode()
except AttributeError:
# NOTE(kgriffs): Assume it was a bytes object already
data = text
data = text # type: ignore[assignment]

else:
# NOTE(vytas): Custom response type.
Expand Down Expand Up @@ -1028,9 +1028,9 @@ def _schedule_callbacks(self, resp: Response) -> None:

loop = asyncio.get_running_loop()

for cb, is_async in callbacks: # type: ignore[attr-defined]
for cb, is_async in callbacks or ():
if is_async:
loop.create_task(cb())
loop.create_task(cb()) # type: ignore[arg-type]
else:
loop.run_in_executor(None, cb)

Expand Down
Loading

0 comments on commit ddff2ce

Please sign in to comment.