-
Notifications
You must be signed in to change notification settings - Fork 92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] thrift: treat required fields as positional arguments while generating init, closes #119 #132
base: master
Are you sure you want to change the base?
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
@@ -68,9 +68,22 @@ def __init__(self): | |||
pass | |||
return __init__ | |||
|
|||
|
|||
varnames, defaults = zip(*spec) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think raising error here should be better, maybe we can remove required arguments from defaults.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
args.append(spec_element[0]) | ||
else: | ||
kwargs.append(spec_element) | ||
defaults.append(spec_element[1]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is breaking lot's of valid tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.