The Base type for complex types now has a constructor that takes attribute values as kwargs

This commit is contained in:
Christophe de Vienne
2012-08-18 00:23:24 +02:00
parent 26c3ec6523
commit 0439f96bf5
2 changed files with 14 additions and 1 deletions

View File

@@ -311,3 +311,10 @@ class TestTypes(unittest.TestCase):
assert B1.b2.datatype is B2, repr(B1.b2.datatype)
assert B2.b2.datatype is B2
def test_base_init(self):
class C1(types.Base):
s = six.text_type
c = C1(s=six.u('test'))
assert c.s == six.u('test')

View File

@@ -490,4 +490,10 @@ class BaseMeta(type):
if bases[0] is not object:
cls.__registry__.register(cls)
Base = BaseMeta('Base', (object, ), {})
def Base__init__(self, **kw):
for key, value in kw.items():
if hasattr(self, key):
setattr(self, key, value)
Base = BaseMeta('Base', (object, ), {'__init__': Base__init__})