Skip to content

Commit

Permalink
Remove deprecated generator function warnings on lifespan
Browse files Browse the repository at this point in the history
  • Loading branch information
Kludex committed Feb 18, 2024
1 parent 03a8eb5 commit d25651c
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 106 deletions.
53 changes: 0 additions & 53 deletions starlette/routing.py
@@ -1,14 +1,10 @@
from __future__ import annotations

import contextlib
import functools
import inspect
import re
import traceback
import types
import typing
import warnings
from contextlib import asynccontextmanager
from enum import Enum

from starlette._exception_handler import wrap_app_handling_exceptions
Expand Down Expand Up @@ -557,36 +553,6 @@ def __repr__(self) -> str:
_T = typing.TypeVar("_T")


class _AsyncLiftContextManager(typing.AsyncContextManager[_T]):
def __init__(self, cm: typing.ContextManager[_T]):
self._cm = cm

async def __aenter__(self) -> _T:
return self._cm.__enter__()

async def __aexit__(
self,
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
traceback: types.TracebackType | None,
) -> bool | None:
return self._cm.__exit__(exc_type, exc_value, traceback)


def _wrap_gen_lifespan_context(
lifespan_context: typing.Callable[
[typing.Any], typing.Generator[typing.Any, typing.Any, typing.Any]
],
) -> typing.Callable[[typing.Any], typing.AsyncContextManager[typing.Any]]:
cmgr = contextlib.contextmanager(lifespan_context)

@functools.wraps(cmgr)
def wrapper(app: typing.Any) -> _AsyncLiftContextManager[typing.Any]:
return _AsyncLiftContextManager(cmgr(app))

return wrapper


class _DefaultLifespan:
def __init__(self, router: Router):
self._router = router
Expand Down Expand Up @@ -619,25 +585,6 @@ def __init__(

if lifespan is None:
self.lifespan_context: Lifespan[typing.Any] = _DefaultLifespan(self)

elif inspect.isasyncgenfunction(lifespan):
warnings.warn(
"async generator function lifespans are deprecated, "
"use an @contextlib.asynccontextmanager function instead",
DeprecationWarning,
)
self.lifespan_context = asynccontextmanager(
lifespan,
)
elif inspect.isgeneratorfunction(lifespan):
warnings.warn(
"generator function lifespans are deprecated, "
"use an @contextlib.asynccontextmanager function instead",
DeprecationWarning,
)
self.lifespan_context = _wrap_gen_lifespan_context(
lifespan,
)
else:
self.lifespan_context = lifespan

Expand Down
53 changes: 0 additions & 53 deletions tests/test_applications.py
Expand Up @@ -359,59 +359,6 @@ async def lifespan(app: ASGIApp) -> AsyncGenerator[None, None]:
assert cleanup_complete


deprecated_lifespan = pytest.mark.filterwarnings(
r"ignore"
r":(async )?generator function lifespans are deprecated, use an "
r"@contextlib\.asynccontextmanager function instead"
r":DeprecationWarning"
r":starlette.routing"
)


@deprecated_lifespan
def test_app_async_gen_lifespan(test_client_factory: TestClientFactory) -> None:
startup_complete = False
cleanup_complete = False

async def lifespan(app: ASGIApp) -> AsyncGenerator[None, None]:
nonlocal startup_complete, cleanup_complete
startup_complete = True
yield
cleanup_complete = True

app = Starlette(lifespan=lifespan) # type: ignore

assert not startup_complete
assert not cleanup_complete
with test_client_factory(app):
assert startup_complete
assert not cleanup_complete
assert startup_complete
assert cleanup_complete


@deprecated_lifespan
def test_app_sync_gen_lifespan(test_client_factory: TestClientFactory) -> None:
startup_complete = False
cleanup_complete = False

def lifespan(app: ASGIApp) -> Generator[None, None, None]:
nonlocal startup_complete, cleanup_complete
startup_complete = True
yield
cleanup_complete = True

app = Starlette(lifespan=lifespan) # type: ignore

assert not startup_complete
assert not cleanup_complete
with test_client_factory(app):
assert startup_complete
assert not cleanup_complete
assert startup_complete
assert cleanup_complete


def test_middleware_stack_init(test_client_factory: TestClientFactory) -> None:
class NoOpMiddleware:
def __init__(self, app: ASGIApp):
Expand Down

0 comments on commit d25651c

Please sign in to comment.