Skip to main content

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

Project description

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, fields without default values cannot appear after fields with default values. 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-2.5.0.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

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

thrift_pyi-2.5.0-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file thrift_pyi-2.5.0.tar.gz.

File metadata

  • Download URL: thrift_pyi-2.5.0.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.9.25 Linux/6.11.0-1018-azure

File hashes

Hashes for thrift_pyi-2.5.0.tar.gz
Algorithm Hash digest
SHA256 2bf50af403deeb4ae62ec6bc9e23f25182c9cf6ba33fd6c6c06599a535a566ca
MD5 3d36328f830b1ea4c841a2c9c0e2086f
BLAKE2b-256 df2671da9577a2cf5bac0eeb90f8e5815973ad2743353e4131d8e926e8b57098

See more details on using hashes here.

File details

Details for the file thrift_pyi-2.5.0-py3-none-any.whl.

File metadata

  • Download URL: thrift_pyi-2.5.0-py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.9.25 Linux/6.11.0-1018-azure

File hashes

Hashes for thrift_pyi-2.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 039a5cffb33e1865b98cf0358019518a9c98e928180b67949f20c8baf0504e81
MD5 0c20e164f2dc55e6aa83c972bb36bd91
BLAKE2b-256 7ccf1c8a301c5d5e383ffbacfa2c46fb2040c9a7b9787702e45fa8ef6112f896

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