Skip to main content

Dead simple way to define shared type definitions between applications

Project description

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:
            - ...
      

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

typebuf-0.1.3.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

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

typebuf-0.1.3-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file typebuf-0.1.3.tar.gz.

File metadata

  • Download URL: typebuf-0.1.3.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.9.9 Linux/5.15.7-1-MANJARO

File hashes

Hashes for typebuf-0.1.3.tar.gz
Algorithm Hash digest
SHA256 8bb93af65233a09631bd34744e048e00cb444d2ed127d7ec86bfe0554ff54563
MD5 0c0595a5bac2fe7e370ed362cdaf6a2a
BLAKE2b-256 bb52028eff34917fd33fd39a4ac23b020dbeea94813a919bd41b54f33d82ce4f

See more details on using hashes here.

File details

Details for the file typebuf-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: typebuf-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 12.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.9.9 Linux/5.15.7-1-MANJARO

File hashes

Hashes for typebuf-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 32203435449acc47cb4fcdc507ae211f69d4209ef6814135f2149cbb9e8dde1c
MD5 fcef3740efac71ed24ea5134f5e56acb
BLAKE2b-256 cc32077cd8ee356f03551c57001c879f06e7b39599328638afb482afb37955e6

See more details on using hashes here.

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