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

[Python] Protobuf python generated pb2 files are without typing files #16115

Open
matejsp opened this issue Mar 11, 2024 · 9 comments
Open

[Python] Protobuf python generated pb2 files are without typing files #16115

matejsp opened this issue Mar 11, 2024 · 9 comments
Assignees
Labels
enhancement python untriaged auto added to all issues by default when created.

Comments

@matejsp
Copy link

matejsp commented Mar 11, 2024

Please generate with pyi files for python for builtin types.

Currently we have a lot of errors using those types with pylint and need to ignore checking:

'google.protobuf.timestamp_pb2' has no 'Timestamp' member .
image

What version of protobuf and what language are you using?
Version: 4.25.2 (from pypi)
Language: Python

What operating system (Linux, Windows, ...) and version?
MacOSX, Linux

What runtime / compiler are you using (e.g., python version or gcc version)
python 3.11.2

What did you do?

import "google/protobuf/timestamp.proto";

message SomeMessage {
  google.protobuf.Timestamp timestamp = 2;
}

python -m grpc_tools.protoc -I. --python_out=. --pyi_out=. --grpc_python_out=. *.proto

What did you expect to see
Protobuf should include pyi files and pass pylint validation when using timestampa type.

What did you see instead?

'google.protobuf.timestamp_pb2' has no 'Timestamp' member .

Anything else we should know about your project / environment
/

@matejsp matejsp added the untriaged auto added to all issues by default when created. label Mar 11, 2024
@matejsp matejsp changed the title Protobuf python generated pb2 files are without typing files [PYTHON] Protobuf python generated pb2 files are without typing files Mar 11, 2024
@matejsp matejsp changed the title [PYTHON] Protobuf python generated pb2 files are without typing files [Python] Protobuf python generated pb2 files are without typing files Mar 11, 2024
@Avasam
Copy link

Avasam commented Apr 18, 2024

This should be fairly easy to do using https://pypi.org/project/mypy-protobuf/
Here's how typeshed currently does it: https://github.com/python/typeshed/blob/main/scripts/generate_proto_stubs.sh

I'd love to see protobuf provide their own generated stubs. In the mean time you can install https://pypi.org/project/types-protobuf/

@matejsp
Copy link
Author

matejsp commented Apr 18, 2024

@Avasam yes for mypy it works with mypy-protobuf, but not with pylint. We actually use both.

@Avasam
Copy link

Avasam commented Apr 18, 2024

Sounds like a pylint issue https://github.com/pylint-dev/pylint/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+pyi+in%3Atitle Pylint didn't even support linting pyi files until last year: pylint-dev/astroid#2182

Specifically relevant issues: pylint-dev/pylint#6281 & pylint-dev/pylint#9185

@matejsp
Copy link
Author

matejsp commented Apr 19, 2024

@Avasam pylint supports pyi just fine and we are using them in our own proto files. But when using timestamp from protobuf package it does not work for that specific type. This is the main reason for this ticket. To also generate typing pyi files. Because then it works out of the box with latest pylint.

For mypy yes it works with mypy-protobuf, but wouldn't it be better to just use python native typing option instead since it is already implemented and it works just bundled generated code is lacking them?

Copy link

github-actions bot commented Oct 8, 2024

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please add a comment.

This issue is labeled inactive because the last activity was over 90 days ago. This issue will be closed and archived after 14 additional days without activity.

@github-actions github-actions bot added the inactive Denotes the issue/PR has not seen activity in the last 90 days. label Oct 8, 2024
@matejsp
Copy link
Author

matejsp commented Oct 8, 2024

This issue is still pending.

Python introduced pyi files with https://peps.python.org/pep-0561/ and it helps IDE, linters and other tools to correctly get type information from python files. Protobuf is still missing bundled pyi files for auto generated files (like timestamp) requiring third party plugins (like mypy-protobuf) instead of using recommended and official way.

@github-actions github-actions bot removed the inactive Denotes the issue/PR has not seen activity in the last 90 days. label Oct 9, 2024
@dyhn78
Copy link

dyhn78 commented Nov 7, 2024

@matejsp Isn't this what you are looking for? I set --python_out and --pyi_out as same directory

https://protobuf.dev/getting-started/pythontutorial/#compiling-protocol-buffers
Protoc is also able to generate python stubs (.pyi) with --pyi_out.

@matejsp
Copy link
Author

matejsp commented Nov 7, 2024

We do that already for our own generated python code from proto files.

But I cannot do that for google.protobuf.timestamp_pb2 since timestamp_pb2 is bundled from this repostiory python whl.

Basically this issue is about adding --pyi_out to the following code in this repo:
https://github.com/protocolbuffers/protobuf/blob/main/python/protobuf_distutils/protobuf_distutils/generate_py_protobufs.py

@dyhn78
Copy link

dyhn78 commented Nov 8, 2024

I see, so you want the builtin types to have pyi files as well 😅 understood.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement python untriaged auto added to all issues by default when created.
Projects
None yet
Development

No branches or pull requests

5 participants