-
Notifications
You must be signed in to change notification settings - Fork 211
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
Handle mutable default arguments cleanly #105
Conversation
Nice. Maybe worth having a test case for this (along the lines of https://github.com/danielgtaylor/python-betterproto/blob/master/betterproto/tests/grpc/test_grpclib_client.py#L33 )? I assume you've verified it manually? |
5b1911a
to
66dfc63
Compare
@nat-n I did test it manually. Although turns out I did not handle the edge case when |
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.
less complex logic in jinja makes me feel better.
Ah, sorry it took me so long, but i finally get it. So:
Correct? What I still don't understand is:
From what I see, the request object created on the fly, and then passed to grpcio for serialization and sending. In theory it should not ever be modified. |
@boukeversteegh that is correct. The change is to ensure generated code adheres to best practices. Since the project aims to generate clean pythonic code at best effort, I think this is a change in that direction. I am not across all possible scenarios, present and future, of how this code gets used to say for certain that the default value (initialised at definition) will never be mutated prior to serialisation. This is more a defense-in-depth change. |
0cf8aaa
to
06a626d
Compare
@boukeversteegh this should be ready for review again. The recent refactor in master makes the implementation here a bit cleaner. |
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.
Looks good!
When generating code, ensure that default list/dict arguments are initialised in local scope if unspecified or `None`.
@boukeversteegh @nat-n rebased and ready again; had to rebased due to improt statement collision. |
When generating code, ensure that default list/dict arguments are initialised in local scope if unspecified or
None
.The following diff shows the difference in the generated source file from
service.proto
.Due to the nature of the issue, the included test simply verifies that the default list instance initialised is not the same object used in any subsequent calls. This is done so by intercepting the calls made to the underlying
ServiceStub._unary_unary
method and verifying that thecomments
instance is not reusing the object.