Skip to content

Commit

Permalink
chores: code maintainence
Browse files Browse the repository at this point in the history
  • Loading branch information
raceychan committed Feb 4, 2025
1 parent a96c165 commit 5ed1533
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 23 deletions.
19 changes: 0 additions & 19 deletions ididi/_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,6 @@ def replace_type(self, param_type: type[T]) -> "Dependency[T]":
default=self.default,
)

def replace_default(self, default: T) -> "Dependency[T]":
return Dependency(
name=self.name,
param_kind=self.param_kind,
param_type=self.param_type,
default=default,
)


def unpack_to_deps(
param_annotation: Annotated[Any, "Unpack"]
Expand Down Expand Up @@ -411,21 +403,10 @@ def analyze_unsolved_params(

for param_name, param in self.dependencies.filter_ignore(ignore):
param_type = cast(Union[type, ForwardRef], param.param_type)

if get_origin(param_type) is Annotated:
if node := resolve_use(param_type):
yield param
self.dependencies.update(
param_name, param.replace_type(node.dependent_type)
)
continue

if isinstance(param_type, ForwardRef):
new_type = resolve_forwardref(self.dependent_type, param_type)
param = param.replace_type(new_type)

yield param

if param.param_type != param_type:
self.dependencies.update(param_name, param)

Expand Down
3 changes: 2 additions & 1 deletion ididi/_type_resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ def get_bases(dependent: type) -> tuple[type, ...]:
def resolve_node_type(dependent: INode[P, T]) -> type[T]:
if is_class(dependent) or is_new_type(dependent):
dependent_type = resolve_annotation(dependent)
if get_origin(dependent_type) is Annotated:
dependent_type, *_ = get_args(dependent_type)
else:
dependent_type = resolve_factory(dependent)

return cast(type[T], dependent_type)
8 changes: 6 additions & 2 deletions ididi/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from inspect import isawaitable, iscoroutinefunction
from types import MappingProxyType, TracebackType
from typing import (
Annotated,
Any,
AsyncContextManager,
Awaitable,
Expand All @@ -19,6 +20,7 @@
Union,
cast,
final,
get_args,
get_origin,
overload,
)
Expand Down Expand Up @@ -405,6 +407,10 @@ def dfs(dependent_type: type[T], dignore: GraphIgnore) -> DependentNode[T]:

if is_class(dependent) or is_new_type(dependent):
dependent_type = resolve_annotation(dependent)
if get_origin(dependent_type) is Annotated:
if node := resolve_use(dependent_type):
self._node(node.factory)
dependent_type, *_ = get_args(dependent_type)
else:
dependent_type = resolve_factory(dependent)
if dependent_type not in self._nodes:
Expand Down Expand Up @@ -1183,5 +1189,3 @@ async def __aexit__(


DependencyGraph = Graph


11 changes: 10 additions & 1 deletion tests/features/test_reuse.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ididi.graph import DependencyGraph
from ididi import DependencyGraph, use

dg = DependencyGraph()

Expand All @@ -20,6 +20,15 @@ def test_config_eq():
assert c1 != c2


def test_remove_annotated():

def config_factory() -> Config: ...

dg = DependencyGraph()
dg.node(config_factory)
dg.remove_dependent(use(config_factory))


class Engine:
def __init__(self, config: Config):
self.config = config
Expand Down

0 comments on commit 5ed1533

Please sign in to comment.