You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@thrift.apache.org by Victor Boivie <vi...@boivie.com> on 2016/08/18 11:22:13 UTC

Python PEP-484 (Type hints)

Hi all,

I'm using Thrift quite heavily from Python, communicating with a Java
service over Thrift IPC.

While Python isn't a statically typed language, Thrift is, and I have
always found it easier to avoid simple typos in Java since it complains
already in my editor if I'm accessing a field that doesn't exist in a
Thrift struct, or specifying the wrong type as argument to a Thrift service
call.

I started prototyping a bit with adding PEP-484[1] to the Thrift compiler
to help editors such as PyCharm to easily spot[2] when I have simple
mistakes in my code.

What I did is to generate python stub files (.pyi) containing the Python
3.5 type hints to allow these tools to work, and not change the actual
.py-files since we need to be compatible with e.g. Python 2.7

Is this something more people are interested in? I can create a Jira issue
and upload some code once it's a bit cleaned.

Example: Given this service file (the definition of MyStructType is in a
different thrift file):

service FooService {
  foo.MyStructType get(1: string id)

  void put(1: string id, foo.MyStructType obj)
}

... it would generate a .pyi-file containing:

class Iface(object):
    def get(self, id: str) -> sample.package.goes.here.ttypes.MyStructType:
...
    def put(self, id: str, obj:
sample.package.goes.here.ttypes.MyStructType) -> None: ...

Any comments? suggestions?
Thanks,
Victor

[1] https://www.python.org/dev/peps/pep-0484/
[2]
https://blog.jetbrains.com/pycharm/2015/11/python-3-5-type-hinting-in-pycharm-5/