Skip to content

Commit

Permalink
feat!: replace UnwiredDependant with a paramter on Dependant
Browse files Browse the repository at this point in the history
  • Loading branch information
adriangb committed Oct 11, 2021
1 parent 07476b9 commit 7da5932
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 23 deletions.
4 changes: 2 additions & 2 deletions di/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


from di.container import Container
from di.dependant import Dependant, UnwiredDependant
from di.dependant import Dependant
from di.params import Depends

__all__ = ("Container", "Dependant", "Depends", "UnwiredDependant")
__all__ = ("Container", "Dependant", "Depends")
25 changes: 13 additions & 12 deletions di/dependant.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
AsyncGeneratorProvider,
CallableProvider,
CoroutineProvider,
Dependency,
DependencyProviderType,
DependencyType,
GeneratorProvider,
Expand All @@ -31,6 +30,7 @@ def __init__(
call: Optional[AsyncGeneratorProvider[DependencyType]] = None,
scope: Optional[Scope] = None,
share: bool = True,
autowire: bool = True,
) -> None:
...

Expand All @@ -40,6 +40,7 @@ def __init__(
call: Optional[CoroutineProvider[DependencyType]] = None,
scope: Optional[Scope] = None,
share: bool = True,
autowire: bool = True,
) -> None:
...

Expand All @@ -49,6 +50,7 @@ def __init__(
call: Optional[GeneratorProvider[DependencyType]] = None,
scope: Optional[Scope] = None,
share: bool = True,
autowire: bool = True,
) -> None:
...

Expand All @@ -58,6 +60,7 @@ def __init__(
call: Optional[CallableProvider[DependencyType]] = None,
scope: Optional[Scope] = None,
share: bool = True,
autowire: bool = True,
) -> None:
...

Expand All @@ -66,13 +69,15 @@ def __init__(
call: Optional[DependencyProviderType[DependencyType]] = None,
scope: Scope = None,
share: bool = True,
autowire: bool = True,
) -> None:
self.call = call
self.scope = scope
self.dependencies: Optional[
Dict[str, DependencyParameter[DependantProtocol[Any]]]
] = None
self.share = share
self.autowire = autowire

def __repr__(self) -> str:
share = "" if self.share is False else ", share=True"
Expand Down Expand Up @@ -133,7 +138,7 @@ def gather_dependencies(
)
if isinstance(param.default, Dependant):
sub_dependant: DependantProtocol[Any] = param.default
elif param.default is param.empty:
elif param.default is param.empty and self.autowire:
sub_dependant = self.create_sub_dependant(param)
else:
continue # pragma: no cover
Expand Down Expand Up @@ -164,13 +169,9 @@ def create_sub_dependant(self, param: inspect.Parameter) -> DependantProtocol[An
raise WiringError(
"Cannot wire a parameter with no default and no type annotation"
)
return Dependant[Any](call=param.annotation, scope=self.scope, share=self.share)


class UnwiredDependant(Dependant[Dependency]):
"""A Dependant that does not autowire"""

def get_dependencies(
self,
) -> Dict[str, DependencyParameter[DependantProtocol[Any]]]:
return {}
return Dependant[Any](
call=param.annotation,
scope=self.scope,
share=self.share,
autowire=self.autowire,
)
6 changes: 2 additions & 4 deletions docs/src/gather_deps_example.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import Any, FrozenSet, Iterable, List

from di import Container, Dependant
from di.dependant import UnwiredDependant
from di.types.dependencies import DependantProtocol


Expand Down Expand Up @@ -36,9 +35,8 @@ def gather_scopes(deps: List[DependantProtocol[Any]]) -> List[str]:
async def web_framework() -> None:
container = Container()

# note: use UnwiredDependant so that the container doesn't try
# to wire the Request itself
with container.bind(UnwiredDependant(Request), Request):
# note: use autowire=False so that the container doesn't try to wire the Request itself
with container.bind(Dependant(Request, autowire=False), Request):
scopes = gather_scopes(
container.solve(Dependant(controller)).get_flat_subdependants()
)
Expand Down
4 changes: 2 additions & 2 deletions docs/src/solved_dependant.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import anyio

from di import Container, Dependant, UnwiredDependant
from di import Container, Dependant
from di.types.solved import SolvedDependency

T = TypeVar("T")
Expand All @@ -27,7 +27,7 @@ async def framework() -> None:
container = Container()
request_log = RequestLog()
container.bind(Dependant(lambda: request_log, scope="app"), RequestLog)
container.bind(UnwiredDependant(Request, scope="request"), Request)
container.bind(Dependant(Request, scope="request", autowire=False), Request)
solved = container.solve(Dependant(controller, scope="request"))
async with container.enter_global_scope("app"):
# simulate concurrent requests
Expand Down
4 changes: 2 additions & 2 deletions docs/src/starlette/src.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from starlette.requests import Request
from starlette.routing import Route

from di import Container, Dependant, UnwiredDependant
from di import Container, Dependant


class WiredGetRoute(Route):
Expand All @@ -32,7 +32,7 @@ async def wrapped_endpoint(request: Request) -> Any:
class App(Starlette):
def __init__(self, container: Container | None = None, **kwargs: Any) -> None:
self.container = container or Container()
self.container.bind(UnwiredDependant(Request), Request)
self.container.bind(Dependant(Request, autowire=False), Request)

@contextlib.asynccontextmanager
async def lifespan(app: App):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "di"
version = "0.13.0"
version = "0.14.0"
description = "Autowiring dependency injection"
authors = ["Adrian Garcia Badaracco <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit 7da5932

Please sign in to comment.