[WIP] thrift: treat required fields as positional arguments while generating init, closes #119#132
[WIP] thrift: treat required fields as positional arguments while generating init, closes #119#132iamsudip wants to merge 3 commits intoThriftpy:masterfrom
Conversation
Codecov Report
@@ Coverage Diff @@
## master #132 +/- ##
==========================================
+ Coverage 83.09% 83.13% +0.03%
==========================================
Files 43 43
Lines 3911 3920 +9
==========================================
+ Hits 3250 3259 +9
Misses 661 661
Continue to review full report at Codecov.
|
thriftpy2/thrift.py
Outdated
| return __init__ | ||
|
|
||
|
|
||
| varnames, defaults = zip(*spec) |
There was a problem hiding this comment.
@ethe This function still returning the same function as before, just that init generated would have positional arguments now cause I didn't change defaults variable which is being passed to types.FunctionType while returning. What do you think?
There was a problem hiding this comment.
Unless we change defaults, I don't think this commit makes any difference.
base.thrift:
struct Hello {
1: optional string name,
2: required string greet
}
In [4]: ab = thriftpy2.load("tests/base.thrift")
In [6]: ab.Hello.__init__??
Signature: ab.Hello.__init__(self, greet=None, name=None)
Source:
def __init__(self, greet, name=None):
self.name = name
File: ~/Work/Personal/thriftpy2/<generated Hello.__init__>
Type: instancemethod
In [9]: ab.Hello()
Out[9]: Hello(name=None, greet=None)
This should raise error, right?
There was a problem hiding this comment.
Yes, I think raising error here should be better, maybe we can remove required arguments from defaults.
There was a problem hiding this comment.
Removing required arguments from defaults breaking lots of tests. Look at new change and breaking test cases. I guess we can keep the signature intact and add ValueError statements inside init if args are None and if required.
| args.append(spec_element[0]) | ||
| else: | ||
| kwargs.append(spec_element) | ||
| defaults.append(spec_element[1]) |
There was a problem hiding this comment.
This is breaking lot's of valid tests
There was a problem hiding this comment.
I checked that it is all crashed in Client._send function, I think it is easy to be fixed.
There was a problem hiding this comment.
@ethe Can you tell me how can I achieve that?
I tried changing args_to_kwargs to not make args to kwargs, passed them to _send, but later failed at TProcessor:process_in:args = getattr(self._service, api + "_args")() because of required args and we removed them from defaults.
There was a problem hiding this comment.
Or we can add a fixture to be added to generated init for raising ValueError if required args don't have value.
There was a problem hiding this comment.
Compare with raising ValueError in runtime, I think it is better to use args because of a friendly function signature. I will fetch your branch and maybe research for a while. Thank you!
There was a problem hiding this comment.
@ethe Sure. I also think the same that using args is the way to go, I faced blocker here: Processor dependency on generated service api. Would love to learn from your implementation.
No description provided.