Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
>>> 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'")
@wraps(func)
def wrapper(*args, **kwargs):
bound_arguments = signature.bind_partial(*args, **kwargs).arguments
args = list(args)
for parameter in autowired_parameters:
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'")
@wraps(func)
def wrapper(*args, **kwargs):
bound_arguments = signature.bind_partial(*args, **kwargs).arguments
args = list(args)
for parameter in autowired_parameters:
if parameter.name in bound_arguments:
continue
dependency = parameter.annotation.inject()
if parameter.kind is parameter.POSITIONAL_ONLY:
args.append(dependency)
else:
kwargs[parameter.name] = dependency
return func(*args, **kwargs)
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'")
@wraps(func)
def wrapper(*args, **kwargs):
bound_arguments = signature.bind_partial(*args, **kwargs).arguments
args = list(args)
for parameter in autowired_parameters:
if parameter.name in bound_arguments:
continue
dependency = parameter.annotation.inject()
if parameter.kind is parameter.POSITIONAL_ONLY:
args.append(dependency)
else:
... ...
"""
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'")
@wraps(func)
def wrapper(*args, **kwargs):
bound_arguments = signature.bind_partial(*args, **kwargs).arguments
args = list(args)
for parameter in autowired_parameters:
if parameter.name in bound_arguments:
continue
dependency = parameter.annotation.inject()
if parameter.kind is parameter.POSITIONAL_ONLY: