Skip to content

Commit

Permalink
Incorporate scope inference (#101)
Browse files Browse the repository at this point in the history
Taking advantage of adriangb/di@b0cd073 to make things a bit less verbose and error prone.
  • Loading branch information
adriangb authored Aug 8, 2022
1 parent 3e34996 commit 688671d
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 52 deletions.
92 changes: 46 additions & 46 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "xpresso"
version = "0.41.2"
version = "0.42.0"
description = "A developer centric, performant Python web framework"
authors = ["Adrian Garcia Badaracco <[email protected]>"]
readme = "README.md"
Expand Down Expand Up @@ -36,7 +36,7 @@ classifiers = [

[tool.poetry.dependencies]
python = ">=3.7,<4"
di = ">=0.69.0"
di = ">=0.70.0"
anyio = ">=3.4.0,<5"
starlette = ">=0.17.1,<1"
typing-extensions = { version = ">=3", python = "<3.9" }
Expand Down
31 changes: 31 additions & 0 deletions xpresso/_utils/scope_resolver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import Any, Sequence

from di.api.dependencies import DependantBase
from di.api.scopes import Scope


def endpoint_scope_resolver(
dep: DependantBase[Any],
sub_dependant_scopes: Sequence[Scope],
_: Sequence[Scope],
) -> Scope:
"""Resolve scopes by defaulting to "connection"
unless a sub-dependency has an "endpoint" scope
in which case we drop down to that scope
"""
if dep.scope is not None:
return dep.scope
if "endpoint" in sub_dependant_scopes:
return "endpoint"
return "connection"


def lifespan_scope_resolver(
dep: DependantBase[Any],
sub_dependant_scopes: Sequence[Scope],
_: Sequence[Scope],
) -> Scope:
"""Always default to the "app" scope"""
if dep.scope is None:
return "app"
return dep.scope
5 changes: 5 additions & 0 deletions xpresso/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from xpresso._utils.asgi import XpressoHTTPExtension, XpressoWebSocketExtension
from xpresso._utils.overrides import DependencyOverrideManager
from xpresso._utils.routing import visit_routes
from xpresso._utils.scope_resolver import lifespan_scope_resolver
from xpresso.dependencies._dependencies import BoundDependsMarker, Scopes
from xpresso.exception_handlers import (
ExcHandler,
Expand Down Expand Up @@ -102,6 +103,7 @@ async def lifespan_ctx(*_: typing.Any) -> typing.AsyncIterator[None]:
placeholder = Dependant(lambda: None, scope="app")
dep: "DependantBase[typing.Any]"
executor = AsyncExecutor()

async with self._container_state.enter_scope(
"app"
) as self._container_state:
Expand All @@ -122,6 +124,7 @@ async def lifespan_ctx(*_: typing.Any) -> typing.AsyncIterator[None]:
],
),
scopes=Scopes,
scope_resolver=lifespan_scope_resolver,
)
try:
await self.container.execute_async(
Expand All @@ -140,13 +143,15 @@ async def lifespan_ctx(*_: typing.Any) -> typing.AsyncIterator[None]:
lifespan_deps.extend(
d for d in prepared.dag if d.scope == "app"
)

await self.container.execute_async(
self.container.solve(
JoinedDependant(
placeholder,
siblings=lifespan_deps,
),
scopes=Scopes,
scope_resolver=lifespan_scope_resolver,
),
executor,
state=self._container_state,
Expand Down
8 changes: 4 additions & 4 deletions xpresso/dependencies/_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from xpresso._utils.typing import Literal

Scope = Literal["app", "connection", "endpoint"]
Scopes: typing.Tuple[Literal["app"], Literal["connection"], Literal["endpoint"]] = (
Scopes = (
"app",
"connection",
"endpoint",
Expand Down Expand Up @@ -44,7 +44,7 @@ def Depends(
use_cache: bool = True,
wire: bool = True,
sync_to_thread: bool = False,
scope: Scope = "connection",
scope: typing.Optional[Scope] = None,
) -> typing.Any:
return DependsMarker(
call=call,
Expand All @@ -68,7 +68,7 @@ def __init__(
use_cache: bool = True,
wire: bool = True,
sync_to_thread: bool = True,
scope: Scope = "connection",
scope: typing.Optional[Scope] = None,
) -> None:
super().__init__(
call=call,
Expand Down Expand Up @@ -98,7 +98,7 @@ class Injectable(InjectableBase):
def __init_subclass__(
cls,
call: typing.Optional[DependencyProvider] = None,
scope: Scope = "connection",
scope: typing.Optional[Scope] = None,
use_cache: bool = True,
**kwargs: typing.Any,
) -> None:
Expand Down
2 changes: 2 additions & 0 deletions xpresso/routing/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import xpresso.openapi.models as openapi_models
from xpresso._utils.asgi import XpressoHTTPExtension
from xpresso._utils.endpoint_dependant import Endpoint, EndpointDependant
from xpresso._utils.scope_resolver import endpoint_scope_resolver
from xpresso.dependencies._dependencies import BoundDependsMarker, Scopes
from xpresso.encoders import Encoder, JsonableEncoder
from xpresso.responses import ResponseSpec, ResponseStatusCode, TypeUnset
Expand Down Expand Up @@ -164,6 +165,7 @@ def prepare(
siblings=[*dependencies, *self.dependencies],
),
scopes=Scopes,
scope_resolver=endpoint_scope_resolver,
)
executor: SupportsAsyncExecutor
if self._execute_dependencies_concurrently:
Expand Down
2 changes: 2 additions & 0 deletions xpresso/routing/websockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from xpresso._utils.asgi import XpressoWebSocketExtension
from xpresso._utils.endpoint_dependant import Endpoint, EndpointDependant
from xpresso._utils.scope_resolver import endpoint_scope_resolver
from xpresso.dependencies._dependencies import BoundDependsMarker, Scopes


Expand Down Expand Up @@ -94,6 +95,7 @@ def prepare(
siblings=[*dependencies, *self.dependencies],
),
scopes=Scopes,
scope_resolver=endpoint_scope_resolver,
)
executor: SupportsAsyncExecutor
if self.execute_dependencies_concurrently:
Expand Down

0 comments on commit 688671d

Please sign in to comment.