Well-known types¶
pure_protobuf.well_known
module provides message definitions for some well-known types.
Any
¶
Since pure-protobuf
is not able to download or parse .proto
definitions, it provides a limited implementation of the Any
message type. That is, you still have to conventionally define message classes and make them importable (similarly to the pickle
behaviour):
test_any.py
from urllib.parse import urlunparse
from pure_protobuf.well_known import Any_
# The class must be importable:
from tests.test_well_known import ChildMessage
# @dataclass
# class ChildMessage(BaseMessage):
# foo: Annotated[int, Field(1)]
child = ChildMessage(foo=42)
any_ = Any_.from_message(child)
assert urlunparse(any_.type_url) == "import://tests.test_well_known/ChildMessage"
assert any_.into_message() == child
Type URL format
Please, consider the URL format a part of the public API. This means, in particular, that future major version bumps may change the format in a backwards-incompatible way.