jaeger-idl icon indicating copy to clipboard operation
jaeger-idl copied to clipboard

[Feature]: Support generate pyi for python binding

Open Wh1isper opened this issue 2 years ago • 6 comments

Requirement

In protobuf 3.20.0, we can use protoc to generate pyi for python's type hint. This has been pulled into a released gRPC version(>= 1.48.2, as metioned here: https://github.com/grpc/grpc/issues/29650#issuecomment-1412755210)

Note that the current version of grpc is 1.35.0, so in the next upgrade, if the version is met, hopefully the relevant builds will be added.

Problem

pyi will provide python users with a great programming experience and enable type-checking tools.

Proposal

No response

Open questions

No response

Wh1isper avatar Nov 09 '23 14:11 Wh1isper

How is that going to be different from the Python binding (python_out) we already have?

yurishkuro avatar Nov 09 '23 16:11 yurishkuro

@yurishkuro You can refer to this commit, I've introduced a portion of the pyi file into the project so that when using the relevant message class, the IDE (and of course VSCode) will be able to understand what properties the class has and give code hints.

https://github.com/hitsz-ids/duetector/commit/d1d05cbb749d91d16b54121f8e53515c576abc1e

Wh1isper avatar Nov 09 '23 16:11 Wh1isper

You would get the same effect by introducing plain .py generated types. What extra value does .pyi provide?

yurishkuro avatar Nov 09 '23 16:11 yurishkuro

You would get the same effect by introducing plain .py generated types. What extra value does .pyi provide?

Not quite right, according to PEP 484, pyi files will provide type references so that type checking tools can work. This approach does not require modification of the code being executed, and therefore does not break any compatibility (in fact, Python's type hints have gone through several changes).

Here's an example of a static analysis I found in the Internet: image

In the official example, pyi files are also provided. https://github.com/grpc/grpc/blob/master/examples/python/helloworld/helloworld_pb2.pyi

Wh1isper avatar Nov 09 '23 16:11 Wh1isper

Type hints are part of Python language. .pyi are C bindings.

yurishkuro avatar Nov 09 '23 16:11 yurishkuro

Type hints are part of Python language. .pyi are C bindings.

Type hints can both be inlined in the python code, as well as separately (including in separate packages) in .pyi files, aka stub files.

They are described in the pep link that @Wh1isper provided above, in the following section: https://peps.python.org/pep-0484/#stub-files

tarmath avatar Jan 03 '24 21:01 tarmath