* 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>
126 lines
2.9 KiB
TOML
126 lines
2.9 KiB
TOML
[tool.poetry]
|
|
name = "betterproto"
|
|
version = "2.0.0b3"
|
|
description = "A better Protobuf / gRPC generator & library"
|
|
authors = ["Daniel G. Taylor <danielgtaylor@gmail.com>"]
|
|
readme = "README.md"
|
|
repository = "https://github.com/danielgtaylor/python-betterproto"
|
|
keywords = ["protobuf", "gRPC"]
|
|
license = "MIT"
|
|
packages = [
|
|
{ include = "betterproto", from = "src" }
|
|
]
|
|
|
|
[tool.poetry.dependencies]
|
|
python = ">=3.6.2,<4.0"
|
|
black = { version = ">=19.3b0", optional = true }
|
|
dataclasses = { version = "^0.7", python = ">=3.6, <3.7" }
|
|
grpclib = "^0.4.1"
|
|
jinja2 = { version = "^2.11.2", optional = true }
|
|
python-dateutil = "^2.8"
|
|
|
|
[tool.poetry.dev-dependencies]
|
|
asv = "^0.4.2"
|
|
black = "^21.11b0"
|
|
bpython = "^0.19"
|
|
grpcio-tools = "^1.40.0"
|
|
jinja2 = "^2.11.2"
|
|
mypy = "^0.770"
|
|
poethepoet = ">=0.9.0"
|
|
protobuf = "^3.12.2"
|
|
pytest = "^5.4.2"
|
|
pytest-asyncio = "^0.12.0"
|
|
pytest-cov = "^2.9.0"
|
|
pytest-mock = "^3.1.1"
|
|
sphinx = "3.1.2"
|
|
sphinx-rtd-theme = "0.5.0"
|
|
tomlkit = "^0.7.0"
|
|
tox = "^3.15.1"
|
|
|
|
|
|
[tool.poetry.scripts]
|
|
protoc-gen-python_betterproto = "betterproto.plugin:main"
|
|
|
|
[tool.poetry.extras]
|
|
compiler = ["black", "jinja2"]
|
|
|
|
|
|
# Dev workflow tasks
|
|
|
|
[tool.poe.tasks.generate]
|
|
script = "tests.generate:main"
|
|
help = "Generate test cases (do this once before running test)"
|
|
|
|
[tool.poe.tasks.test]
|
|
cmd = "pytest"
|
|
help = "Run tests"
|
|
|
|
[tool.poe.tasks.types]
|
|
cmd = "mypy src --ignore-missing-imports"
|
|
help = "Check types with mypy"
|
|
|
|
[tool.poe.tasks.format]
|
|
cmd = "black . --exclude tests/output_"
|
|
help = "Apply black formatting to source code"
|
|
|
|
[tool.poe.tasks.docs]
|
|
cmd = "sphinx-build docs docs/build"
|
|
help = "Build the sphinx docs"
|
|
|
|
[tool.poe.tasks.bench]
|
|
shell = "asv run master^! && asv run HEAD^! && asv compare master HEAD"
|
|
help = "Benchmark current commit vs. master branch"
|
|
|
|
[tool.poe.tasks.clean]
|
|
cmd = """
|
|
rm -rf .asv .coverage .mypy_cache .pytest_cache
|
|
dist betterproto.egg-info **/__pycache__
|
|
testsoutput_*
|
|
"""
|
|
help = "Clean out generated files from the workspace"
|
|
|
|
[tool.poe.tasks.generate_lib]
|
|
cmd = """
|
|
protoc
|
|
--plugin=protoc-gen-custom=src/betterproto/plugin/main.py
|
|
--custom_opt=INCLUDE_GOOGLE
|
|
--custom_out=src/betterproto/lib
|
|
-I /usr/local/include/
|
|
/usr/local/include/google/protobuf/**/*.proto
|
|
"""
|
|
help = "Regenerate the types in betterproto.lib.google"
|
|
|
|
# CI tasks
|
|
|
|
[tool.poe.tasks.full-test]
|
|
shell = "poe generate && tox"
|
|
help = "Run tests with multiple pythons"
|
|
|
|
[tool.poe.tasks.check-style]
|
|
cmd = "black . --check --diff --exclude tests/output_"
|
|
help = "Check if code style is correct"
|
|
|
|
|
|
[tool.black]
|
|
target-version = ['py36']
|
|
|
|
[tool.coverage.run]
|
|
omit = ["betterproto/tests/*"]
|
|
|
|
[tool.tox]
|
|
legacy_tox_ini = """
|
|
[tox]
|
|
isolated_build = true
|
|
envlist = py36, py37, py38
|
|
|
|
[testenv]
|
|
whitelist_externals = poetry
|
|
commands =
|
|
poetry install -v --extras compiler
|
|
poetry run pytest --cov betterproto
|
|
"""
|
|
|
|
[build-system]
|
|
requires = ["poetry-core>=1.0.0,<2"]
|
|
build-backend = "poetry.core.masonry.api"
|