Skip to content
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

add protobuf serialization test #4224

Merged
merged 6 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ jobs:
run: |
source ${{ github.workspace }}/python/.venv/bin/activate
poe gen-proto
poe gen-test-proto
working-directory: ./python
- name: Check if there are uncommited changes
id: changes
Expand Down
6 changes: 3 additions & 3 deletions python/packages/autogen-core/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ dev-dependencies = [

[tool.ruff]
extend = "../../pyproject.toml"
exclude = ["build", "dist", "src/autogen_core/application/protos"]
exclude = ["build", "dist", "src/autogen_core/application/protos", "tests/protos"]
include = ["src/**", "samples/*.py", "docs/**/*.ipynb", "tests/**"]

[tool.ruff.lint.per-file-ignores]
Expand All @@ -91,7 +91,7 @@ include = ["src/**", "samples/*.py", "docs/**/*.ipynb", "tests/**"]
[tool.pyright]
extends = "../../pyproject.toml"
include = ["src", "tests", "samples"]
exclude = ["src/autogen_core/application/protos"]
exclude = ["src/autogen_core/application/protos", "tests/protos"]
reportDeprecated = false

[tool.pytest.ini_options]
Expand All @@ -111,7 +111,7 @@ include = "../../shared_tasks.toml"
test = "pytest -n auto"
mypy.default_item_type = "cmd"
mypy.sequence = [
"mypy --config-file ../../pyproject.toml --exclude src/autogen_core/application/protos src tests",
"mypy --config-file ../../pyproject.toml --exclude src/autogen_core/application/protos --exclude tests/protos src tests",
"nbqa mypy docs/src --config-file ../../pyproject.toml",
]

Expand Down
11 changes: 11 additions & 0 deletions python/packages/autogen-core/tests/protos/serialization_test.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto3";

package agents;

message ProtoMessage {
string message = 1;
}
message NestingProtoMessage {
string message = 1;
ProtoMessage nested = 2;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
@generated by mypy-protobuf. Do not edit manually!
isort:skip_file
"""

import builtins
import google.protobuf.descriptor
import google.protobuf.message
import typing

DESCRIPTOR: google.protobuf.descriptor.FileDescriptor

@typing.final
class ProtoMessage(google.protobuf.message.Message):
DESCRIPTOR: google.protobuf.descriptor.Descriptor

MESSAGE_FIELD_NUMBER: builtins.int
message: builtins.str
def __init__(
self,
*,
message: builtins.str = ...,
) -> None: ...
def ClearField(self, field_name: typing.Literal["message", b"message"]) -> None: ...

global___ProtoMessage = ProtoMessage

@typing.final
class NestingProtoMessage(google.protobuf.message.Message):
DESCRIPTOR: google.protobuf.descriptor.Descriptor

MESSAGE_FIELD_NUMBER: builtins.int
NESTED_FIELD_NUMBER: builtins.int
message: builtins.str
@property
def nested(self) -> global___ProtoMessage: ...
def __init__(
self,
*,
message: builtins.str = ...,
nested: global___ProtoMessage | None = ...,
) -> None: ...
def HasField(self, field_name: typing.Literal["nested", b"nested"]) -> builtins.bool: ...
def ClearField(self, field_name: typing.Literal["message", b"message", "nested", b"nested"]) -> None: ...

global___NestingProtoMessage = NestingProtoMessage
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
"""Client and server classes corresponding to protobuf-defined services."""
import grpc

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""
@generated by mypy-protobuf. Do not edit manually!
isort:skip_file
"""

import abc
import collections.abc
import grpc
import grpc.aio
import typing

_T = typing.TypeVar("_T")

class _MaybeAsyncIterator(collections.abc.AsyncIterator[_T], collections.abc.Iterator[_T], metaclass=abc.ABCMeta): ...

class _ServicerContext(grpc.ServicerContext, grpc.aio.ServicerContext): # type: ignore[misc, type-arg]
...
31 changes: 31 additions & 0 deletions python/packages/autogen-core/tests/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from autogen_core.base._serialization import DataclassJsonMessageSerializer, PydanticJsonMessageSerializer
from autogen_core.components import Image
from PIL import Image as PILImage
from protos.serialization_test_pb2 import NestingProtoMessage, ProtoMessage
from pydantic import BaseModel


Expand Down Expand Up @@ -83,6 +84,36 @@ def test_nesting_dataclass_dataclass() -> None:
serde.add_serializer(try_get_known_serializers_for_type(NestingDataclassMessage))


def test_proto() -> None:
serde = SerializationRegistry()
serde.add_serializer(try_get_known_serializers_for_type(ProtoMessage))

message = ProtoMessage(message="hello")
name = serde.type_name(message)
# TODO: should be PROTO_DATA_CONTENT_TYPE
data = serde.serialize(message, type_name=name, data_content_type=JSON_DATA_CONTENT_TYPE)
assert name == "ProtoMessage"
# TODO: assert data == stuff
deserialized = serde.deserialize(data, type_name=name, data_content_type=JSON_DATA_CONTENT_TYPE)
assert deserialized == message


def test_nested_proto() -> None:
serde = SerializationRegistry()
serde.add_serializer(try_get_known_serializers_for_type(NestingProtoMessage))

message = NestingProtoMessage(message="hello", nested=ProtoMessage(message="world"))
name = serde.type_name(message)

# TODO: should be PROTO_DATA_CONTENT_TYPE
data = serde.serialize(message, type_name=name, data_content_type=JSON_DATA_CONTENT_TYPE)

# TODO: assert data == stuff

deserialized = serde.deserialize(data, type_name=name, data_content_type=JSON_DATA_CONTENT_TYPE)
assert deserialized == message


@dataclass
class DataclassNestedUnionSyntaxOldMessage:
message: Union[str, int]
Expand Down
2 changes: 2 additions & 0 deletions python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,5 @@ test = "python run_task_in_pkgs_if_exist.py test"
check = ["fmt", "lint", "pyright", "mypy", "test"]

gen-proto = "python -m grpc_tools.protoc --python_out=./packages/autogen-core/src/autogen_core/application/protos --grpc_python_out=./packages/autogen-core/src/autogen_core/application/protos --mypy_out=./packages/autogen-core/src/autogen_core/application/protos --mypy_grpc_out=./packages/autogen-core/src/autogen_core/application/protos --proto_path ../protos/ agent_worker.proto --proto_path ../protos/ cloudevent.proto"

gen-test-proto = "python -m grpc_tools.protoc --python_out=./packages/autogen-core/tests/protos --grpc_python_out=./packages/autogen-core/tests/protos --mypy_out=./packages/autogen-core/tests/protos --mypy_grpc_out=./packages/autogen-core/tests/protos --proto_path ./packages/autogen-core/tests/protos serialization_test.proto"
Loading