Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix typing of ASGIApp (#2057) #2058

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions starlette/types.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import typing

Scope = typing.MutableMapping[str, typing.Any]
Message = typing.MutableMapping[str, typing.Any]
Scope = typing.Dict[str, typing.Any]
Message = typing.Dict[str, typing.Any]

Receive = typing.Callable[[], typing.Awaitable[Message]]
Send = typing.Callable[[Message], typing.Awaitable[None]]
Expand Down
20 changes: 19 additions & 1 deletion tests/test_applications.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
from contextlib import asynccontextmanager
from typing import Any, Callable
from typing import Any, Awaitable, Callable, Coroutine, Dict

import anyio
import httpx
Expand Down Expand Up @@ -531,3 +531,21 @@ def get_app() -> ASGIApp:
test_client_factory(app).get("/foo")

assert SimpleInitializableMiddleware.counter == 2


def test_app_typing():
# Not a real test, but catches the typing bug when checked with mypy
# https://github.com/encode/starlette/issues/2057

# The types from `httpx`
_Message = Dict[str, Any]
_Receive = Callable[[], Awaitable[_Message]]
_Send = Callable[[Dict[str, Any]], Coroutine[None, None, None]]
_ASGIApp = Callable[[Dict[str, Any], _Receive, _Send], Coroutine[None, None, None]]

class ASGITransport:
def __init__(self, app: _ASGIApp) -> None:
...

# Check types compatibility
ASGITransport(Starlette())