Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Support generate pyi for python binding #100

Open
Wh1isper opened this issue Nov 9, 2023 · 6 comments
Open

[Feature]: Support generate pyi for python binding #100

Wh1isper opened this issue Nov 9, 2023 · 6 comments

Comments

@Wh1isper
Copy link

Wh1isper commented Nov 9, 2023

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: grpc/grpc#29650 (comment))

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

@yurishkuro
Copy link
Member

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

@Wh1isper
Copy link
Author

Wh1isper commented Nov 9, 2023

@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.

hitsz-ids/duetector@d1d05cb

@yurishkuro
Copy link
Member

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

@Wh1isper
Copy link
Author

Wh1isper commented Nov 9, 2023

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

@yurishkuro
Copy link
Member

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

@tarmath
Copy link

tarmath commented Jan 3, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants