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-0.4.0.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

thrift_pyi-0.4.0-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file thrift-pyi-0.4.0.tar.gz.

File metadata

  • Download URL: thrift-pyi-0.4.0.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.15 CPython/3.8.13 Linux/5.15.0-1017-azure

File hashes

Hashes for thrift-pyi-0.4.0.tar.gz
Algorithm Hash digest
SHA256 d65f8370ecb9ce5e3cbd33d12d0d4aec03d90861c0851b3d3fa1e47d83341c9b
MD5 e1e40bd8e3f9d34e7820f46315b90406
BLAKE2b-256 60514417608f16887e3c46a896c074eb96b1677531437c05d318712183401c06

See more details on using hashes here.

File details

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

File metadata

  • Download URL: thrift_pyi-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.15 CPython/3.8.13 Linux/5.15.0-1017-azure

File hashes

Hashes for thrift_pyi-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f3fd1eef8a1f07b95ba45da673efac4eccc4cdc6829621afbc255f956431789c
MD5 312a9f0d10e3c28233cbc498a3759741
BLAKE2b-256 4c15c501e75855f8f98b35a3f011784ce9a53a4c79681eedc34fa02413346115

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