mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-03-03 19:25:48 +03:00
Refactor inject decorator
This commit is contained in:
parent
c8fda3da8d
commit
9f2a521888
|
@ -225,18 +225,11 @@ def inject(*args, **kwargs):
|
||||||
"""Dependency injection decorator."""
|
"""Dependency injection decorator."""
|
||||||
if isinstance(callback_or_cls, six.class_types):
|
if isinstance(callback_or_cls, six.class_types):
|
||||||
cls = callback_or_cls
|
cls = callback_or_cls
|
||||||
try:
|
cls.__init__ = decorator(_fetch_cls_init(cls))
|
||||||
cls_init = six.get_unbound_function(cls.__init__)
|
|
||||||
assert cls_init is not _OBJECT_INIT
|
|
||||||
except (AttributeError, AssertionError):
|
|
||||||
raise Error(
|
|
||||||
'Class {0}.{1} has no __init__() '.format(cls.__module__,
|
|
||||||
cls.__name__) +
|
|
||||||
'method and could not be decorated with @inject decorator')
|
|
||||||
cls.__init__ = decorator(cls_init)
|
|
||||||
return cls
|
return cls
|
||||||
|
|
||||||
callback = callback_or_cls
|
callback = callback_or_cls
|
||||||
|
|
||||||
if hasattr(callback, 'injections'):
|
if hasattr(callback, 'injections'):
|
||||||
callback.args += arg_injections
|
callback.args += arg_injections
|
||||||
callback.kwargs += kwarg_injections
|
callback.kwargs += kwarg_injections
|
||||||
|
@ -263,6 +256,19 @@ def inject(*args, **kwargs):
|
||||||
return decorator
|
return decorator
|
||||||
|
|
||||||
|
|
||||||
|
def _fetch_cls_init(cls):
|
||||||
|
"""Return reference to the class.__init__() method if it is defined."""
|
||||||
|
try:
|
||||||
|
cls_init = six.get_unbound_function(cls.__init__)
|
||||||
|
assert cls_init is not _OBJECT_INIT
|
||||||
|
except (AttributeError, AssertionError):
|
||||||
|
raise Error(
|
||||||
|
'Class {0}.{1} has no __init__() '.format(cls.__module__,
|
||||||
|
cls.__name__) +
|
||||||
|
'method and could not be decorated with @inject decorator')
|
||||||
|
return cls_init
|
||||||
|
|
||||||
|
|
||||||
def _parse_args_injections(args):
|
def _parse_args_injections(args):
|
||||||
"""Parse positional argument injections according to current syntax."""
|
"""Parse positional argument injections according to current syntax."""
|
||||||
return tuple(Arg(arg) if not is_injection(arg) else arg
|
return tuple(Arg(arg) if not is_injection(arg) else arg
|
||||||
|
|
Loading…
Reference in New Issue
Block a user