protocolbuffers / protobuf

Protocol Buffers - Google's data interchange format
http://protobuf.dev
Other
65.11k stars 15.43k forks source link

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

Open matejsp opened 6 months ago

matejsp commented 6 months ago

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 /

Avasam commented 4 months ago

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 commented 4 months ago

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

Avasam commented 4 months ago

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: https://github.com/pylint-dev/astroid/pull/2182

Specifically relevant issues: https://github.com/pylint-dev/pylint/issues/6281 & https://github.com/pylint-dev/pylint/issues/9185

matejsp commented 4 months ago

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