Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
.. note::
This decorator accepts no arguments and must be used without trailing parenthesis.
Usage::
>>> from injectable import Autowired, autowired
>>>
>>> @autowired
... def foo(dep: Autowired(...)):
... ...
"""
signature = inspect.signature(func)
autowired_parameters = []
for index, parameter in enumerate(signature.parameters.values()):
if not isinstance(parameter.annotation, _Autowired):
if len(autowired_parameters) == 0 or parameter.kind in [
parameter.KEYWORD_ONLY,
parameter.VAR_KEYWORD,
]:
continue
raise AutowiringError(
"Non-Autowired positional parameter follows Autowired parameter"
)
if parameter.default is not parameter.empty:
raise AutowiringError("Default value assigned to Autowired parameter")
if parameter.kind in (parameter.VAR_POSITIONAL, parameter.VAR_KEYWORD):
raise AutowiringError(f"Autowired parameter is of kind {parameter.kind}")
autowired_parameters.append(parameter)
if len(autowired_parameters) == 0:
raise AutowiringError("No parameter is typed with 'Autowired'")
def __new__(
cls,
dependency: Union[T, str],
*,
namespace: str = None,
group: str = None,
exclude_groups: Sequence[str] = None,
lazy: bool = False,
) -> T:
return _Autowired(
dependency,
namespace=namespace,
group=group,
exclude_groups=exclude_groups,
lazy=lazy,
)