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 __init__.py that will load thrift interfaces, for example:
frompathlibimportPath
fromtypesimportModuleType
fromtypingimportDict
importthriftpy2
_interfaces_path=Path("example/interfaces")
_interfaces:Dict[str,ModuleType]={}
def__getattr__(name):
try:
return_interfaces[name]
exceptKeyError:
interface=thriftpy2.load(str(_interfaces_path.joinpath(f "{name}.thrift ")))
_interfaces[name]=interface
returninterface
To see more detailed example of usage refer toexample app
Python and thrift are very different at argument handling. For example in thrift the following will be correct declaration:
structTodoItem{
1:requiredi32id
3:optionali32type=1
2:requiredstringtext
}
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 tothriftpy2.
However, if you want more strict behaviour you can specify --strict-optional option. For the case above, the following stubs will be generated:
fromdataclassesimportdataclass
@dataclass
classTodoItem:
id:int
type:int=1
text:str
To install pre-commit hooks:
pre-commit install
To run the all tests run:
tox