Add provider type checks for declarative container

This commit is contained in:
Roman Mogilatov 2016-05-29 22:05:43 +03:00
parent ed7f354afe
commit d8f03c4487

View File

@ -26,7 +26,18 @@ class DeclarativeContainerMetaClass(type):
attributes['inherited_providers'] = dict(inherited_providers)
attributes['providers'] = dict(cls_providers + inherited_providers)
return type.__new__(mcs, class_name, bases, attributes)
cls = type.__new__(mcs, class_name, bases, attributes)
if cls.provider_type:
for provider in six.itervalues(cls.providers):
try:
assert isinstance(provider, cls.provider_type)
except AssertionError:
raise errors.Error('{0} can contain only {1} '
'instances'.format(cls,
cls.provider_type))
return cls
def __setattr__(cls, name, value):
"""Set class attribute.
@ -57,6 +68,8 @@ class DeclarativeContainer(object):
__IS_CATALOG__ = True
provider_type = None
providers = dict()
cls_providers = dict()
inherited_providers = dict()