Skip to content

Commit

Permalink
feat(wip): threading support(beta)
Browse files Browse the repository at this point in the history
  • Loading branch information
raceychan committed Nov 23, 2024
1 parent 1f0f698 commit 29c1b74
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 53 deletions.
17 changes: 9 additions & 8 deletions ididi/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -862,14 +862,15 @@ def auth_service_factory() -> AuthService: ...
old_node = self._nodes[resolved_type]
self.remove_node(old_node)

if inspect.isgeneratorfunction(factory_or_class):
func_gen = contextmanager(factory_or_class)
node = DependentNode[T].from_node(func_gen, config=node_config)
elif inspect.isasyncgenfunction(factory_or_class):
func_gen = asynccontextmanager(factory_or_class)
node = DependentNode[T].from_node(func_gen, config=node_config)
else:
node = DependentNode[T].from_node(factory_or_class, config=node_config)
with self._lock:
if inspect.isgeneratorfunction(factory_or_class):
func_gen = contextmanager(factory_or_class)
node = DependentNode[T].from_node(func_gen, config=node_config)
elif inspect.isasyncgenfunction(factory_or_class):
func_gen = asynccontextmanager(factory_or_class)
node = DependentNode[T].from_node(func_gen, config=node_config)
else:
node = DependentNode[T].from_node(factory_or_class, config=node_config)

self.register_node(node)
return factory_or_class
45 changes: 45 additions & 0 deletions tests/features/test_nonreuse_trans.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import pytest

from ididi import DependencyGraph
from ididi.errors import ReusabilityConflictError


class Config:
def __init__(self, file: str = "file"):
self.file = file


class Database:
def __init__(self, config: Config):
self.config = config


class Repository:
def __init__(self, db: Database):
self.db = db


class AuthService:
def __init__(self, repo: Repository):
self.repo = repo


def test_reuse_before_nonreuse_error():
dg = DependencyGraph()
dg.node(reuse=False)(Database)
with pytest.raises(ReusabilityConflictError):
dg.static_resolve(AuthService)


def test_nonreuse_before_nonreuse():
dg = DependencyGraph()
dg.node(reuse=False)(Database)
dg.node(reuse=False)(Repository)
dg.node(reuse=False)(AuthService)
dg.static_resolve(AuthService)


def test_nonreuse_before_reuse():
dg = DependencyGraph()
dg.node(reuse=False)(AuthService)
dg.static_resolve(AuthService)
45 changes: 0 additions & 45 deletions tests/test_feat.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,3 @@
run test with: make feat
"""

import pytest

from ididi import DependencyGraph
from ididi.errors import ReusabilityConflictError


class Config:
def __init__(self, file: str = "file"):
self.file = file


class Database:
def __init__(self, config: Config):
self.config = config


class Repository:
def __init__(self, db: Database):
self.db = db


class AuthService:
def __init__(self, repo: Repository):
self.repo = repo


def test_reuse_before_nonreuse_error():
dg = DependencyGraph()
dg.node(reuse=False)(Database)
with pytest.raises(ReusabilityConflictError):
dg.static_resolve(AuthService)


def test_nonreuse_before_nonreuse():
dg = DependencyGraph()
dg.node(reuse=False)(Database)
dg.node(reuse=False)(Repository)
dg.node(reuse=False)(AuthService)
dg.static_resolve(AuthService)


def test_nonreuse_before_reuse():
dg = DependencyGraph()
dg.node(reuse=False)(AuthService)
dg.static_resolve(AuthService)

0 comments on commit 29c1b74

Please sign in to comment.