Skip to content

Commit

Permalink
feat!: add wire=False parameter to skip wiring of dependency itself
Browse files Browse the repository at this point in the history
  • Loading branch information
adriangb committed Oct 12, 2021
1 parent c00b13c commit 8ec5861
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
8 changes: 8 additions & 0 deletions di/dependant.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def __init__(
call: Optional[AsyncGeneratorProvider[DependencyType]] = None,
scope: Optional[Scope] = None,
share: bool = True,
wire: bool = True,
autowire: bool = True,
) -> None:
...
Expand All @@ -40,6 +41,7 @@ def __init__(
call: Optional[CoroutineProvider[DependencyType]] = None,
scope: Optional[Scope] = None,
share: bool = True,
wire: bool = True,
autowire: bool = True,
) -> None:
...
Expand All @@ -50,6 +52,7 @@ def __init__(
call: Optional[GeneratorProvider[DependencyType]] = None,
scope: Optional[Scope] = None,
share: bool = True,
wire: bool = True,
autowire: bool = True,
) -> None:
...
Expand All @@ -60,6 +63,7 @@ def __init__(
call: Optional[CallableProvider[DependencyType]] = None,
scope: Optional[Scope] = None,
share: bool = True,
wire: bool = True,
autowire: bool = True,
) -> None:
...
Expand All @@ -69,6 +73,7 @@ def __init__(
call: Optional[DependencyProviderType[DependencyType]] = None,
scope: Scope = None,
share: bool = True,
wire: bool = True,
autowire: bool = True,
) -> None:
self.call = call
Expand All @@ -78,6 +83,7 @@ def __init__(
] = None
self.share = share
self.autowire = autowire
self.wire = wire

def __repr__(self) -> str:
share = "" if self.share is False else ", share=True"
Expand Down Expand Up @@ -127,6 +133,8 @@ def gather_dependencies(
The returned dict corresponds to keyword arguments that will be passed
to this dependencies `call` after all sub-dependencies are themselves resolved.
"""
if self.wire is False:
return []
assert (
self.call is not None
), "Container should have assigned call; this is a bug!"
Expand Down
7 changes: 6 additions & 1 deletion di/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def Depends(
*,
scope: Scope = None,
share: bool = True,
wire: bool = True,
autowire: bool = True,
) -> DependencyType:
...
Expand All @@ -32,6 +33,7 @@ def Depends(
*,
scope: Scope = None,
share: bool = True,
wire: bool = True,
autowire: bool = True,
) -> DependencyType:
...
Expand All @@ -43,6 +45,7 @@ def Depends(
*,
scope: Scope = None,
share: bool = True,
wire: bool = True,
autowire: bool = True,
) -> DependencyType:
...
Expand All @@ -54,6 +57,7 @@ def Depends(
*,
scope: Scope = None,
share: bool = True,
wire: bool = True,
autowire: bool = True,
) -> DependencyType:
...
Expand All @@ -64,6 +68,7 @@ def Depends(
*,
scope: Scope = None,
share: bool = True,
wire: bool = True,
autowire: bool = True,
) -> DependencyType:
return Dependant(call=call, scope=scope, share=share, autowire=autowire) # type: ignore
return Dependant(call=call, scope=scope, share=share, wire=wire, autowire=autowire) # type: ignore
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.15.0"
version = "0.17.0"
description = "Autowiring dependency injection"
authors = ["Adrian Garcia Badaracco <adrian@adriangb.com>"]
readme = "README.md"
Expand Down
22 changes: 15 additions & 7 deletions tests/test_solving.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,24 @@ def gather_dependencies(
assert calls == [True]


def test_no_autowire() -> None:
"""Specifying autowire=False skips autowiring non explicit dependencies"""
class CannotBeWired:
# cannot be autowired because of *args, **kwargs
def __init__(self, *args, **kwargs) -> None:
...

class CannotBeAutoWired:
# cannot be autowired because of *args, **kwargs
def __init__(self, *args, **kwargs) -> None:
...

def collect(thing: CannotBeAutoWired) -> None:
def test_no_autowire() -> None:
"""Specifying autowire=False skips autowiring non explicit sub dependencies"""

def collect(bad: CannotBeWired) -> None:
...

container = Container()
container.solve(Dependant(collect, autowire=False))


def test_no_wire() -> None:
"""Specifying wire=False skips wiring on the dependency itself"""

container = Container()
container.solve(Dependant(CannotBeWired, wire=False))

0 comments on commit 8ec5861

Please sign in to comment.