typebuf

Dead simple way to define shared type definitions between applications


Keywords
python, types, typescript
License
Apache-2.0
Install
pip install typebuf==0.1.4

Documentation

TypeBuf

PyPI version CircleCI codecov Maintainability Rating Reliability Rating


Dead simple way to create shared type definitions between applications

  • Other tools have too much setup? Too much boilerplate? Do too much "magic"? Just:
    • Define your Types in types.yaml
    • Run TypeBuf: $ typebuf compile -f types.yaml -l python -l typescript

Install

pip install typebuf

Quickstart

  1. Create a file called types.yaml, it can be anywhere, like your project's root dir

  2. Add the following lines to the newly created file:

---
typedefs:
  - typename: User
    fields:
      - name: first_name
        type: string
        optional: false
      - name: age
        type: int
        optional: true
  1. Now call TypeBuf with: $ typebuf compile -l python -l typescript

  2. You now have two new files, one called user.py and one called user.ts that can you use for things like data serialization/deserialization, validation, subclassing, etc

Generated Python:

"""
User type definition file generated by TypeBuf
Note: Any changes made to this file will be overwritten
      during next compilation
"""

from typing import *


class User:
    first_name: str
    age: Optional[int]

Generated TypeScript:

/**
 * User type definition file generated by TypeBuf
 * Note: Any changes made to this file will be overwritten
 *       during next compilation
 */

interface User {
  first_name: string;
  age?: number;
}

Note: So far only Python and TypeScript are supported. More languages coming soon!

Demo

Here's a quick demo of me using TypeBuf. First I show the contents of the types.yaml file then I generate Python and TypeScript source code and show the contents of the new files

asciicast

Documentation

  • Reads only a file named types.yaml (for now)

  • Inside that file there is an array called typedefs. This is where you add your shared type definitions

  • Each type will have the following fields:

    • typename: string
    • fields: array[Field]
      • A Field has the following attributes:
        • name: string
        • type: string
        • optional: boolean
  • In a type definition you can also specify custom/required imports for any depedencies your classes may need:

    • typedefs:
        - typename: Address
          imports:
            python:
              - 'from mymodule.user import User'
              - 'from typing import Optional'
            typescript:
              - 'import { User } from "./User";'
          fields:
            - ...