Extract providers type checker into function

This commit is contained in:
Roman Mogilatov 2016-06-01 16:00:11 +03:00
parent 1c1596543d
commit be94a1badc

View File

@ -31,7 +31,7 @@ class DeclarativeContainerMetaClass(type):
cls = type.__new__(mcs, class_name, bases, attributes) cls = type.__new__(mcs, class_name, bases, attributes)
for provider in six.itervalues(cls.providers): for provider in six.itervalues(cls.providers):
cls._check_provider_type(provider) _check_provider_type(cls, provider)
return cls return cls
@ -42,7 +42,7 @@ class DeclarativeContainerMetaClass(type):
dictionary. dictionary.
""" """
if utils.is_provider(value): if utils.is_provider(value):
cls._check_provider_type(value) _check_provider_type(cls, value)
cls.providers[name] = value cls.providers[name] = value
cls.cls_providers[name] = value cls.cls_providers[name] = value
super(DeclarativeContainerMetaClass, cls).__setattr__(name, value) super(DeclarativeContainerMetaClass, cls).__setattr__(name, value)
@ -58,11 +58,6 @@ class DeclarativeContainerMetaClass(type):
del cls.cls_providers[name] del cls.cls_providers[name]
super(DeclarativeContainerMetaClass, cls).__delattr__(name) super(DeclarativeContainerMetaClass, cls).__delattr__(name)
def _check_provider_type(cls, provider):
if not isinstance(provider, cls.provider_type):
raise errors.Error('{0} can contain only {1} '
'instances'.format(cls, cls.provider_type))
@six.add_metaclass(DeclarativeContainerMetaClass) @six.add_metaclass(DeclarativeContainerMetaClass)
class DeclarativeContainer(object): class DeclarativeContainer(object):
@ -174,3 +169,9 @@ def copy(container):
return copied_container return copied_container
return _decorator return _decorator
def _check_provider_type(cls, provider):
if not isinstance(provider, cls.provider_type):
raise errors.Error('{0} can contain only {1} '
'instances'.format(cls, cls.provider_type))