Skip to content

Commit

Permalink
chore: incorporate upstream changes from di (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
adriangb authored Feb 19, 2022
1 parent 6452dc2 commit 52489fe
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 134 deletions.
191 changes: 81 additions & 110 deletions poetry.lock

Large diffs are not rendered by default.

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.18.1"
version = "0.19.0"
description = "A developer centric, performant Python web framework"
authors = ["Adrian Garcia Badaracco <adrian@adriangb.com>"]
readme = "README.md"
Expand Down Expand Up @@ -36,7 +36,7 @@ classifiers = [

[tool.poetry.dependencies]
python = ">=3.7,<4"
di = "~0.56"
di = ">=0.58.1,<0.59.0"
anyio = "~3"
starlette = ">=0.16.0,<1"
importlib-metadata = {version = ">=3", python = "<3.8"}
Expand Down
4 changes: 2 additions & 2 deletions tests/test_extractors/body/test_field_extractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
else:
from typing import Annotated

from di import Dependant
from di import Marker
from pydantic import BaseModel
from starlette.responses import Response
from starlette.testclient import TestClient
Expand All @@ -18,7 +18,7 @@


def test_non_field_markers_are_ignored() -> None:
class RandomMarker(Dependant[typing.Any]):
class RandomMarker(Marker):
pass

class FormDataModel(BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion xpresso/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def __init__(
routes: typing.Optional[typing.Sequence[BaseRoute]] = None,
*,
container: typing.Optional[BaseContainer] = None,
dependencies: typing.Optional[typing.List[Depends]] = None,
dependencies: typing.Optional[typing.List[DependantBase[typing.Any]]] = None,
debug: bool = False,
middleware: typing.Optional[typing.Sequence[Middleware]] = None,
exception_handlers: typing.Optional[ExceptionHandlers] = None,
Expand Down
6 changes: 3 additions & 3 deletions xpresso/binders/dependants.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import inspect
import typing

from di import Dependant
from di import Dependant, Marker
from di.api.dependencies import CacheKey, DependantBase

from xpresso.binders.api import (
Expand All @@ -16,7 +16,7 @@
)


class ParameterBinderMarker(Dependant[typing.Any]):
class ParameterBinderMarker(Marker):
def __init__(
self,
*,
Expand Down Expand Up @@ -75,7 +75,7 @@ def __init__(
self.extractor = extractor


class BodyBinderMarker(Dependant[typing.Any]):
class BodyBinderMarker(Marker):
def __init__(
self,
*,
Expand Down
23 changes: 18 additions & 5 deletions xpresso/dependencies/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from typing import Literal

import di
from di.api.dependencies import DependantBase
from di.api.providers import DependencyProvider

T = typing.TypeVar("T")
Expand All @@ -18,8 +19,7 @@
Scope = Literal["app", "connection", "endpoint"]


class Depends(di.Dependant[typing.Any]):
__slots__ = ()
class Depends(di.Marker, di.Dependant[typing.Any]):
scope: Scope

def __init__(
Expand All @@ -38,19 +38,32 @@ def __init__(
sync_to_thread=sync_to_thread,
)

def initialize_sub_dependant(self, param: inspect.Parameter) -> Depends:
def from_callable(
self, call: typing.Optional[DependencyProvider]
) -> DependantBase[typing.Any]:
return Depends(
call=call,
scope=self.scope,
use_cache=self.use_cache,
wire=self.wire,
sync_to_thread=self.sync_to_thread,
)

def initialize_sub_dependant(
self, param: inspect.Parameter
) -> DependantBase[typing.Any]:
if param.default is param.empty:
# try to auto-wire
return Depends(
call=None,
scope=self.scope,
use_cache=self.use_cache,
)
).register_parameter(param)
# has a default parameter but we create a dependency anyway just for binds
# but do not wire it to make autowiring less brittle and less magic
return Depends(
call=None,
scope=self.scope,
use_cache=self.use_cache,
wire=False,
)
).register_parameter(param)
7 changes: 4 additions & 3 deletions xpresso/routing/operation.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import typing

from di import AsyncExecutor, BaseContainer, ConcurrentAsyncExecutor
from di import AsyncExecutor, BaseContainer, ConcurrentAsyncExecutor, JoinedDependant
from di.api.dependencies import DependantBase
from di.api.executor import AsyncExecutorProtocol
from di.api.providers import DependencyProvider as Endpoint
from di.api.solved import SolvedDependant
from di.dependant import JoinedDependant
from starlette.datastructures import URLPath
from starlette.requests import HTTPConnection, Request
from starlette.responses import JSONResponse, Response
Expand Down Expand Up @@ -81,7 +80,9 @@ def __init__(
responses: typing.Optional[Responses] = None,
# xpresso params
name: typing.Optional[str] = None,
dependencies: typing.Optional[typing.Sequence[Depends]] = None,
dependencies: typing.Optional[
typing.Sequence[DependantBase[typing.Any]]
] = None,
execute_dependencies_concurrently: bool = False,
response_factory: typing.Callable[[typing.Any], Response] = JSONResponse,
response_encoder: Encoder = JsonableEncoder(),
Expand Down
6 changes: 4 additions & 2 deletions xpresso/routing/pathitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import starlette.routing
import starlette.types
from di.api.dependencies import DependantBase
from di.api.providers import DependencyProvider as Endpoint

import xpresso.binders.dependants as param_dependants
import xpresso.openapi.models as openapi_models
from xpresso.dependencies.models import Depends
from xpresso.responses import Responses
from xpresso.routing.operation import Operation

Expand Down Expand Up @@ -45,7 +45,9 @@ def __init__(
options: typing.Optional[typing.Union[Operation, Endpoint]] = None,
trace: typing.Optional[typing.Union[Operation, Endpoint]] = None,
redirect_slashes: bool = True,
dependencies: typing.Optional[typing.Sequence[Depends]] = None,
dependencies: typing.Optional[
typing.Sequence[DependantBase[typing.Any]]
] = None,
# OpenAPI metadata
include_in_schema: bool = True,
name: typing.Optional[str] = None,
Expand Down
8 changes: 5 additions & 3 deletions xpresso/routing/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
from typing import Protocol

import starlette.middleware
from di.api.dependencies import DependantBase
from starlette.routing import BaseRoute
from starlette.routing import Router as StarletteRouter
from starlette.types import Receive, Scope, Send

from xpresso.dependencies.models import Depends
from xpresso.responses import Responses


Expand All @@ -33,7 +33,7 @@ def __call__(
class Router:
routes: typing.Sequence[BaseRoute]
lifespan: typing.Optional[typing.Callable[..., typing.AsyncContextManager[None]]]
dependencies: typing.Sequence[Depends]
dependencies: typing.Sequence[DependantBase[typing.Any]]
tags: typing.Sequence[str]
responses: Responses
include_in_schema: bool
Expand Down Expand Up @@ -62,7 +62,9 @@ def __init__(
] = None,
redirect_slashes: bool = True,
default: typing.Optional[_ASGIApp] = None,
dependencies: typing.Optional[typing.Sequence[Depends]] = None,
dependencies: typing.Optional[
typing.Sequence[DependantBase[typing.Any]]
] = None,
tags: typing.Optional[typing.List[str]] = None,
responses: typing.Optional[Responses] = None,
include_in_schema: bool = True,
Expand Down
7 changes: 4 additions & 3 deletions xpresso/routing/websockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
import starlette.routing
import starlette.types
import starlette.websockets
from di import AsyncExecutor, BaseContainer, ConcurrentAsyncExecutor
from di import AsyncExecutor, BaseContainer, ConcurrentAsyncExecutor, JoinedDependant
from di.api.dependencies import DependantBase
from di.api.executor import AsyncExecutorProtocol
from di.api.providers import DependencyProvider as Endpoint
from di.api.solved import SolvedDependant
from di.dependant import JoinedDependant

import xpresso._utils.asgi_scope_extension as asgi_scope_extension
from xpresso.dependencies.models import Depends
Expand Down Expand Up @@ -58,7 +57,9 @@ def __init__(
endpoint: Endpoint,
*,
name: typing.Optional[str] = None,
dependencies: typing.Optional[typing.Sequence[Depends]] = None,
dependencies: typing.Optional[
typing.Sequence[DependantBase[typing.Any]]
] = None,
execute_dependencies_concurrently: bool = False,
) -> None:
super().__init__( # type: ignore
Expand Down

0 comments on commit 52489fe

Please sign in to comment.