Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_positional_arg_as_injectable_raises_type_error(self):
with pytest.raises(TypeError):
autowired(['b'])(foo)
def test_autowiring(self):
caller_args = (False, None, True)
caller_kwargs = {
'w': {},
'x': "string",
'f': lambda x: print(x),
'kwargs': {'extra': True},
}
_, kwargs = autowired()(foo)(*caller_args, **caller_kwargs)
assert isinstance(kwargs['y'], bool)
assert isinstance(kwargs['z'], DummyClass)
assert isinstance(kwargs['l'], DummyClass)
assert isinstance(kwargs['s'], DummyClass)
def test_injectable_with_non_instantiable_class_raises_type_error(self):
# eligible injectable argument is annotated with non
# instantiable class
with pytest.raises(TypeError):
autowired()(baz)
with pytest.raises(TypeError):
autowired()(qux)
# specified argument for injection is annotated with non
# instantiable class
with pytest.raises(TypeError):
autowired(['nope'])(baz)
with pytest.raises(TypeError):
autowired(['nope'])(qux)
@autowired(lazy=True)
def __init__(self, *, dep1: CircularDependant1, dep2: CircularDependant2):
self.dep1 = dep1
self.dep2 = dep2
def test_missing_kwonly_args_raises_type_error(self):
with pytest.raises(TypeError):
autowired()(foo)(a=None, b=10, c='')
def test_injectable_lazy_initialization(self):
caller_args = (False, None, True)
caller_kwargs = {
'w': {},
'x': "string",
'f': lambda x: print(x),
'kwargs': {'extra': True},
}
_, kwargs = autowired()(foo)(*caller_args, **caller_kwargs)
assert isinstance(kwargs['l'], Proxy)
assert isinstance(kwargs['l'], DummyClass)
assert kwargs['l'].dummy_method() == 42
@autowired
def __init__(
self,
composite_singleton: Autowired(CompositeSingleton),
simple_singleton: Autowired(SimpleSingleton),
):
self.composite_singleton = composite_singleton
self.simple_singleton = simple_singleton
@autowired
def __init__(
self,
simple_service: Autowired(SimpleService),
dependable_service: Autowired(DependableService),
):
self.simple_service = simple_service
self.dependable_service = dependable_service
@autowired
def __init__(
self, client: Autowired(ExternalClient),
):
self.client = client
@autowired
def __init__(self, service_b: Autowired("B", lazy=True)):
self.service_b = service_b
self.some_property = "some property from A"