Skip to content

Python: Missing datetime imports in generated pyi stub files #22178

@iulian-gm

Description

@iulian-gm

What version of protobuf and what language are you using?
Versions:

  • protobuf: 6.31.1
  • grpcio-tools: 1.73.0
  • Python 3.13.3

Language: Python

What operating system (Linux, Windows, ...) and version?
macOS Sequoia 15.5

What runtime / compiler are you using (e.g., python version or gcc version)
Python 3.13.3
uv 0.7.12

What did you do?
Generate Python code from proto files that import google/protobuf/timestamp.proto.
They are generated using grpcio-tools:

uv run --with grpcio-tools python -m grpc_tools.protoc \
        --proto_path={{PROTO_DIR}} \
        --python_out={{PROTO_OUTPUT_DIR_PY}} \
        --grpc_python_out={{PROTO_OUTPUT_DIR_PY}} \
        --pyi_out={{PROTO_OUTPUT_DIR_PY}} \
        .... 

What did you expect to see
Generated Python files should have correct imports.

What did you see instead?
Generated .pyi stub files use datetime.datetime in type hints but don't import the datetime module.
When protobuf generates type stubs for timestamp fields, it creates type hints like _Optional[_Union[datetime.datetime, _timestamp_pb2.Timestamp, _Mapping]] but forgets to add import datetime to the imports section.

Anything else we should know about your project / environment
I am using edition = "2023"; syntax.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions