Skip to content

Commit 3d09c8b

Browse files
committed
Remove deprecated generator function warnings on lifespan
1 parent fa739e1 commit 3d09c8b

File tree

3 files changed

+0
-110
lines changed

3 files changed

+0
-110
lines changed

starlette/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
<<<<<<< HEAD
2-
__version__ = "0.38.1"
3-
=======
41
__version__ = "1.0.0"
5-
>>>>>>> b9a1385 (Version 1.0.0)

starlette/routing.py

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
from __future__ import annotations
22

3-
import contextlib
43
import functools
54
import inspect
65
import re
76
import traceback
8-
import types
97
import typing
10-
import warnings
11-
from contextlib import asynccontextmanager
128
from enum import Enum
139

1410
from starlette._exception_handler import wrap_app_handling_exceptions
@@ -555,36 +551,6 @@ def __repr__(self) -> str:
555551
_T = typing.TypeVar("_T")
556552

557553

558-
class _AsyncLiftContextManager(typing.AsyncContextManager[_T]):
559-
def __init__(self, cm: typing.ContextManager[_T]):
560-
self._cm = cm
561-
562-
async def __aenter__(self) -> _T:
563-
return self._cm.__enter__()
564-
565-
async def __aexit__(
566-
self,
567-
exc_type: type[BaseException] | None,
568-
exc_value: BaseException | None,
569-
traceback: types.TracebackType | None,
570-
) -> bool | None:
571-
return self._cm.__exit__(exc_type, exc_value, traceback)
572-
573-
574-
def _wrap_gen_lifespan_context(
575-
lifespan_context: typing.Callable[
576-
[typing.Any], typing.Generator[typing.Any, typing.Any, typing.Any]
577-
],
578-
) -> typing.Callable[[typing.Any], typing.AsyncContextManager[typing.Any]]:
579-
cmgr = contextlib.contextmanager(lifespan_context)
580-
581-
@functools.wraps(cmgr)
582-
def wrapper(app: typing.Any) -> _AsyncLiftContextManager[typing.Any]:
583-
return _AsyncLiftContextManager(cmgr(app))
584-
585-
return wrapper
586-
587-
588554
class _DefaultLifespan:
589555
def __init__(self, router: Router):
590556
self._router = router
@@ -617,25 +583,6 @@ def __init__(
617583

618584
if lifespan is None:
619585
self.lifespan_context: Lifespan[typing.Any] = _DefaultLifespan(self)
620-
621-
elif inspect.isasyncgenfunction(lifespan):
622-
warnings.warn(
623-
"async generator function lifespans are deprecated, "
624-
"use an @contextlib.asynccontextmanager function instead",
625-
DeprecationWarning,
626-
)
627-
self.lifespan_context = asynccontextmanager(
628-
lifespan,
629-
)
630-
elif inspect.isgeneratorfunction(lifespan):
631-
warnings.warn(
632-
"generator function lifespans are deprecated, "
633-
"use an @contextlib.asynccontextmanager function instead",
634-
DeprecationWarning,
635-
)
636-
self.lifespan_context = _wrap_gen_lifespan_context(
637-
lifespan,
638-
)
639586
else:
640587
self.lifespan_context = lifespan
641588

tests/test_applications.py

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -359,59 +359,6 @@ async def lifespan(app: ASGIApp) -> AsyncGenerator[None, None]:
359359
assert cleanup_complete
360360

361361

362-
deprecated_lifespan = pytest.mark.filterwarnings(
363-
r"ignore"
364-
r":(async )?generator function lifespans are deprecated, use an "
365-
r"@contextlib\.asynccontextmanager function instead"
366-
r":DeprecationWarning"
367-
r":starlette.routing"
368-
)
369-
370-
371-
@deprecated_lifespan
372-
def test_app_async_gen_lifespan(test_client_factory: TestClientFactory) -> None:
373-
startup_complete = False
374-
cleanup_complete = False
375-
376-
async def lifespan(app: ASGIApp) -> AsyncGenerator[None, None]:
377-
nonlocal startup_complete, cleanup_complete
378-
startup_complete = True
379-
yield
380-
cleanup_complete = True
381-
382-
app = Starlette(lifespan=lifespan) # type: ignore
383-
384-
assert not startup_complete
385-
assert not cleanup_complete
386-
with test_client_factory(app):
387-
assert startup_complete
388-
assert not cleanup_complete
389-
assert startup_complete
390-
assert cleanup_complete
391-
392-
393-
@deprecated_lifespan
394-
def test_app_sync_gen_lifespan(test_client_factory: TestClientFactory) -> None:
395-
startup_complete = False
396-
cleanup_complete = False
397-
398-
def lifespan(app: ASGIApp) -> Generator[None, None, None]:
399-
nonlocal startup_complete, cleanup_complete
400-
startup_complete = True
401-
yield
402-
cleanup_complete = True
403-
404-
app = Starlette(lifespan=lifespan) # type: ignore
405-
406-
assert not startup_complete
407-
assert not cleanup_complete
408-
with test_client_factory(app):
409-
assert startup_complete
410-
assert not cleanup_complete
411-
assert startup_complete
412-
assert cleanup_complete
413-
414-
415362
def test_middleware_stack_init(test_client_factory: TestClientFactory) -> None:
416363
class NoOpMiddleware:
417364
def __init__(self, app: ASGIApp):

0 commit comments

Comments
 (0)