Skip to main content

schematics-proto3

Built upon Schematics - Python Data Structures for Humans™, schematics-proto3 brings the awesome features of Schematics to Protobuf 3 world.

Build codecov Documentation Status PyPI version

Caution

Library is currently in WORK IN PROGRESS state.

What is implemented and tested:

  1. Loading Protobuf 3 messages to Model instances.
    • for most of the Protobuf 3 types, including wrappers, repeated oneof fields
    • Enum type
  2. Validation and structured error messages.

To be done:

  1. Serializing Model instances to Protobuf 3 messages.
  2. Make the library more user-friendly.
  3. Schematics "roles".

Installation

pip install schematics-proto3

Motivation

As good and widely supported as it is, Protobuf 3 still has some quirks which can make working with it painful and repetitive. Especially, building complex gRPC services might reveal a number of deficiencies in available tooling.

schematics-proto3 aims to address this problem, in particular:

  • [#359] default values and testing if a field is set in v3

    There is a workaround for this, schematics-proto3 incorporates wrapper types to hide nested messages underneath.

  • no proper data handling library

    Comparing to Serializers in Django Rest Framework or Marshmallow, there seems to be no full fledged serialization / validation / deserialization library for Protobuf 3. Thanks to Schematics, schematics-proto3 is able to provide:

    • declarative models
    • custom validation functions
    • structured error messages (currently only as Python dict)

Example

Let's take Schematics example and modify it to work with Protobuf.

We have a following Protobuf message (and person_pb2 Python module).

syntax = "proto3";

import "google/protobuf/wrappers.proto";

package example;

message Person {
  google.protobuf.StringValue name = 1;
  google.protobuf.StringValue website = 2;
}

And reflect above message in Model class.

from schematics_proto3 import Model
from schematics_proto3 import types as pbtypes

import person_pb2 as pb2


class PersonModel(Model, protobuf_message=pb2.Person):
    name = pbtypes.StringWrapperType()
    website = pbtypes.StringWrapperType()

Let's load some data.

msg = pb2.Person()
msg.name.value = 'Jon Doe'
msg.website.value = 'https://example.com'

model = PersonModel.load_protobuf(msg)
model.validate()

assert model.name == 'Jon Doe'
assert model.website == 'https://example.com'

assert model.to_native() == {'name': 'Jon Doe', 'website': 'https://example.com'}

Not setting a field will give you an Unset.

from schematics_proto3.unset import Unset

msg = pb2.Person()
msg.name.value = 'Jon Doe'

model = PersonModel.load_protobuf(msg)
model.validate()

assert model.name == 'Jon Doe'
assert model.website is Unset

Release files for schematics-proto3 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for schematics-proto3 0.1.3
File Size Uploaded
schematics-proto3-0.1.3.tar.gz 46.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for schematics-proto3 0.1.3
File Interpreter ABI Platform
schematics_proto3-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 62.6 kB

Release files / schematics-proto3-0.1.3.tar.gz

Download URL schematics-proto3-0.1.3.tar.gz
Size 46.9 kB
Tags Source
SHA-256 checksum
How to use checksums
aec8296d87ddd499ea5c0ab6807e595c328a4aa717e212a622cd534816c9a6d8
BLAKE2b-256 checksum
How to use checksums
b8f91c90d2668b7084bc073c7691fff875a73fd78a6bc47f706eb6fe7d25dc7e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.3.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

Release files / schematics_proto3-0.1.3-py3-none-any.whl

Download URL schematics_proto3-0.1.3-py3-none-any.whl
Size 15.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fc12800a8e34b3839f665b7459b25e33e00638537d38775b9e0b309651397bc3
BLAKE2b-256 checksum
How to use checksums
5015fdcecb1961e5a8cde70bdd81d18b9677433c96c3294bd3c4096cad773646
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.3.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page