python / python/mypy

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

Open
#15,888 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature topic-stubgen
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the stubgen implementation and reproduce the report using the provided C++ enum example, then run stubgen -p native_enum_test. The work is done when generated __members__ and __entries annotations include appropriate type parameters and the resulting stubs no longer trigger the reported strict mypy warning.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.