mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-02-16 19:40:59 +03:00
Add fix for @inject decorator with classes on PyPy and Py3
This commit is contained in:
parent
2c85b38113
commit
512544ea9f
|
@ -1,5 +1,6 @@
|
||||||
"""Injections module."""
|
"""Injections module."""
|
||||||
|
|
||||||
|
import sys
|
||||||
import six
|
import six
|
||||||
|
|
||||||
from .utils import is_provider
|
from .utils import is_provider
|
||||||
|
@ -9,6 +10,13 @@ from .utils import get_injectable_kwargs
|
||||||
from .errors import Error
|
from .errors import Error
|
||||||
|
|
||||||
|
|
||||||
|
IS_PYPY = '__pypy__' in sys.builtin_module_names
|
||||||
|
if IS_PYPY or six.PY3: # pragma: no cover
|
||||||
|
OBJECT_INIT = six.get_unbound_function(object.__init__)
|
||||||
|
else: # pragma: no cover
|
||||||
|
OBJECT_INIT = None
|
||||||
|
|
||||||
|
|
||||||
class Injection(object):
|
class Injection(object):
|
||||||
|
|
||||||
"""Base injection class."""
|
"""Base injection class."""
|
||||||
|
@ -68,11 +76,11 @@ def inject(*args, **kwargs):
|
||||||
cls = callback
|
cls = callback
|
||||||
try:
|
try:
|
||||||
cls_init = six.get_unbound_function(cls.__init__)
|
cls_init = six.get_unbound_function(cls.__init__)
|
||||||
assert cls_init is not object.__init__
|
assert cls_init is not OBJECT_INIT
|
||||||
except (AttributeError, AssertionError):
|
except (AttributeError, AssertionError):
|
||||||
raise Error(
|
raise Error(
|
||||||
'Class {0} has no __init__() '.format(cls.__module__,
|
'Class {0}.{1} has no __init__() '.format(cls.__module__,
|
||||||
cls.__name__) +
|
cls.__name__) +
|
||||||
'method and could not be decorated with @inject decorator')
|
'method and could not be decorated with @inject decorator')
|
||||||
cls.__init__ = decorator(cls_init)
|
cls.__init__ = decorator(cls_init)
|
||||||
return cls
|
return cls
|
||||||
|
|
Loading…
Reference in New Issue
Block a user