Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

iamsudip
Copy link
Contributor

No description provided.

@codecov
Copy link

codecov bot commented May 28, 2020

Codecov Report

Merging #132 into master will increase coverage by 0.03%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            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              
Impacted Files Coverage Δ
thriftpy2/thrift.py 89.23% <100.00%> (+0.34%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b981e8c...5617a55. Read the comment docs.

@@ -68,9 +68,22 @@ def __init__(self):
pass
return __init__


varnames, defaults = zip(*spec)
Copy link
Contributor Author

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?

Copy link
Contributor Author

@iamsudip iamsudip May 29, 2020

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?

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ethe Look at this commit: 5617a55

thriftpy2/thrift.py Outdated Show resolved Hide resolved
args.append(spec_element[0])
else:
kwargs.append(spec_element)
defaults.append(spec_element[1])
Copy link
Contributor Author

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

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Member

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!

Copy link
Contributor Author

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.

@iamsudip iamsudip changed the title thrift: treat required fields as positional arguments while generating init, closes #119 [WIP] thrift: treat required fields as positional arguments while generating init, closes #119 Sep 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants