Commit Graph
90 Commits
Author SHA1 Message Date
5666393f9d betterproto: support Struct and Value (#551)
* betterproto: support `Struct` and `Value`

Signed-off-by: William Woodruff <william@trailofbits.com>

* betterproto: handle struct in to_dict as well

Signed-off-by: William Woodruff <william@trailofbits.com>

* tests: add Struct roundtrip tests

Signed-off-by: William Woodruff <william@trailofbits.com>

* specialize from_dict and to_dict on Struct

...rather than special-casing in the Message ABC.

Signed-off-by: William Woodruff <william@trailofbits.com>

* betterproto: `poe format`

Signed-off-by: William Woodruff <william@trailofbits.com>

* Update src/betterproto/__init__.py

Co-authored-by: James Hilton-Balfe <gobot1234yt@gmail.com>

* remove future annotations

Signed-off-by: William Woodruff <william@trailofbits.com>

* replace type[...] with typing.T

Signed-off-by: William Woodruff <william@trailofbits.com>

* quote instead

Signed-off-by: William Woodruff <william@trailofbits.com>

---------

Signed-off-by: William Woodruff <william@trailofbits.com>
Co-authored-by: James Hilton-Balfe <gobot1234yt@gmail.com>
2024-01-02 20:16:15 +00:00
John CollinsandGitHub b8a091ae70 Turn off doc-string line wrapping (#532) 2023-12-16 13:36:53 +00:00
Erik FrieseandGitHub d34b16993d Use external package betterproto-rust-codec for better (de-)serialization performance (#545)
* optionally use betterproto-rust-codec
* monkey patch `parse` and `__bytes__`
2023-12-07 11:21:29 +11:00
bd7de203e1 Remove Enum prefixes (#187)
Co-authored-by: nat <n@natn.me>
Co-authored-by: Tim Schmidt <w4rum@users.noreply.github.com>
Co-authored-by: Arun Babu Neelicattu <arun.neelicattu@gmail.com>
2023-10-25 22:35:16 +01:00
d9b7608980 Support Message.from_dict() as a class and an instance method (#476)
* Make Message.from_dict() a class method

Signed-off-by: Marek Pikuła <marek.pikula@embevity.com>

* Sync 1/2 of review comments

* Sync other half

* Update .pre-commit-config.yaml

* Update __init__.py

* Update utils.py

* Update src/betterproto/__init__.py

* Update .pre-commit-config.yaml

* Update __init__.py

* Update utils.py

* Fix CI again

* Fix failing formatting

---------

Signed-off-by: Marek Pikuła <marek.pikula@embevity.com>
Co-authored-by: James Hilton-Balfe <gobot1234yt@gmail.com>
2023-10-25 22:20:23 +01:00
02aa4e88b7 Fix _Timestamp edge cases (#534)
* Add failing test cases for timestamp conversion

* Fix timestamp to datetime conversion

* Fix formatting

* Move timestamp tests outside of inputs folder

---------

Co-authored-by: Lukas Bindreiter <lukas.bindreiter@tilebox.io>
Co-authored-by: James Hilton-Balfe <gobot1234yt@gmail.com>
2023-10-18 00:41:09 +01:00
Sebastian PietrasandGitHub e309513131 Fix _stream_unary not working with empty iterables (#422)
* Fixed `do_many_things` method in `ThingService`
* Added test for empty iterables
* Fixed `_stream_unary` not sending request first
2023-10-18 09:54:23 +11:00
VasiliosandGitHub ca6b9fe1a2 Implementing pickle methods (#535)
Continuing work from #484 by @kevinaud to fix #419

* Implementing pickle methods
* Implement __copy__, __reduce__, and fix __setstate__
* Moved pickling tests into their own file
* Add test for caching
2023-10-16 22:17:15 +11:00
Joshua LeiversandGitHub 4f18ed1325 Add support for streaming delimited messages (#529)
* Add support for streaming delimited messages

This allows developers to easily dump and load multiple messages
from a stream in a way that is compatible with official
protobuf implementations (such as Java's
`MessageLite#writeDelimitedTo(...)`).

* Add Java compatibility tests for streaming

These tests stream data such as messages to output files, have a
Java binary read them and then write them back using the
`protobuf-java` functions, and then read them back in on the Python
side to check that the returned data is as expected. This checks
that the official Java implementation (and so any other matching
implementations) can properly parse outputs from Betterproto, and
vice-versa, ensuring compatibility in these functions between the
two.

* Replace `xxxxableBuffer` with `SupportsXxxx`
2023-10-16 21:59:33 +11:00
Erik FrieseandGitHub 8b5dd6c1f8 Dont set group for optional fields (#528)
Fixes #523
2023-10-16 13:43:42 +11:00
Micael JarniacandGitHub 3514991133 Rich support (#508)
Closes #507

https://rich.readthedocs.io/en/latest/pretty.html#rich-repr-protocol
2023-10-16 13:36:26 +11:00
James Hilton-BalfeandGitHub e3b44f491f Fix timestamp parsing (#415)
Fixes #407
2023-10-16 13:35:32 +11:00
c82816b8be Map enum int's into Enums redux (#293)
Re-implement Enum to be faster along with being an open set

---------
Co-authored-by: ydylla <ydylla@gmail.com>
2023-10-16 13:32:30 +11:00
Joshua LeiversandGitHub 8659c51123 Add message streaming support (#518) 2023-08-29 14:26:25 +01:00
AndrewandGitHub 4cdf1bb9e0 Fix Message equality comparison (#513) 2023-07-29 12:06:56 +01:00
Alexander KhabarovandGitHub 6faac1d1ca Raise AttributeError on attempts to access unset oneof fields (#510) 2023-07-21 13:26:30 +01:00
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
pi-slhandGitHub aad7d2ad76 Replace pkg_resources with importlib (#462) 2023-05-25 11:12:15 +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
James Hilton-BalfeandGitHub e7f07fa2a1 Update __init__.py (#451) 2023-03-08 08:20:56 +00:00
Samuel YvonandGitHub 2fa0be2141 Fix for #459 (pydantic code gen only) (#460) 2023-02-21 19:41:32 +00:00
Samuel YvonandGitHub 13d656587c Add support for pydantic dataclasses (#406) 2023-02-13 15:37:16 +00:00
James Hilton-BalfeandGitHub 1b1bd47cb1 Drop support for python3.6 (#444) 2023-02-09 08:35:41 +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
Antonín ŘíhaandGitHub 8fbf4476a8 Fix typechecker compatiblity checks in server streaming methods (#413) 2022-08-31 00:05:29 +01:00
Samuel YvonandGitHub 591ec5efb3 Pull down the include_default_values argument to to_json (#405) 2022-08-08 14:26:28 +01:00
Antonín ŘíhaandGitHub f31d51cf3c Added support for @generated marker (#382) 2022-08-03 11:05:13 +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
James Hilton-BalfeandGitHub 6a70b8e8ea compiler: do not overwrite top level __init__.py
Resolves: #168 #260
2022-04-24 01:44:16 +02:00
James Hilton-BalfeandGitHub 3ca092a724 Fix is_set for optional proto3 fields 2022-04-24 01:37:15 +02:00
James Hilton-BalfeandGitHub 6f7d706a8e fix float nan comparison
This change also adds minor docstrings fixes and bumps pre-commit blac
version.
2022-04-24 01:13:58 +02: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
James Hilton-BalfeandGitHub b9b0b22d57 Make Message.__getattribute__ invisible to type checkers (#359)
This lets linters know that we shouldn't access fields that aren't actually defined
2022-04-21 15:44:55 +01:00
Gabriel PajotandGitHub 5f7e4d58ef Fix documentation for nested enums (#351) 2022-03-18 22:36:27 +00:00
Arun Babu NeelicattuandGitHub 1aaf7728cc compiler: Run isort on compiled code (#355) 2022-03-18 22:29:42 +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
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
Michael OsthegeandGitHub 9b5594adbe Format field comments also as docstrings (#304)
Closes #303

* Format field comments also as docstrings
To make it clear that they refer to the item above.
* Fix placement of enum item docstrings
* Add line breaks after class attribute or enum item docstrings
2022-01-27 09:25:48 +11:00
Danil AkhtarovandGitHub d991040ff6 Fix message text in NotImplementedError (#325) 2022-01-21 11:39:09 +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
573c7292a6 Add Python 3.10 to GitHub Actions test matrix (#280)
Co-authored-by: James Hilton-Balfe <50501825+Gobot1234@users.noreply.github.com>
2021-12-29 23:10:34 +00: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