diff --git a/tests/test_http.py b/tests/test_http.py index f0e39310..2c3c8b2d 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -13,7 +13,7 @@ thriftpy2.install_import_hook() # noqa from thriftpy2.http import make_server, make_client, client_context # noqa -from thriftpy2.thrift import TApplicationException +from thriftpy2.thrift import TApplicationException # noqa addressbook = thriftpy2.load(os.path.join(os.path.dirname(__file__), diff --git a/thriftpy2/thrift.py b/thriftpy2/thrift.py index 381595a1..92cbb8fc 100644 --- a/thriftpy2/thrift.py +++ b/thriftpy2/thrift.py @@ -68,9 +68,26 @@ def __init__(self): pass return __init__ - varnames, defaults = zip(*spec) + if hasattr(cls, 'thrift_spec'): + args = [] + kwargs = [] + varnames = [] + defaults = [] + for spec_element, t_spec in zip(spec, cls.thrift_spec.values()): + varnames.append(spec_element[0]) + if t_spec[-1]: + args.append(spec_element[0]) + else: + kwargs.append(spec_element) + defaults.append(spec_element[1]) + defaults = tuple(defaults) if defaults else None + + args.extend(map('{0[0]}={0[1]!r}'.format, kwargs)) + args = ', '.join(args) + else: + varnames, defaults = zip(*spec) + args = ', '.join(map('{0[0]}={0[1]!r}'.format, spec)) - args = ', '.join(map('{0[0]}={0[1]!r}'.format, spec)) init = "def __init__(self, {}):\n".format(args) init += "\n".join(map(' self.{0} = {0}'.format, varnames))