Fix missing pythonic type hints for native_enum (#5619)

* fix missing pythonic type hints for native_enum

* Fix __qualname__ in native_enum

* Rename `enum class native` → `func_sig_rendering`. Add to `ENUM_TYPES_AND_MEMBERS`. Move new code around to fit in more organically.

"native" is used in so many places here, it would be very difficult to pin-point where the specific enum type is used.

Similarly, "value" is difficult to pin-point. Changed to "nested_value".

---------

Co-authored-by: Bryn Lloyd <12702862+dyollb@users.noreply.github.com>
Co-authored-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
This commit is contained in:
Bryn Lloyd
2025-04-16 06:38:15 +02:00
committed by GitHub
parent 3c586340fb
commit 223e2e9da2
4 changed files with 44 additions and 10 deletions

View File

@@ -54,6 +54,8 @@ MEMBER_DOC_MEMBERS = (
("mem2", 2),
)
FUNC_SIG_RENDERING_MEMBERS = ()
ENUM_TYPES_AND_MEMBERS = (
(m.smallenum, SMALLENUM_MEMBERS),
(m.color, COLOR_MEMBERS),
@@ -62,6 +64,7 @@ ENUM_TYPES_AND_MEMBERS = (
(m.flags_uint, FLAGS_UINT_MEMBERS),
(m.export_values, EXPORT_VALUES_MEMBERS),
(m.member_doc, MEMBER_DOC_MEMBERS),
(m.func_sig_rendering, FUNC_SIG_RENDERING_MEMBERS),
(m.class_with_enum.in_class, CLASS_WITH_ENUM_IN_CLASS_MEMBERS),
)
@@ -84,15 +87,10 @@ def test_enum_members(enum_type, members):
def test_pickle_roundtrip(enum_type, members):
for name, _ in members:
orig = enum_type[name]
if enum_type is m.class_with_enum.in_class:
# This is a general pickle limitation.
with pytest.raises(pickle.PicklingError):
pickle.dumps(orig)
else:
# This only works if __module__ is correct.
serialized = pickle.dumps(orig)
restored = pickle.loads(serialized)
assert restored == orig
# This only works if __module__ is correct.
serialized = pickle.dumps(orig)
restored = pickle.loads(serialized)
assert restored == orig
@pytest.mark.parametrize("enum_type", [m.flags_uchar, m.flags_uint])
@@ -139,7 +137,7 @@ def test_pass_color_success():
def test_pass_color_fail():
with pytest.raises(TypeError) as excinfo:
m.pass_color(None)
assert "test_native_enum::color" in str(excinfo.value)
assert "pybind11_tests.native_enum.color" in str(excinfo.value)
def test_return_color_success():
@@ -155,6 +153,22 @@ def test_return_color_fail():
assert str(excinfo_cast.value) == str(excinfo_direct.value)
def test_property_type_hint():
prop = m.class_with_enum.__dict__["nested_value"]
assert isinstance(prop, property)
assert prop.fget.__doc__.startswith(
"(self: pybind11_tests.native_enum.class_with_enum)"
" -> pybind11_tests.native_enum.class_with_enum.in_class"
)
def test_func_sig_rendering():
assert m.pass_and_return_func_sig_rendering.__doc__.startswith(
"pass_and_return_func_sig_rendering(e: pybind11_tests.native_enum.func_sig_rendering)"
" -> pybind11_tests.native_enum.func_sig_rendering"
)
def test_type_caster_enum_type_enabled_false():
# This is really only a "does it compile" test.
assert m.pass_some_proto_enum(None) is None