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

Installation

pip install thrift-pyi

Quickstart

Sample usage:

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

Additionally to generated stubs you might want to create __init__.py 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):
    try:
        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

–strict-optional

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

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

Development

To install pre-commit hooks:

pre-commit install

To run the all tests run:

tox

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

thrift-pyi-0.1.0b4.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

thrift_pyi-0.1.0b4-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file thrift-pyi-0.1.0b4.tar.gz.

File metadata

  • Download URL: thrift-pyi-0.1.0b4.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.16 CPython/3.7.1 Linux/4.15.0-1028-gcp

File hashes

Hashes for thrift-pyi-0.1.0b4.tar.gz
Algorithm Hash digest
SHA256 0bf98980e850e7685e434a40eae03e2566fdaea5cd488af8c95dc8f2fe6e605a
MD5 03078ae2b09633757ba5d16e5fa66611
BLAKE2b-256 dcfd532555f407fad8cda18f6b41abf9acf45fa4e108c1f7142713b8f7d5252d

See more details on using hashes here.

File details

Details for the file thrift_pyi-0.1.0b4-py3-none-any.whl.

File metadata

  • Download URL: thrift_pyi-0.1.0b4-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.16 CPython/3.7.1 Linux/4.15.0-1028-gcp

File hashes

Hashes for thrift_pyi-0.1.0b4-py3-none-any.whl
Algorithm Hash digest
SHA256 73ecb9c338cd64202710845fbb79280c07c3aa90ac50fb954624f3b6f3512521
MD5 2b8f70b3f5444d1db82f312b05f664d4
BLAKE2b-256 aff338e6b884bfe2103fd6ad6e393ea28d458504b70c144c13fcf25e366d5976

See more details on using hashes here.

Supported by

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