-
Notifications
You must be signed in to change notification settings - Fork 309
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: use proto-plus instead of _pb #332
Conversation
I've filed googleapis/proto-plus-python#153 and googleapis/proto-plus-python#152 which will allow us to ignore unknown fields and avoid the use of the |
this._proto = json_format.ParseDict( | ||
resource, types.Model()._pb, ignore_unknown_fields=True | ||
) | ||
this._proto = types.Model.from_json(json.dumps(resource)) |
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.
Can we pass resource
to the Model
constructor here?
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.
We'll still want googleapis/proto-plus-python#153 to support ignore_unknown_fields
, though.
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 already had tried that during my testing. It gives a KeyError on the metaclass during __init__
, right here:
https://github.com/googleapis/proto-plus-python/blob/master/proto/message.py#L425
I was using this code:
this._proto = types.Model(resource)
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.
We should be able to update this to types.Model(resource, ignore_unknown_fields=True)
now.
googleapis/proto-plus-python#154 adds |
They already released. Still getting some test errors here, but I think they are related to assumptions about how the pb json_format conversion worked. Hopefully will have a working PR at some point tonight. |
@tswast Speaking about hidden json_format pb conversion behavior: somewhere along the line it converted key names to camelCase automatically, so many checks we have on resources fail due to the ids now using snake_case and the manually constructed test resources using camelCase. In some cases, the camelCase names are part of the main code, like in job and client modules. Dov said this has been a problem elsewhere. I'm still not familiar enough with the bigquery codebase to be completely sure which way to go here: my instinct is to convert everything to snake_case, what do you think? |
@cguardia You mean it's taking the
https://developers.google.com/protocol-buffers/docs/proto3#json Does the same behavior happen with |
Not using JSON anymore. The tests are expecting CamelCase, but the calls to the constructor and to_dict are returning snake_case. I was thinking of getting rid of camelCase everywhere, but unsure if that would affect something else. |
This seems like a safe operation to me. We aren't ever sending that JSON representation to the API, so it shouldn't even affect that. |
Well, this has been quite the rabbit hole. Turns out not only the tests, but some of the code as well, seem to be counting on the automatic case conversion, so that a lot of code would need to be changed to get rid of that. The
@tswast what do you think? |
I don't recall what code relies on this. Could you send a pointer to the line on GitHub? Are there really all that many tests for this? I recall that all of the properties we use |
I'm going to close this one, and open another with the current approach. |
This is a proof of concept PR to fix #319. This almost works, but there are four tests failing. The failures are of two types:
The
message.from_json
method, which is the likeliest successor to thejson_format.ParseDict
call used with the_pb
object, does not allow us to pass in the "ignore_unknown_fields" flag to the parser, thus causing errors in tests that try to ignore fields.The json format created by the regular json library has some slight differences with the format generated by
json_format
, which causes tests trying to comparerepr
output to fail.I don't like very much the use of the json library in this PR, and would prefer to avoid it if possible. I think perhaps making a PR to proto-plus to make the
to_json
andfrom_json
methods more flexible would be a good idea, along with possibly addingto_dict
andfrom_dict
methods there, which seems to me could be generally helpful.