protostub

package module
v0.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 6, 2018 License: MIT Imports: 4 Imported by: 0

README

protostub

A tool for generating Mypy type stubs from a Protocol Buffer definition.

Usage

You can download a binary from the releases page, or you can use Docker.

Assuming that you have a .proto file called foo.proto and you want to generate a .pyi file from it, usage is as such:

protostub generate --proto foo.proto

In this case, protostub assumes you want to call your stub foo_pb2.pyi. If this is not the case, or perhaps you want to specify a different directory, the output can be specified like so:

protostub generate --proto foo.proto --mypy bar.pyi

Help is included in the tool:

Generate Mypy type stubs from Protobuf definitions

Usage:
  protostub [command]

Available Commands:
  generate    Generate a stub from a given proto file
  help        Help about any command

Flags:
  -h, --help   help for protostub

Use "protostub [command] --help" for more information about a command.
Using the stubs

If you've tried using mypy in your project, you've probably noticed that it does not do anything for protobuf types. This is because the generated python contains nothing that Mypy can use - classes are not defined in the "normal" way.

We currently use a script to generate a list of files for Mypy to check. If the .py file has a corresponding .pyi file, then the .py is ignored. Otherwise it is used. This allows us to override any Python with a type stub, meaning we can override Python generated by protoc with something generated by protostub, and get functioning type checks!

An alternative would be to invoke Mypy on a module rather than on files, and provide the stubs in MYPYPATH. However, I have not tested this, so I don't know how well it works.

Docker

A docker image is also provided! The easiest way for you to use this is as follows (assuming there is a file called foo.proto in the current directory):

docker run -v $(pwd):/protostub protostub:latest generate -p foo.proto

After doing this, you should see the help text.

Building

Requirements
  • Go
  • make (optional)
With go get

If you already have Go all setup in your PATH, then it is as simple as:

go get github.com/arachnys/protostub/cmd/protostub
With Make

This approach might be best if you're less familiar with Go, and want it to just work. It requires no messing with $GOPATH.

git clone https://github.com/arachnys/protostub
cd protostub
make

The protostub binary should be in the bin folder.

License

The MIT License (MIT)
Copyright © 2018 Arachnys Information Services Ltd and individual contributors.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TranslateType

func TranslateType(t string) string

TranslateType takes in a protobuf type and translates it to a Python primitive type. If there is no translation needed, the input is returned unaltered.

Types

type Enum

type Enum struct {
	Members []Member
	// contains filtered or unexported fields
}

Enum is a protobuf enum

func (Enum) Name

func (s Enum) Name() string

Name returns the name of the enum.

func (Enum) Typename

func (s Enum) Typename() string

Typename returns the typename of the enum. Again, it is the same as Name.

type EnumVisitor

type EnumVisitor struct {
	ProtoData
	Enum Enum
}

EnumVisitor is the Visitor for an enum, created by the main visitor. It contains an Enum with all the type data - see types.go.

func NewEnumVisitor

func NewEnumVisitor() *EnumVisitor

NewEnumVisitor creates a new EnumVisitor

func (*EnumVisitor) VisitEnum

func (ev *EnumVisitor) VisitEnum(e *proto.Enum)

VisitEnum will set the correct name and visit the elements

func (*EnumVisitor) VisitEnumField

func (ev *EnumVisitor) VisitEnumField(e *proto.EnumField)

VisitEnumField creates an enum field to the member list

type Function

type Function struct {
	Comment []string
	// contains filtered or unexported fields
}

Function is a function, usually a RPC method in this case

func (Function) Name

func (f Function) Name() string

Name returns the name of the function.

func (Function) Typename

func (f Function) Typename() string

Typename returns the typename of the function. This includes the name, parameter, and return value in the form foo(bar) -> baz.

type Member

type Member struct {
	Comment []string
	// contains filtered or unexported fields
}

Member is a member of a message

func (Member) Name

func (m Member) Name() string

Name returns the name of the member

func (Member) Typename

func (m Member) Typename() string

Typename returns the type name of the member

type Message

type Message struct {
	Types    []ProtoType
	Members  []Member
	IsExtend bool
	Comment  []string
	// contains filtered or unexported fields
}

Message represents a ProtoBuf message

func (Message) Name

func (m Message) Name() string

Name returns the name of the message

func (Message) Typename

func (m Message) Typename() string

Typename returns the type name of the message. Note that in this case, Name() == Typename()

type MessageVisitor

type MessageVisitor struct {
	ProtoData
	// contains filtered or unexported fields
}

MessageVisitor is created by the main visitor. It contains a Message with all the type data - see types.go.

func NewMessageVisitor

func NewMessageVisitor(name string, extend bool, comment *proto.Comment) *MessageVisitor

NewMessageVisitor creates a new message visitor. `extend` indicates if this is an extension, and comment contains the comment attached to this message.

func (*MessageVisitor) VisitNormalField

func (mv *MessageVisitor) VisitNormalField(n *proto.NormalField)

VisitNormalField adds the field to the list. Also makes sure primitive types are properly translated.

func (*MessageVisitor) VisitOneof

func (mv *MessageVisitor) VisitOneof(o *proto.Oneof)

VisitOneof visits a OneOf

func (*MessageVisitor) VisitOneofField

func (mv *MessageVisitor) VisitOneofField(o *proto.OneOfField)

VisitOneofField visits a field in a OneOf

type ProtoData

type ProtoData struct {
	Types []ProtoType
	// contains filtered or unexported fields
}

ProtoData is the main visitor, which passes off to the other ones. I try to have a visitor per "major" type - message, service, enum, etc. This base Visitor handles the basic stuff.

func New

func New(r io.Reader) *ProtoData

New creates a new protodata object. It takes a reader to the proto file

func (*ProtoData) Parse

func (v *ProtoData) Parse() error

Parse will parse the proto file, and dispatch to all the visitors. Error is returned on failure

func (*ProtoData) VisitComment

func (v *ProtoData) VisitComment(c *proto.Comment)

VisitComment currently does nothing, comments are only handled when attached to messages and their fields.

func (*ProtoData) VisitEnum

func (v *ProtoData) VisitEnum(e *proto.Enum)

VisitEnum will create a new Enum visitor and dispatch it.

func (*ProtoData) VisitEnumField

func (v *ProtoData) VisitEnumField(e *proto.EnumField)

VisitEnumField will panic when called here, as enum fields should be inside enums.

func (*ProtoData) VisitExtensions

func (v *ProtoData) VisitExtensions(e *proto.Extensions)

VisitExtensions is currently not implemented

func (*ProtoData) VisitGroup

func (v *ProtoData) VisitGroup(g *proto.Group)

VisitGroup is currently not implemented

func (*ProtoData) VisitImport

func (v *ProtoData) VisitImport(i *proto.Import)

VisitImport will try to translate a proto import to a python one. Currently it is a bit of a hack.

func (*ProtoData) VisitMapField

func (v *ProtoData) VisitMapField(m *proto.MapField)

VisitMapField is currently not implemented

func (*ProtoData) VisitMessage

func (v *ProtoData) VisitMessage(m *proto.Message)

VisitMessage will create a MessageVisitor, and dispatch it. The message type is included in protodata.

func (*ProtoData) VisitNormalField

func (v *ProtoData) VisitNormalField(n *proto.NormalField)

VisitNormalField does nothing here, and is implemented in the message visitor.

func (*ProtoData) VisitOneof

func (v *ProtoData) VisitOneof(o *proto.Oneof)

VisitOneof will panic when called from here.

func (*ProtoData) VisitOneofField

func (v *ProtoData) VisitOneofField(o *proto.OneOfField)

VisitOneofField will panic when called from here.

func (*ProtoData) VisitOption

func (v *ProtoData) VisitOption(o *proto.Option)

VisitOption presently does nothing.

func (*ProtoData) VisitPackage

func (v *ProtoData) VisitPackage(p *proto.Package)

VisitPackage presently does nothing.

func (*ProtoData) VisitRPC

func (v *ProtoData) VisitRPC(r *proto.RPC)

VisitRPC will currently do nothing, as RPC should be inside a service.

func (*ProtoData) VisitReserved

func (v *ProtoData) VisitReserved(r *proto.Reserved)

VisitReserved is currently not implemented.

func (*ProtoData) VisitService

func (v *ProtoData) VisitService(r *proto.Service)

VisitService will create a new service visitor, and dispatch it.

func (*ProtoData) VisitSyntax

func (v *ProtoData) VisitSyntax(s *proto.Syntax)

VisitSyntax presently does nothing.

type ProtoType

type ProtoType interface {
	Name() string
	Typename() string
}

ProtoType defines that a type must have both a name and a TypeName

type Service

type Service struct {
	Types     []ProtoType
	Functions []Function
	Comment   []string
	// contains filtered or unexported fields
}

Service representts a protobuf service

func (Service) Name

func (s Service) Name() string

Name returns the name of the service

func (Service) Typename

func (s Service) Typename() string

Typename returns the typename of the service. Like Message, in this case Typename is the same as Name.

type ServiceVisitor

type ServiceVisitor struct {
	ProtoData
	// contains filtered or unexported fields
}

ServiceVisitor will visit a service and extract the types.

func NewServiceVisitor

func NewServiceVisitor(name string, comment *proto.Comment) *ServiceVisitor

NewServiceVisitor will create a new service visitor, from a name and a comment.

func (*ServiceVisitor) VisitRPC

func (sv *ServiceVisitor) VisitRPC(r *proto.RPC)

VisitRPC will visit a RPC, and make sure the function specification is included in the service data definition.

Directories

Path Synopsis
cmd
protostub command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL