Skip to content

Autowired descriptor with hacky typing support #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/anemic/ioc/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .container import IOCContainer
from .autowired import autowired
36 changes: 36 additions & 0 deletions src/anemic/ioc/autowired.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from typing import Any, TypeVar, cast

from ..decorators import reify_attr

__all__ = [
"autowired",
]

T = TypeVar("T")


class SentinelType(Any):
pass


def autowired(
interface: type[T] = SentinelType,
*,
name: str = "",
context: Any = None,
) -> T:
# Default for IOCContainer.get is object
if interface is SentinelType:
interface = cast(type[T], object)

@reify_attr[T]
def getter(self) -> T:
return self.container.get(
interface=interface,
name=name,
context=context,
)

# TODO: cast to T from reify_attr[T], because PyCharm
# doesn't understand types of custom descriptors
return cast(T, getter)
65 changes: 65 additions & 0 deletions tests/anemic_test/ioc/test_autowired.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import pytest

from anemic.ioc.autowired import autowired
from anemic.ioc.container import FactoryRegistry, IOCContainer


class ServiceA:
times_inited = 0

def __init__(self, container):
ServiceA.times_inited += 1
self.container = container

def foo(self):
return "foo"


class ServiceB:
a = autowired(ServiceA)

def __init__(self, container):
self.container = container

def foobar(self):
return self.a.foo() + "bar"


@pytest.fixture(autouse=True)
def setup():
ServiceA.times_inited = 0


@pytest.fixture
def registry():
ret = FactoryRegistry("application")
ret.register(
interface=ServiceA,
factory=ServiceA,
)
ret.register(
interface=ServiceB,
factory=ServiceB,
)
return ret


@pytest.fixture
def container(registry):
return IOCContainer(registry)


def test_autowired_attribute_access(container: IOCContainer):
assert ServiceA.times_inited == 0 # sanity
b = container.get(interface=ServiceB)
assert ServiceA.times_inited == 0 # ServiceA not initialized yet
a = b.a
assert ServiceA.times_inited == 1 # ServiceA initialized after b.a accessed
assert isinstance(a, ServiceA)
assert a is b.a # same instance
assert ServiceA.times_inited == 1 # ServiceA not initialized again


def test_autowired_method_call(container: IOCContainer):
b = container.get(interface=ServiceB)
assert b.foobar() == "foobar"