From 094b747f8ea8c82cef5f886f287ec8d6b60904d8 Mon Sep 17 00:00:00 2001 From: iamsudip Date: Fri, 29 May 2020 02:57:20 +0530 Subject: [PATCH] thrift: treat required fields as positional arguments while generating init, closes #119 --- thriftpy2/thrift.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/thriftpy2/thrift.py b/thriftpy2/thrift.py index 381595a1..a409e56c 100644 --- a/thriftpy2/thrift.py +++ b/thriftpy2/thrift.py @@ -68,9 +68,22 @@ def __init__(self): pass return __init__ + varnames, defaults = zip(*spec) + if hasattr(cls, 'thrift_spec'): + args = [] + kwargs = [] + for spec_element, t_spec in zip(spec, cls.thrift_spec.values()): + if t_spec[-1]: + args.append(spec_element[0]) + else: + kwargs.append(spec_element) + + args.extend(map('{0[0]}={0[1]!r}'.format, kwargs)) + args = ', '.join(args) + else: + 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))