Skip to main content

Generate Python MyPy stub modules from protobuf files.

Project description

pyprotostuben

Python Supported versions MyPy Strict Test Coverage Downloads GitHub stars

Generate Python MyPy stub modules from protobuf files.

usage

install into dev dependencies group

poetry add --group dev pyprotostuben

protoc-gen-pyprotostuben

a protoc plugin that generates MyPy stubs

features

  • choose message structure immutability / mutability
  • choose async / sync grpc module stubs
  • grpc servicer abstract methods have full signature (with appropriate type args in generics), thus it is easier to implement methods in IDE

flags

  • message-mutable -- add setters for fields, use mutable containers
  • message-all-init-args-optional -- each field is optional in message constructor (event if required)
  • grpc-sync -- use sync grpc stubs instead of grpc.aio module and async defs
  • grpc-skip-servicer -- don't generate code for servicers
  • grpc-skip-stub -- don't generate code for stubs

examples

requirements

project structure

  • /
    • src/
      • greeting.proto
        syntax = "proto3";
        
        package greeting;
        
        // RPC request for greeting
        message GreetRequest {
          string name = 1;
        }
        
        // RPC response for greeting
        message GreetResponse {
          string text = 1;
        }
        
        // RPC service that provides greet functionality
        service Greeter {
          // RPC method for greeting
          rpc Greet(GreetRequest) returns (GreetResponse) {}
        }
        
    • buf.yaml
      version: v1beta1
      build:
        roots:
          - src
      
    • buf.gen.yaml
      version: v1
      managed:
      enabled: true
      plugins:
      - plugin: pyprotostuben
        out: out
        strategy: all
      

run codegen

buf generate

output

src/greeting_pb2.pyi
import builtins
import google.protobuf.message
import typing

class GreetRequest(google.protobuf.message.Message):
    """RPC request for greeting"""

    def __init__(self, *, name: builtins.str) -> None:...

    @builtins.property
    def name(self) -> builtins.str:...

    def HasField(self, field_name: typing.NoReturn) -> typing.NoReturn:...

    def WhichOneof(self, oneof_group: typing.NoReturn) -> typing.NoReturn:...

class GreetResponse(google.protobuf.message.Message):
    """RPC response for greeting"""

    def __init__(self, *, text: builtins.str) -> None:...

    @builtins.property
    def text(self) -> builtins.str:...

    def HasField(self, field_name: typing.NoReturn) -> typing.NoReturn:...

    def WhichOneof(self, oneof_group: typing.NoReturn) -> typing.NoReturn:...
src/greeting_pb2_grpc.pyi
import abc
import builtins
import greeting_pb2
import grpc
import grpc.aio
import typing

class GreeterServicer(metaclass=abc.ABCMeta):
    """RPC service that provides greet functionality"""

    @abc.abstractmethod
    async def Greet(self, request: greeting_pb2.GreetRequest, context: grpc.aio.ServicerContext[greeting_pb2.GreetRequest, greeting_pb2.GreetResponse]) -> greeting_pb2.GreetResponse:
        """RPC method for greeting"""
        ...

def add_GreeterServicer_to_server(servicer: GreeterServicer, server: grpc.aio.Server) -> None:...

class GreeterStub:
    """RPC service that provides greet functionality"""

    def __init__(self, channel: grpc.aio.Channel) -> None:...

    def Greet(self, request: greeting_pb2.GreetRequest, *, timeout: typing.Optional[builtins.float]=None, metadata: typing.Optional[grpc.aio.MetadataType]=None, credentials: typing.Optional[grpc.CallCredentials]=None, wait_for_ready: typing.Optional[builtins.bool]=None, compression: typing.Optional[grpc.Compression]=None) -> grpc.aio.UnaryUnaryCall[greeting_pb2.GreetRequest, greeting_pb2.GreetResponse]:
        """RPC method for greeting"""
        ...

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyprotostuben-0.2.1.tar.gz (22.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyprotostuben-0.2.1-py3-none-any.whl (31.6 kB view details)

Uploaded Python 3

File details

Details for the file pyprotostuben-0.2.1.tar.gz.

File metadata

  • Download URL: pyprotostuben-0.2.1.tar.gz
  • Upload date:
  • Size: 22.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for pyprotostuben-0.2.1.tar.gz
Algorithm Hash digest
SHA256 82417a0a574623821c3718ae869dff528ec2e7d1a146a240d5fb082e10d12513
MD5 556daf6ee0f8b1df9eae0335e3cb1043
BLAKE2b-256 288b4cdadb6c07830d8107a1abb2dcae65b6ae9b98007e083f62da56c0b4b611

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyprotostuben-0.2.1.tar.gz:

Publisher: publish.yaml on zerlok/pyprotostuben

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyprotostuben-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: pyprotostuben-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 31.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for pyprotostuben-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 848e9c7a17156e5630726b33c681210409c4544cd5e826b1df198ed9b85a3d29
MD5 783cd96c50d6dd7048a9d3ccd1ca3239
BLAKE2b-256 0125b6fc03674697228c352487098ee4a70a4f7d869100873e97961acf34e264

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyprotostuben-0.2.1-py3-none-any.whl:

Publisher: publish.yaml on zerlok/pyprotostuben

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page