Skip to content

Commit 50e48d1

Browse files
committed
add native enum support
1 parent 7c36b24 commit 50e48d1

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

mypy/stubgenc.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -870,8 +870,12 @@ def generate_class_stub(
870870
# special case for __hash__
871871
continue
872872
prop_type_name = self.strip_or_import(self.get_type_annotation(value))
873-
classvar = self.add_name("typing.ClassVar")
874-
static_properties.append(f"{self._indent}{attr}: {classvar}[{prop_type_name}] = ...")
873+
# Pybind supports native enums now
874+
if issubclass(cls, enum.Enum) and attr in cls._member_names_:
875+
static_properties.append(f"{self._indent}{attr} = ...")
876+
else:
877+
classvar = self.add_name("typing.ClassVar")
878+
static_properties.append(f"{self._indent}{attr}: {classvar}[{prop_type_name}] = ...")
875879

876880
self.dedent()
877881

test-data/pybind11_fixtures/expected_stubs_no_docs/pybind11_fixtures/demo.pyi

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
1+
import enum
12
import typing
2-
from typing import ClassVar, overload
3+
from typing import Callable, ClassVar, overload
34

45
PI: float
56
__version__: str
67

8+
class Color(enum.Enum):
9+
__new__: ClassVar[Callable] = ...
10+
GREEN = ...
11+
RED = ...
12+
_generate_next_value_: ClassVar[Callable] = ...
13+
_member_map_: ClassVar[dict] = ...
14+
_member_names_: ClassVar[list] = ...
15+
_member_type_: ClassVar[type[object]] = ...
16+
_unhashable_values_: ClassVar[list] = ...
17+
_use_args_: ClassVar[bool] = ...
18+
_value2member_map_: ClassVar[dict] = ...
19+
_value_repr_: ClassVar[None] = ...
20+
721
class Point:
822
class AngleUnit:
923
__members__: ClassVar[dict] = ... # read-only

test-data/pybind11_fixtures/expected_stubs_with_docs/pybind11_fixtures/demo.pyi

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
1+
import enum
12
import typing
2-
from typing import ClassVar, overload
3+
from typing import Callable, ClassVar, overload
34

45
PI: float
56
__version__: str
67

8+
class Color(enum.Enum):
9+
__new__: ClassVar[Callable] = ...
10+
GREEN = ...
11+
RED = ...
12+
_generate_next_value_: ClassVar[Callable] = ...
13+
_member_map_: ClassVar[dict] = ...
14+
_member_names_: ClassVar[list] = ...
15+
_member_type_: ClassVar[type[object]] = ...
16+
_unhashable_values_: ClassVar[list] = ...
17+
_use_args_: ClassVar[bool] = ...
18+
_value2member_map_: ClassVar[dict] = ...
19+
_value_repr_: ClassVar[None] = ...
20+
721
class Point:
822
class AngleUnit:
923
"""Members:

test-data/pybind11_fixtures/src/main.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include <pybind11/pybind11.h>
5252
#include <pybind11/stl.h>
5353
#include <pybind11/stl/filesystem.h>
54+
#include <pybind11/native_enum.h>
5455

5556
namespace py = pybind11;
5657

@@ -212,6 +213,11 @@ const Point Point::y_axis = Point(0, 1);
212213
Point::LengthUnit Point::length_unit = Point::LengthUnit::mm;
213214
Point::AngleUnit Point::angle_unit = Point::AngleUnit::radian;
214215

216+
enum Color {
217+
RED = 0,
218+
GREEN = 1
219+
};
220+
215221
} // namespace: demo
216222

217223
// Bindings
@@ -230,6 +236,11 @@ void bind_demo(py::module& m) {
230236
py::class_<Point> pyPoint(m, "Point");
231237
py::enum_<Point::LengthUnit> pyLengthUnit(pyPoint, "LengthUnit");
232238
py::enum_<Point::AngleUnit> pyAngleUnit(pyPoint, "AngleUnit");
239+
py::native_enum<Color>(m, "Color", "enum.Enum")
240+
.value("RED", Color::RED)
241+
.value("GREEN", Color::GREEN)
242+
.finalize();
243+
233244

234245
pyPoint
235246
.def(py::init<>())

0 commit comments

Comments
 (0)