Commit Graph
53 Commits
Author SHA1 Message Date
182aedaec4 Handle empty value objects properly (#481)
Co-authored-by: James Hilton-Balfe <gobot1234yt@gmail.com>
2023-06-24 20:19:13 +01:00
Nick DeRobertisandGitHub fcbd8a3759 Fix pydict serialization for optional fields (#495) 2023-05-28 17:47:52 +01:00
2b41383745 Fix dict encoding for timezone aware datetimes (#468)
Co-authored-by: James Hilton-Balfe <gobot1234yt@gmail.com>
2023-04-13 23:34:19 +01:00
Samuel YvonandGitHub 13d656587c Add support for pydantic dataclasses (#406) 2023-02-13 15:37:16 +00:00
Wouter HorréandGitHub 0adcc9020c Pythonize input_type name in py_input_message (#436)
Co-authored-by: konstantin <konstantin.klein@hochfrequenz.de>
Co-authored-by: James Hilton-Balfe <gobot1234yt@gmail.com>
Fixes https://github.com/danielgtaylor/python-betterproto/issues/427
Fixes https://github.com/danielgtaylor/python-betterproto/issues/438
2022-12-02 22:18:48 +00:00
sterliakovandGitHub bfc0fac754 Enforce serialize_empty for repeated fields (#417) 2022-08-31 18:59:12 +01:00
Samuel YvonandGitHub 591ec5efb3 Pull down the include_default_values argument to to_json (#405) 2022-08-08 14:26:28 +01:00
James Hilton-BalfeandGitHub 3fd5a0d662 Fix parameters missing from services (#381) 2022-07-06 19:05:40 +01:00
6536181902 Add to/from_pydict methods (#203)
* add to/from_pydict methods

* Remove unnecessary method call

* Fix formatting

Co-authored-by: James Hilton-Balfe <gobot1234yt@gmail.com>
2022-05-09 17:34:12 +01:00
85e4be96d8 fix Message.to_dict mutating the underlying Message (#378)
* [fix] to_dict modifies the underlying message (#151)

* add test for mapmessage

* fix for to_dict

* formatting

* Apply suggestions from code review

Co-authored-by: Arun Babu Neelicattu <arun.neelicattu@gmail.com>

* change to_json to to_dict

Co-authored-by: Arun Babu Neelicattu <arun.neelicattu@gmail.com>
2022-05-09 17:29:42 +01:00
efokschanerandGitHub 06c26ba60d tests: Lazy evaluate Deadline parameter in pytest (#380)
The pytest parameters are evaluated when the tests are loading.
The Deadline.from_timeout is a fixed point in time. 
By deferring the evaluation it helps ensure that the deadline is not reached before the test is executed.
2022-04-30 23:11:21 +01:00
James Hilton-BalfeandGitHub 3ca092a724 Fix is_set for optional proto3 fields 2022-04-24 01:37:15 +02:00
James Hilton-BalfeandGitHub ac96d8254b Add a minimal repro for #312 (#340) 2022-04-22 11:04:40 +01:00
MaxandGitHub e7133adeb3 fix: map field edge-case
This change ensures a parent is a nested type when checking if a field is a map.
2022-04-22 11:06:44 +02:00
Gabriel PajotandGitHub 5f7e4d58ef Fix documentation for nested enums (#351) 2022-03-18 22:36:27 +00:00
Arun Babu NeelicattuandGitHub 70310c9e8c pre-commit: add isort hook and apply (#354) 2022-03-17 00:01:17 +00:00
Arun Babu NeelicattuandGitHub 18a518efa7 Expose timeout, deadline and metadata parameters from grpclib (#352) 2022-03-13 22:34:11 +00:00
Arun Babu NeelicattuandGitHub 62da35b3ea parser: ensure prefix is separated when traversing (#353) 2022-03-12 09:08:03 +00:00
Arun Babu NeelicattuandGitHub 69f4192341 Fix incorrect deprecation warnings on defaults (#348)
This change ensures that deprecation warnings are only raised when
either a deprecated field is explicitly set or a deprecated message is
initialised.

Resolves: #347
2022-03-11 23:36:14 +00:00
Arun Babu NeelicattuandGitHub 9c1bf25304 tests.generate: stop using asyncio.get_event_loop (#349)
The use of `asyncio.get_event_loop()` has been deprecated in python 3.10+. We replace this usage with `asyncio.run()` for python 3.7+.
2022-03-03 18:11:57 +00:00
Arun Babu NeelicattuandGitHub bd69862a02 test input: use explicit package declaration (#345) 2022-03-03 13:34:53 +00:00
James Hilton-BalfeandGitHub 74205e3319 Implement __deepcopy__ for Message (#339) 2022-02-16 23:12:51 +00:00
Eitan MosenkisandGitHub 8c727d904f Fix from_dict() in the presence of optional datetime fields. (#329) 2022-02-03 09:00:56 +00:00
efokschanerandGitHub d260f071e0 Client and Service Stubs take 1 request parameter, not one for each field (#311) 2022-01-17 19:58:57 +01:00
d77f44ebb7 Support proto3 field presence (#281)
* Update protobuf pregenerated files

* Update grpcio-tools to latest version

* Implement proto3 field presence

* Fix to_dict with None optional fields.

* Add test with optional enum

* Properly support optional enums

* Add tests for 64-bit ints and floats

* Support field presence for int64 types

* Fix oneof serialization with proto3 field presence (#292)

= Description

The serialization of a oneof message that contains a message with fields
with explicit presence was buggy.

For example:

```
message A {
    oneof kind {
        B b = 1;
        C c = 2;
    }
}

message B {}
message C {
    optional bool z = 1;
}
```

Serializing `A(b=B())` would lead to this payload:

```
0A # tag1, length delimited
00 # length: 0
12 # tag2, length delimited
00 # length: 0
```

Which when deserialized, leads to the message `A(c=C())`.

= Explanation

The issue lies in the post_init method. All fields are introspected, and
if different from PLACEHOLDER, the message is marked as having been
"serialized_on_wire".
Then, when serializing `A(b=B())`, we go through each field of the
oneof:

- field 'b': this is the selected field from the group, so it is
  serialized
- field 'c': marked as 'serialized_on_wire', so it is added as well.

= Fix

The issue is that support for explicit presence changed the default
value from PLACEHOLDER to None. This breaks the post_init method in that
case, which is relatively easy to fix: if a field is optional, and set
to None, this is considered as the default value (which it is).

This fix however has a side-effect: the group_current for this field (the
oneof trick for explicit presence) is no longer set. This changes the
behavior when serializing the message in JSON: as the value is the
default one (None), and the group is not set (which would force the
serialization of the field), so None fields are no longer serialized in
JSON. This break one test, and will be fixed in the next commit.

* fix: do not serialize None fields in JSON format

This is linked to the fix from the previous commit: after it, scalar
None fields were not included in the JSON format, but some were still
included.

This is all cleaned up: None fields are not added in JSON by default,
as they indicate the default value of fields with explicit presence.
However, if `include_default_values is set, they are included.

* Fix: use builtin annotation prefix

* Remove comment

Co-authored-by: roblabla <unfiltered@roblab.la>
Co-authored-by: Vincent Thiberville <vthib@pm.me>
2021-12-29 13:38:32 -08:00
Kim GustyrandGitHub bc3cfc5562 Fix default values for enum service args #298 (#299) 2021-12-03 21:26:48 +00:00
b0a36d12e4 Fix compilation of fields with name identical to their type (#294)
* Revert "Fix compilation of fields named 'bytes' or 'str' (#226)"

This reverts commit deb623ed14.

* Fix compilation of fileds with name identical to their type

* Added test for field-name identical to python type

Co-authored-by: Guy Szweigman <guysz@nvidia.com>
2021-12-01 16:31:02 +00:00
a4d2d39546 Fix Python 3.9 Tests (#284)
Co-authored-by: James Hilton-Balfe <50501825+Gobot1234@users.noreply.github.com>
2021-11-19 21:32:36 +00:00
natandGitHub 7368299a70 Fix serialization of repeated fields with empty messages (#180)
Extend test config and utils to support exclusion of certain json samples from
testing for symetry.
2021-04-06 10:50:45 +10:00
deb623ed14 Fix compilation of fields named 'bytes' or 'str' (#226)
* if you have a field named "bytes" using the bytes type, it doesn't work.
* Enable existing use-case & generalize solution to cover it

Co-authored-by: Spencer <spencer@sf-n.com>
2021-04-06 10:45:57 +10:00
natandGitHub 95339bf74d Misc cleanup, see commit body (#227)
- Enable oneof_enum test case that passes now (removed the xfail)
- Switch from toml to tomlkit as a dev dep for better toml support
- upgrade poethepoet to latest stable release
- use full table format for poe tasks to avoid long lines in pyproject.toml
- remove redundant _WrappedMessage class
- fix various Mypy warnings
- reformat some comments for consistent line length
2021-04-06 10:43:09 +10:00
Matthew BadgerandGitHub 7c5ee47e68 Added support for infinite and nan floats/doubles (#215)
- Added support for the custom double values from
   the protobuf json spec: "Infinity", "-Infinity", and "NaN"
- Added `infinite_floats` test data
- Updated Message.__eq__ to consider nan values
   equal
- Updated `test_message_json` and
   `test_binary_compatibility` to replace NaN float
   values in dictionaries before comparison
   (because two NaN values are not equal)
2021-04-02 15:15:28 +02:00
Nat NoordanusandBasileus 2a73dbac98 Make plugin use betterproto generated classes internally
This means the betterproto plugin no longer needs to depend durectly on
protobuf.

This requires a small runtime hack to monkey patch some google types to
get around the fact that the compiler uses proto2, but betterproto
expects proto3.

Also:
- regenerate google.protobuf package
- fix a regex bug in the logic for determining whether to use a google
  wrapper type.
- fix a bug causing comments to get mixed up when multiple proto files
  generate code into a single python module
2021-04-02 10:13:00 +11:00
Nat NoordanusandBasileus a890514b5c Update deps & add generate_lib task
- Remove plugin dependency on protobuf since it's no longer required.
- Update poethepoet to for better pyproject toml syntax support
- Add handy generate_lib poe task for maintaining generated libs
2021-04-01 09:49:22 +11:00
Nat NoordanusandBasileus fe1e712fdb Make plugin use betterproto generated classes internally
This means the betterproto plugin no longer needs to depend durectly on
protobuf.

This requires a small runtime hack to monkey patch some google types to
get around the fact that the compiler uses proto2, but betterproto
expects proto3.

Also:
- regenerate google.protobuf package
- fix a regex bug in the logic for determining whether to use a google
  wrapper type.
- fix a bug causing comments to get mixed up when multiple proto files
  generate code into a single python module
2021-04-01 09:49:22 +11:00
Vasili Syrakis 7a358a63cf Add __version__ attribute to package 2021-03-31 11:44:32 +11:00
342e6559dc Properly serialize zero-value messages in a oneof group (#176)
Also improve test utils to make it easier to have multiple json examples.

Co-authored-by: Christopher Chambers <chris@peanutcode.com>
2021-03-15 13:52:35 +01:00
Vladimir SolomatinandGitHub 2f62189346 Fix typing and datetime imports not being present for service method type annotations (#183) 2021-03-12 22:15:15 +01:00
MinJune KimandGitHub 8a215367ad Allow empty services (#222)
Fixes issue #220
2021-03-12 21:49:58 +01:00
robinalyandGitHub 6c1c41e9cc Use dateutil parser (#213)
Switch to using `isoparse` from `dateutil.parser` instead of `datetime.fromisoformat` for more robust parsing of dates in from_dict.
2021-02-24 22:18:05 +01:00
Matthew BadgerandGitHub 9e6881999e Add support for repeated timestamps and durations to to_dict from_dict (#211) 2021-02-16 19:54:50 +01:00
Tim SchmidtandGitHub 1d54ef8f99 Generate grpclib service stubs (#170) 2020-12-04 22:22:11 +01:00
69dfe9cafc Implement Message.__bool__ (#142)
* Implement Message.__bool__ with similar semantics to a collection, such that any value being set on the message (i.e. having a non-default value) make the Message value truthy .

Co-authored-by: nat <n@natn.me>
2020-11-24 19:35:09 +01:00
JamesandGitHub 8f7af272cc QOL fixes (#141)
- Add missing type annotations
- Various style improvements
- Use constants more consistently
- enforce black on benchmark code
2020-10-17 19:27:11 +02:00
034e2e7da0 Add support for recursive messages (#130)
Changes message initialization (`__post_init__`) so that default values
are no longer eagerly created to prevent infinite recursion when
initializing recursive messages.

As a result, `PLACEHOLDER` will be present in the message for any
uninitialized fields.  So, an implementation of `__get_attribute__` is
added that checks for `PLACEHOLDER` and lazily creates and stores
default field values.

And, because `PLACEHOLDER` values don't compare equal with zero values,
a custom implementation of `__eq__` is provided, and the code generation
template is updated so that messages generate with `@dataclass(eq=False)`.

Also add new Message __repr__ implementation that skips PLACEHOLDER 
values and orders keys by number from the proto.

Co-authored-by: Christopher Chambers <chris@peanutcode.com>
Co-authored-by: nat <n@natn.me>
Co-authored-by: James <50501825+Gobot1234@users.noreply.github.com>
2020-08-30 21:04:36 +02:00
JamesandGitHub 16d554db75 Update black 2020-08-29 17:15:59 +02:00
Arun Babu NeelicattuandGitHub 43c134d27c ci: refactor jobs and improve platform coverage (#128) 2020-07-30 14:47:38 +02:00
Arun Babu NeelicattuandGitHub 0cd9510b54 Support deprecated message and fields (#126) 2020-07-30 14:47:01 +02:00
Arun Babu NeelicattuandGitHub 3d8c0cb713 grpclib_client: handle trailer-only responses (#127)
Resolves: #123
2020-07-25 19:57:46 +02:00
Brady KiefferandGitHub c1a76a5f5e Serialize default values in oneofs when calling to_dict() or to_json() (#110)
* Serialize default values in oneofs when calling to_dict() or to_json()

This change is consistent with the official protobuf implementation. If
a default value is set when using a oneof, and then a message is
translated from message -> JSON -> message, the default value is kept in
tact. Also, if no default value is set, they remain null.

* Some cleanup + testing for nested messages with oneofs

* Cleanup oneof_enum test cases, they should be fixed

This _should_ address:
https://github.com/danielgtaylor/python-betterproto/issues/63

* Include default value oneof fields when serializing to bytes

This will cause oneof fields with default values to explicitly be sent
to clients. Note that does not mean that all fields are serialized and
sent to clients, just those that _could_ be null and are not.

* Remove assignment when populating a sub-message within a proto

Also, move setattr out one indentation level

* Properly transform proto with empty string in oneof to bytes

Also, updated tests to ensure that which_one_of picks up the set field

* Formatting betterproto/__init__.py

* Adding test cases demonstrating equivalent behaviour with google impl

* Removing a temporary file I made locally

* Adding some clarifying comments

* Fixing tests for python38
2020-07-25 19:51:40 +02:00