Add types declaration for containers

This commit is contained in:
Roman Mogilatov 2017-03-26 22:38:26 +03:00
parent 622a6d08e5
commit 7840e2a13e
2 changed files with 857 additions and 850 deletions

File diff suppressed because it is too large Load Diff

View File

@ -88,7 +88,7 @@ class DynamicContainer(object):
del self.providers[name] del self.providers[name]
super(DynamicContainer, self).__delattr__(name) super(DynamicContainer, self).__delattr__(name)
def override(self, overriding): def override(self, object overriding):
"""Override current container by overriding container. """Override current container by overriding container.
:param overriding: Overriding container. :param overriding: Overriding container.
@ -138,8 +138,12 @@ class DynamicContainer(object):
class DeclarativeContainerMetaClass(type): class DeclarativeContainerMetaClass(type):
"""Declarative inversion of control container meta class.""" """Declarative inversion of control container meta class."""
def __new__(mcs, class_name, bases, attributes): def __new__(type mcs, str class_name, tuple bases, dict attributes):
"""Declarative container class factory.""" """Declarative container class factory."""
cdef tuple cls_providers
cdef tuple inherited_providers
cdef type cls
cls_providers = tuple((name, provider) cls_providers = tuple((name, provider)
for name, provider in six.iteritems(attributes) for name, provider in six.iteritems(attributes)
if isinstance(provider, Provider)) if isinstance(provider, Provider))
@ -154,14 +158,14 @@ class DeclarativeContainerMetaClass(type):
attributes['inherited_providers'] = dict(inherited_providers) attributes['inherited_providers'] = dict(inherited_providers)
attributes['providers'] = dict(cls_providers + inherited_providers) attributes['providers'] = dict(cls_providers + inherited_providers)
cls = type.__new__(mcs, class_name, bases, attributes) cls = <type>type.__new__(mcs, class_name, bases, attributes)
for provider in six.itervalues(cls.providers): for provider in six.itervalues(cls.providers):
_check_provider_type(cls, provider) _check_provider_type(cls, provider)
return cls return cls
def __setattr__(cls, name, value): def __setattr__(cls, str name, object value):
"""Set class attribute. """Set class attribute.
If value of attribute is provider, it will be added into providers If value of attribute is provider, it will be added into providers
@ -181,7 +185,7 @@ class DeclarativeContainerMetaClass(type):
cls.cls_providers[name] = value cls.cls_providers[name] = value
super(DeclarativeContainerMetaClass, cls).__setattr__(name, value) super(DeclarativeContainerMetaClass, cls).__setattr__(name, value)
def __delattr__(cls, name): def __delattr__(cls, str name):
"""Delete class attribute. """Delete class attribute.
If value of attribute is provider, it will be deleted from providers If value of attribute is provider, it will be deleted from providers
@ -264,7 +268,7 @@ class DeclarativeContainer(object):
return container return container
@classmethod @classmethod
def override(cls, overriding): def override(cls, object overriding):
"""Override current container by overriding container. """Override current container by overriding container.
:param overriding: Overriding container. :param overriding: Overriding container.
@ -323,7 +327,7 @@ def override(object container):
:return: Declarative container's overriding decorator. :return: Declarative container's overriding decorator.
:rtype: callable(:py:class:`DeclarativeContainer`) :rtype: callable(:py:class:`DeclarativeContainer`)
""" """
def _decorator(overriding_container): def _decorator(object overriding_container):
"""Overriding decorator.""" """Overriding decorator."""
container.override(overriding_container) container.override(overriding_container)
return overriding_container return overriding_container
@ -344,7 +348,7 @@ def copy(object container):
:rtype: callable(:py:class:`DeclarativeContainer`) :rtype: callable(:py:class:`DeclarativeContainer`)
""" """
def _decorator(copied_container): def _decorator(copied_container):
memo = dict() cdef dict memo = dict()
for name, provider in six.iteritems(copied_container.cls_providers): for name, provider in six.iteritems(copied_container.cls_providers):
try: try:
source_provider = getattr(container, name) source_provider = getattr(container, name)