Send typed wire values matching Python value's runtime type#142
Merged
Conversation
set()/set_many() always wrapped the value in TypedValue(string_value=...),
but the server requires the populated oneof variant to match the field's
declared schema type exactly — so writing to any non-string field raised
InvalidArgumentError ("expected bool value", etc).
make_typed_value() now picks the wire variant from value's Python runtime
type (bool, int, float, datetime, timedelta, dict/list, str), mirroring
convert_value() on the read side — no schema lookup or extra round trip
needed. URL becomes a real str subtype so url-typed fields (which also use
a string wire representation, but a distinct oneof variant) can be targeted
by passing URL(...) instead of a plain str.
Closes #139
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
set()/set_many()always sentTypedValue(string_value=...), but the server requires the populated oneof variant to match the field's declared schema type exactly — every non-stringfield (bool,integer,number,time,duration,url,json) raisedInvalidArgumentError.make_typed_value()now picks the wire variant from the value's Python runtime type —bool/int/float/datetime/timedelta/dict/list/strmap tobool_value/integer_value/number_value/time_value/duration_value/json_value/string_valuerespectively — mirroringconvert_value()on the read side. No schema lookup or extra round trip needed.URLbecomes a realstrsubtype (was a plain alias) sourl-typed fields, which also use a string wire representation but a distinct oneof variant, can be targeted by passingURL(...).Test plan
test_stubs.pycovering everymake_typed_valuebranch (string, URL, bool-before-int ordering, integer, number, time, duration, json dict/list, unsupported type) — 100% coverage on_stubs.pytest_url_is_str_subtypeconfirmingURLstays read-compatible withstrwhile being distinguishable for writesmake lint/make typecheck/make testall pass (100% coverage)Closes #139