Skip to content

Commit

Permalink
Rename Dependant to Dependent (#90)
Browse files Browse the repository at this point in the history
* Rename Dependant to Dependent

Fixes #89
  • Loading branch information
adriangb authored Nov 2, 2022
1 parent 9cfbeff commit 7474113
Show file tree
Hide file tree
Showing 64 changed files with 411 additions and 411 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Here is a simple example of how `di` works:
```python
from dataclasses import dataclass

from di import Container, Dependant, SyncExecutor
from di import Container, Dependent, SyncExecutor


class A:
Expand All @@ -60,7 +60,7 @@ class C:
def main():
container = Container()
executor = SyncExecutor()
solved = container.solve(Dependant(C, scope="request"), scopes=["request"])
solved = container.solve(Dependent(C, scope="request"), scopes=["request"])
with container.enter_scope("request") as state:
c = container.execute_sync(solved, executor=executor, state=state)
assert isinstance(c, C)
Expand Down
6 changes: 3 additions & 3 deletions benchmarks/solve.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from benchmarks.utils import GraphSize, SleepTimes, generate_dag
from di.api.executor import SupportsAsyncExecutor, SupportsSyncExecutor
from di.container import Container
from di.dependant import Dependant
from di.dependent import Dependent
from di.executors import AsyncExecutor, ConcurrentAsyncExecutor, SyncExecutor

INTERVAL = 10e-6 # 10 us
Expand All @@ -25,7 +25,7 @@ async def async_bench(
) -> None:
container = Container()
solved = container.solve(
Dependant(generate_dag(graph, sync=False, sleep=sleep)), scopes=[None]
Dependent(generate_dag(graph, sync=False, sleep=sleep)), scopes=[None]
)
p = Profiler()
async with container.enter_scope(None) as state:
Expand All @@ -49,7 +49,7 @@ def sync_bench(
) -> None:
container = Container()
solved = container.solve(
Dependant(generate_dag(graph, sync=True, sleep=sleep)), scopes=[None]
Dependent(generate_dag(graph, sync=True, sleep=sleep)), scopes=[None]
)
executor = SyncExecutor()
p = Profiler()
Expand Down
8 changes: 4 additions & 4 deletions benchmarks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import anyio # noqa

from di.dependant import Dependant
from di.dependent import Dependent
from di.typing import Annotated

random = Random(0)
Expand Down Expand Up @@ -52,7 +52,7 @@ async def fasync(secs: float) -> None:
globals: Dict[str, Any] = {
"Annotated": Annotated,
"sleep": sleep_func,
"Dependant": Dependant,
"Dependent": Dependent,
}

funcs: Dict[str, Callable[..., Any]] = {}
Expand All @@ -66,7 +66,7 @@ async def fasync(secs: float) -> None:
)
params = ", ".join(
[
f"dep_{dep_name}: Annotated[None, Dependant({dep_name})]"
f"dep_{dep_name}: Annotated[None, Dependent({dep_name})]"
for dep_name in deps
]
)
Expand All @@ -77,7 +77,7 @@ async def fasync(secs: float) -> None:
name = "final"
deps = list(funcs.keys())
params = ", ".join(
[f"dep_{dep_name}: Annotated[None, Dependant({dep_name})]" for dep_name in deps]
[f"dep_{dep_name}: Annotated[None, Dependent({dep_name})]" for dep_name in deps]
)
func_def = template.format(name, params, 0)
exec(func_def, globals, funcs)
Expand Down
8 changes: 4 additions & 4 deletions di/_utils/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
)
from di._utils.scope_map import ScopeMap
from di._utils.types import CacheKey
from di.api.dependencies import DependantBase
from di.api.dependencies import DependentBase
from di.api.providers import DependencyProvider
from di.api.scopes import Scope
from di.exceptions import IncompatibleDependencyError
Expand All @@ -47,7 +47,7 @@ class Task:
"user_function",
"scope",
"cache_key",
"dependant",
"dependent",
"task_id",
"call_user_func_with_deps",
"compute",
Expand All @@ -66,15 +66,15 @@ def __init__(
call: DependencyProvider,
use_cache: bool,
cache_key: CacheKey,
dependant: DependantBase[Any],
dependent: DependentBase[Any],
task_id: int,
positional_parameters: Iterable[Task],
keyword_parameters: Mapping[str, Task],
) -> None:
self.scope = scope
self.user_function = call
self.cache_key = cache_key
self.dependant = dependant
self.dependent = dependent
self.task_id = task_id
if is_async_gen_callable(self.user_function):
self.wrapped_call = contextlib.asynccontextmanager(call) # type: ignore[arg-type]
Expand Down
14 changes: 7 additions & 7 deletions di/api/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

__all__ = (
"CacheKey",
"DependantBase",
"DependentBase",
"DependencyParameter",
"InjectableClassProvider",
)
Expand All @@ -25,13 +25,13 @@
@runtime_checkable
class InjectableClassProvider(Protocol):
@classmethod
def __di_dependency__(cls, param: inspect.Parameter) -> "DependantBase[Any]":
def __di_dependency__(cls, param: inspect.Parameter) -> "DependentBase[Any]":
...


class DependantBase(Generic[T]):
"""A dependant is an object that can provide the container with:
- A hash, to compare itself against other dependants
class DependentBase(Generic[T]):
"""A dependent is an object that can provide the container with:
- A hash, to compare itself against other dependents
- A scope
- A callable who's returned value is the dependency
"""
Expand All @@ -45,10 +45,10 @@ def cache_key(self) -> CacheKey:
raise NotImplementedError

def get_dependencies(self) -> "List[DependencyParameter]":
"""Collect all of the sub dependencies for this dependant"""
"""Collect all of the sub dependencies for this dependent"""
raise NotImplementedError


class DependencyParameter(NamedTuple):
dependency: DependantBase[Any]
dependency: DependentBase[Any]
parameter: Optional[inspect.Parameter]
4 changes: 2 additions & 2 deletions di/api/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
else:
from typing import Protocol

from di.api.dependencies import DependantBase
from di.api.dependencies import DependentBase

StateType = TypeVar("StateType")


class Task(Hashable, Protocol[StateType]):
dependant: DependantBase[StateType]
dependent: DependentBase[StateType]

@property
def compute(self) -> Callable[[StateType], Union[Awaitable[None], None]]:
Expand Down
16 changes: 8 additions & 8 deletions di/api/solved.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import typing
from typing import Any, Generic, Iterable, Mapping, TypeVar

from di.api.dependencies import DependantBase, DependencyParameter
from di.api.dependencies import DependencyParameter, DependentBase

Dependency = Any

DependencyType = TypeVar("DependencyType")


class SolvedDependant(Generic[DependencyType]):
class SolvedDependent(Generic[DependencyType]):
"""Representation of a fully solved dependency as DAG.
A SolvedDependant could be a user's endpoint/controller function.
A SolvedDependent could be a user's endpoint/controller function.
"""

__slots__ = ("dependency", "dag", "container_cache")

dependency: DependantBase[DependencyType]
dag: Mapping[DependantBase[Any], Iterable[DependencyParameter]]
dependency: DependentBase[DependencyType]
dag: Mapping[DependentBase[Any], Iterable[DependencyParameter]]
# container_cache can be used by the creating container to store data that is tied
# to the SolvedDependant
# to the SolvedDependent
container_cache: typing.Any

def __init__(
self,
dependency: DependantBase[DependencyType],
dag: Mapping[DependantBase[Any], Iterable[DependencyParameter]],
dependency: DependentBase[DependencyType],
dag: Mapping[DependentBase[Any], Iterable[DependencyParameter]],
container_cache: typing.Any,
):
self.dependency = dependency
Expand Down
10 changes: 5 additions & 5 deletions di/container/_bind_by_type_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
from typing import Any, Optional

from di._utils.inspect import get_type
from di.api.dependencies import DependantBase
from di.api.dependencies import DependentBase
from di.container._bind_hook import BindHook


def bind_by_type(
provider: DependantBase[Any],
provider: DependentBase[Any],
dependency: type,
*,
covariant: bool = False,
) -> BindHook:
"""Hook to substitute the matched dependency"""

def hook(
param: Optional[inspect.Parameter], dependant: DependantBase[Any]
) -> Optional[DependantBase[Any]]:
if dependant.call is dependency:
param: Optional[inspect.Parameter], dependent: DependentBase[Any]
) -> Optional[DependentBase[Any]]:
if dependent.call is dependency:
return provider
if param is None:
return None
Expand Down
6 changes: 3 additions & 3 deletions di/container/_bind_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
from typing import Protocol


from di.api.dependencies import DependantBase
from di.api.dependencies import DependentBase


class BindHook(Protocol):
def __call__(
self, param: Optional[inspect.Parameter], dependant: DependantBase[Any]
) -> Optional[DependantBase[Any]]: # pragma: no cover
self, param: Optional[inspect.Parameter], dependent: DependentBase[Any]
) -> Optional[DependentBase[Any]]: # pragma: no cover
...
14 changes: 7 additions & 7 deletions di/container/_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
)

from di._utils.types import FusedContextManager
from di.api.dependencies import DependantBase
from di.api.dependencies import DependentBase
from di.api.executor import SupportsAsyncExecutor, SupportsSyncExecutor
from di.api.providers import DependencyProvider
from di.api.scopes import Scope
from di.api.solved import SolvedDependant
from di.api.solved import SolvedDependent
from di.container._bind_hook import BindHook
from di.container._execution_planning import plan_execution
from di.container._solving import ScopeResolver, solve
Expand All @@ -30,7 +30,7 @@ class Container:
Generally you will want one Container per application.
There is not performance advantage to re-using a container, the only reason to do so is to share binds.
For each "thing" you want to wire with di and execute you'll want to call `Container.solve()`
exactly once and then keep a reference to the returned `SolvedDependant` to pass to `Container.execute`.
exactly once and then keep a reference to the returned `SolvedDependent` to pass to `Container.execute`.
Solving is very expensive so avoid doing it in a hot loop.
"""

Expand Down Expand Up @@ -64,10 +64,10 @@ def unbind() -> "Generator[None, None, None]":

def solve(
self,
dependency: DependantBase[DependencyType],
dependency: DependentBase[DependencyType],
scopes: Sequence[Scope],
scope_resolver: Optional[ScopeResolver] = None,
) -> SolvedDependant[DependencyType]:
) -> SolvedDependent[DependencyType]:
"""Build the dependency graph.
Should happen once, maybe during startup.
Expand All @@ -78,7 +78,7 @@ def solve(

def execute_sync(
self,
solved: SolvedDependant[DependencyType],
solved: SolvedDependent[DependencyType],
executor: SupportsSyncExecutor,
*,
state: ContainerState,
Expand All @@ -100,7 +100,7 @@ def execute_sync(

async def execute_async(
self,
solved: SolvedDependant[DependencyType],
solved: SolvedDependent[DependencyType],
executor: SupportsAsyncExecutor,
*,
state: ContainerState,
Expand Down
10 changes: 5 additions & 5 deletions di/container/_execution_planning.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from di.api.executor import Task as SupportsTask
from di.api.providers import DependencyProvider
from di.api.scopes import Scope
from di.api.solved import SolvedDependant
from di.api.solved import SolvedDependent


class TaskGraph:
Expand Down Expand Up @@ -56,8 +56,8 @@ def static_order(self) -> Iterable[Task]:
return self._static_order


class SolvedDependantCache(NamedTuple):
"""Private data that the Container attaches to SolvedDependant"""
class SolvedDependentCache(NamedTuple):
"""Private data that the Container attaches to SolvedDependent"""

root_task: Task
topological_sorter: TopologicalSorter[Task]
Expand All @@ -71,10 +71,10 @@ class SolvedDependantCache(NamedTuple):
def plan_execution(
stacks: Mapping[Scope, Union[AsyncExitStack, ExitStack]],
cache: ScopeMap[CacheKey, Any],
solved: SolvedDependant[Any],
solved: SolvedDependent[Any],
values: Optional[Mapping[DependencyProvider, Any]] = None,
) -> Tuple[List[Any], SupportsTaskGraph[ExecutionState], ExecutionState, Task,]:
solved_dependency_cache: SolvedDependantCache = solved.container_cache
solved_dependency_cache: SolvedDependentCache = solved.container_cache
results = solved_dependency_cache.empty_results.copy()
if values is None:
values = EMPTY_VALUES
Expand Down
Loading

0 comments on commit 7474113

Please sign in to comment.