From e9d4fc4f318df7c4b5b2f5129e63d1af924f5949 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Fri, 18 Aug 2023 08:52:24 +0200 Subject: [PATCH] Revert "Upgraded to AnyIO 4.0 (#2211)" This reverts commit 1a71441ed0240f0aea17b3807a1c26bef756e0c1. --- requirements.txt | 3 +-- starlette/middleware/base.py | 28 ++++------------------------ starlette/middleware/wsgi.py | 4 ---- starlette/testclient.py | 19 ++++++------------- tests/middleware/test_wsgi.py | 8 +------- tests/test_websockets.py | 4 ---- 6 files changed, 12 insertions(+), 54 deletions(-) diff --git a/requirements.txt b/requirements.txt index 65e240832..6d044c13d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,8 +12,7 @@ types-contextvars==2.4.7.2 types-PyYAML==6.0.12.10 types-dataclasses==0.6.6 pytest==7.4.0 -trio==0.22.1 -anyio@git+https://github.com/agronholm/anyio.git +trio==0.21.0 # Documentation mkdocs==1.4.3 diff --git a/starlette/middleware/base.py b/starlette/middleware/base.py index ee99ee6cb..170a805a7 100644 --- a/starlette/middleware/base.py +++ b/starlette/middleware/base.py @@ -1,18 +1,12 @@ -import sys import typing -from contextlib import contextmanager import anyio -from anyio.abc import ObjectReceiveStream, ObjectSendStream from starlette.background import BackgroundTask from starlette.requests import ClientDisconnect, Request from starlette.responses import ContentStream, Response, StreamingResponse from starlette.types import ASGIApp, Message, Receive, Scope, Send -if sys.version_info < (3, 11): # pragma: no cover - from exceptiongroup import BaseExceptionGroup - RequestResponseEndpoint = typing.Callable[[Request], typing.Awaitable[Response]] DispatchFunction = typing.Callable[ [Request, RequestResponseEndpoint], typing.Awaitable[Response] @@ -20,17 +14,6 @@ T = typing.TypeVar("T") -@contextmanager -def _convert_excgroups() -> typing.Generator[None, None, None]: - try: - yield - except BaseException as exc: - while isinstance(exc, BaseExceptionGroup) and len(exc.exceptions) == 1: - exc = exc.exceptions[0] - - raise exc - - class _CachedRequest(Request): """ If the user calls Request.body() from their dispatch function @@ -124,8 +107,6 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: async def call_next(request: Request) -> Response: app_exc: typing.Optional[Exception] = None - send_stream: ObjectSendStream[typing.MutableMapping[str, typing.Any]] - recv_stream: ObjectReceiveStream[typing.MutableMapping[str, typing.Any]] send_stream, recv_stream = anyio.create_memory_object_stream() async def receive_or_disconnect() -> Message: @@ -201,11 +182,10 @@ async def body_stream() -> typing.AsyncGenerator[bytes, None]: response.raw_headers = message["headers"] return response - with _convert_excgroups(): - async with anyio.create_task_group() as task_group: - response = await self.dispatch_func(request, call_next) - await response(scope, wrapped_receive, send) - response_sent.set() + async with anyio.create_task_group() as task_group: + response = await self.dispatch_func(request, call_next) + await response(scope, wrapped_receive, send) + response_sent.set() async def dispatch( self, request: Request, call_next: RequestResponseEndpoint diff --git a/starlette/middleware/wsgi.py b/starlette/middleware/wsgi.py index 95578c9d2..5fab01d79 100644 --- a/starlette/middleware/wsgi.py +++ b/starlette/middleware/wsgi.py @@ -5,7 +5,6 @@ import warnings import anyio -from anyio.abc import ObjectReceiveStream, ObjectSendStream from starlette.types import Receive, Scope, Send @@ -73,9 +72,6 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: class WSGIResponder: - stream_send: ObjectSendStream[typing.MutableMapping[str, typing.Any]] - stream_receive: ObjectReceiveStream[typing.MutableMapping[str, typing.Any]] - def __init__(self, app: typing.Callable[..., typing.Any], scope: Scope) -> None: self.app = app self.scope = scope diff --git a/starlette/testclient.py b/starlette/testclient.py index bfac4bb9f..73efa941c 100644 --- a/starlette/testclient.py +++ b/starlette/testclient.py @@ -12,7 +12,6 @@ import anyio import anyio.from_thread -from anyio.abc import ObjectReceiveStream, ObjectSendStream from anyio.streams.stapled import StapledObjectStream from starlette._utils import is_async_callable @@ -738,18 +737,12 @@ def __enter__(self) -> "TestClient": def reset_portal() -> None: self.portal = None - send1: ObjectSendStream[ - typing.Optional[typing.MutableMapping[str, typing.Any]] - ] - receive1: ObjectReceiveStream[ - typing.Optional[typing.MutableMapping[str, typing.Any]] - ] - send2: ObjectSendStream[typing.MutableMapping[str, typing.Any]] - receive2: ObjectReceiveStream[typing.MutableMapping[str, typing.Any]] - send1, receive1 = anyio.create_memory_object_stream(math.inf) - send2, receive2 = anyio.create_memory_object_stream(math.inf) - self.stream_send = StapledObjectStream(send1, receive1) - self.stream_receive = StapledObjectStream(send2, receive2) + self.stream_send = StapledObjectStream( + *anyio.create_memory_object_stream(math.inf) + ) + self.stream_receive = StapledObjectStream( + *anyio.create_memory_object_stream(math.inf) + ) self.task = portal.start_task_soon(self.lifespan) portal.call(self.wait_startup) diff --git a/tests/middleware/test_wsgi.py b/tests/middleware/test_wsgi.py index ad3975403..bcb4cd6ff 100644 --- a/tests/middleware/test_wsgi.py +++ b/tests/middleware/test_wsgi.py @@ -4,9 +4,6 @@ from starlette.middleware.wsgi import WSGIMiddleware, build_environ -if sys.version_info < (3, 11): # pragma: no cover - from exceptiongroup import ExceptionGroup - def hello_world(environ, start_response): status = "200 OK" @@ -69,12 +66,9 @@ def test_wsgi_exception(test_client_factory): # The HTTP protocol implementations would catch this error and return 500. app = WSGIMiddleware(raise_exception) client = test_client_factory(app) - with pytest.raises(ExceptionGroup) as exc: + with pytest.raises(RuntimeError): client.get("/") - assert len(exc.value.exceptions) == 1 - assert isinstance(exc.value.exceptions[0], RuntimeError) - def test_wsgi_exc_info(test_client_factory): # Note that we're testing the WSGI app directly here. diff --git a/tests/test_websockets.py b/tests/test_websockets.py index 71bccd455..c1ec1153e 100644 --- a/tests/test_websockets.py +++ b/tests/test_websockets.py @@ -1,9 +1,7 @@ import sys -from typing import Any, MutableMapping import anyio import pytest -from anyio.abc import ObjectReceiveStream, ObjectSendStream from starlette import status from starlette.types import Receive, Scope, Send @@ -180,8 +178,6 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None: def test_websocket_concurrency_pattern(test_client_factory): - stream_send: ObjectSendStream[MutableMapping[str, Any]] - stream_receive: ObjectReceiveStream[MutableMapping[str, Any]] stream_send, stream_receive = anyio.create_memory_object_stream() async def reader(websocket):