Skip to content

stubgen: Add typings of keys and values of dict (__members__, __entries) #15888

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

Open
jasminlapalme opened this issue Aug 16, 2023 · 2 comments
Open

Comments

@jasminlapalme
Copy link

jasminlapalme commented Aug 16, 2023

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

For example, if we take the same example as #12717.

#include <cstddef>
#include <memory>
#include <pybind11/pybind11.h>

namespace py = pybind11;

enum class MyEnum { FOO, BAR };

PYBIND11_MODULE(native_enum_test, module) {
  pybind11::enum_<MyEnum>(module, "MyEnum", pybind11::arithmetic())
      .value("FOO", MyEnum::FOO)
      .value("BAR", MyEnum::BAR);
}

Compile via:

$ c++ -O3 -Wall -shared -std=c++17 -fPIC $(python3 -m pybind11 --includes) native_enum_test.cpp -o native_enum_test.so

Run the stub generator:

stubgen -p native_enum_test

the result is something like

class MyEnum:
    __members__: ClassVar[dict] = ...  # read-only
    BAR: ClassVar[MyEnum] = ...
    FOO: ClassVar[MyEnum] = ...
    __entries: ClassVar[dict] = ...
...

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

error: Missing type parameters for generic type "dict"  [type-arg]

Expected Behavior

The result of stubgen would be like this:

class MyEnum:
    __members__: ClassVar[dict[str, MyEnum]] = ...  # read-only
    BAR: ClassVar[MyEnum] = ...
    FOO: ClassVar[MyEnum] = ...
    __entries: ClassVar[dict[str, tuple[MyEnum, None]]] = ... # not sure about the type of this one...
...

Environment

  • Mypy version used: 1.4.1
  • Mypy command-line flags: not relevant, this is about the stub generator --show-column-numbers --no-error-summary --no-pretty --no-color-output
  • Mypy configuration options from mypy.ini (and other config files): strict = true
  • Python version used: 3.9
@jasminlapalme jasminlapalme added the bug mypy got something wrong label Aug 16, 2023
@AlexWaygood AlexWaygood added topic-stubgen feature and removed bug mypy got something wrong labels Aug 16, 2023
@sobolevn
Copy link
Member

Question about __entries: we usually don't include and annotate private members, why do you think that this one is needed?

@jasminlapalme
Copy link
Author

In my case, I don't need __entries. But mypy generate an error with strict on __entries this is why I include it. I think that stubgen could ignore __entries and not expose this attribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants