Skip to content

[python] Add type hints for generated classes #2638

@cbornet

Description

@cbornet

It would be great to generate type hints (PEP484/PEP526) for the python generated class so we have IDE completion and type incompatibility warnings.

Eg for :

message Foo {
  string bar = 1;
}

generate

@overload
class Foo:
  bar = None  # type: str

  def __init__(self, bar=None):  # type: (str) -> None
    pass

Foo = _reflection.GeneratedProtocolMessageType('Foo', (_message.Message,), dict(

Note: this way of writing types works with both python 2 and 3. With python >= 3.6, the Foo class could be documented more nicely.
The type annotations are well handled at least by pycharm.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions