Skip to main content

This is simple `.pyi` stubs generator from thrift interfaces

Project description

Travis-CI Build Status Coverage Status Codacy Badge Requirements Status PyPI Package latest release PyPI Wheel Supported versions MIT License

This is simple .pyi stubs generator from thrift interfaces. Motivation for this project is to have autocomplete and type checking for dynamically loaded thrift interfaces


pip install thrift-pyi


Sample usage:

$ thriftpyi example/interfaces --output example/app/interfaces

Additionally to generated stubs you might want to create that will load thrift interfaces, for example:

from pathlib import Path
from types import ModuleType
from typing import Dict

import thriftpy2

_interfaces_path = Path("example/interfaces")
_interfaces: Dict[str, ModuleType] = {}

def __getattr__(name):
        return _interfaces[name]
    except KeyError:
        interface = thriftpy2.load(str(_interfaces_path.joinpath(f"{name}.thrift")))
        _interfaces[name] = interface
        return interface

To see more detailed example of usage refer to example app


Python and thrift are very different at argument handling. For example in thrift the following will be correct declaration:

struct TodoItem {
    1: required i32 id
    3: optional i32 type = 1
    2: required string text

In python attributes without a default cannot follow attributes with one. Therefore by default all fields are optional with default to None. This is compliant to thriftpy2.

However, if you want more strict behaviour you can specify –strict-optional option. For the case above, the following stubs will be generated:

from dataclasses import dataclass

class TodoItem:
    id: int
    type: int = 1
    text: str


To install pre-commit hooks:

pre-commit install

To run the all tests run:


Project details

Download files

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

Files for thrift-pyi, version 0.2.0
Filename, size File type Python version Upload date Hashes
Filename, size thrift_pyi-0.2.0-py3-none-any.whl (9.5 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size thrift-pyi-0.2.0.tar.gz (8.5 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page