File tree Expand file tree Collapse file tree 4 files changed +47
-4
lines changed
test-data/pybind11_fixtures
expected_stubs_no_docs/pybind11_fixtures
expected_stubs_with_docs/pybind11_fixtures Expand file tree Collapse file tree 4 files changed +47
-4
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 1+ import enum
12import typing
2- from typing import ClassVar , overload
3+ from typing import Callable , ClassVar , overload
34
45PI : 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+
721class Point :
822 class AngleUnit :
923 __members__ : ClassVar [dict ] = ... # read-only
Original file line number Diff line number Diff line change 1+ import enum
12import typing
2- from typing import ClassVar , overload
3+ from typing import Callable , ClassVar , overload
34
45PI : 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+
721class Point :
822 class AngleUnit :
923 """Members:
Original file line number Diff line number Diff line change 5151#include < pybind11/pybind11.h>
5252#include < pybind11/stl.h>
5353#include < pybind11/stl/filesystem.h>
54+ #include < pybind11/native_enum.h>
5455
5556namespace py = pybind11;
5657
@@ -212,6 +213,11 @@ const Point Point::y_axis = Point(0, 1);
212213Point::LengthUnit Point::length_unit = Point::LengthUnit::mm;
213214Point::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<>())
You can’t perform that action at this time.
0 commit comments