sizmailov / pybind11-stubgen

Generate stubs for python modules
Other
238 stars 49 forks source link

Add typings of keys and values of __members__ dict #111

Closed jasminlapalme closed 1 year ago

jasminlapalme commented 1 year ago

It would be useful to add typings of the keys and values of the __members__ dictionary on class that represent enum in C.

For example, with this pybind11 code of library named lib :

py::enum_<Number>(m, "Number")
  .value("Zero", Number::Zero)
  .value("One", Number::One)
  .value("Tow", Number::Two);

instead of :

class Number():
  ....
  __members__: dict # value = {'Zero': <Number.Zero: 0>, 'One': < Number.One: 1>, 'Two': <Number.Two: 2>}

to have :

class Number():
  ....
  __members__: dict[str, lib.Number] # value = {'Zero': <Number.Zero: 0>, 'One': < Number.One: 1>, 'Two': <Number.Two: 2>}

When we import code with these kinds of stubs and use mypy to type checked the code, we have this warning.

error: Missing type parameters for generic type "dict"  [type-arg]
sizmailov commented 1 year ago

Thanks for the suggestion!

Implemented in #112

Example from tests: https://github.com/sizmailov/pybind11-stubgen/blob/a1cd5faab09e642d4c0692474790ca737a271baf/tests/stubs/demo/_bindings/enum.pyi#L39-L41