From c58758d04915197e16c7c0d8efc3b85c63b765f9 Mon Sep 17 00:00:00 2001 From: Yannick Jadoul Date: Tue, 10 Nov 2020 18:49:42 +0100 Subject: [PATCH] fix: add reasonable argument names to enum_ methods (#2637) * Add argument names to enum_ methods * Add test_enum::test_docstring_signatures --- include/pybind11/pybind11.h | 10 +++++----- tests/test_enum.py | 7 +++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 4cc5445d3..e2ddda020 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1628,7 +1628,7 @@ struct enum_base { strict_behavior; \ return expr; \ }, \ - name(op), is_method(m_base)) + name(op), is_method(m_base), arg("other")) #define PYBIND11_ENUM_OP_CONV(op, expr) \ m_base.attr(op) = cpp_function( \ @@ -1636,7 +1636,7 @@ struct enum_base { int_ a(a_), b(b_); \ return expr; \ }, \ - name(op), is_method(m_base)) + name(op), is_method(m_base), arg("other")) #define PYBIND11_ENUM_OP_CONV_LHS(op, expr) \ m_base.attr(op) = cpp_function( \ @@ -1644,7 +1644,7 @@ struct enum_base { int_ a(a_); \ return expr; \ }, \ - name(op), is_method(m_base)) + name(op), is_method(m_base), arg("other")) if (is_convertible) { PYBIND11_ENUM_OP_CONV_LHS("__eq__", !b.is_none() && a.equal(b)); @@ -1730,7 +1730,7 @@ public: constexpr bool is_convertible = std::is_convertible::value; m_base.init(is_arithmetic, is_convertible); - def(init([](Scalar i) { return static_cast(i); })); + def(init([](Scalar i) { return static_cast(i); }), arg("value")); def("__int__", [](Type value) { return (Scalar) value; }); #if PY_MAJOR_VERSION < 3 def("__long__", [](Type value) { return (Scalar) value; }); @@ -1744,7 +1744,7 @@ public: detail::initimpl::setstate(v_h, static_cast(arg), Py_TYPE(v_h.inst) != v_h.type->type); }, detail::is_new_style_constructor(), - pybind11::name("__setstate__"), is_method(*this)); + pybind11::name("__setstate__"), is_method(*this), arg("state")); } /// Export enumeration entries into the parent scope diff --git a/tests/test_enum.py b/tests/test_enum.py index 385fbbc9e..f6b24fc23 100644 --- a/tests/test_enum.py +++ b/tests/test_enum.py @@ -218,3 +218,10 @@ def test_duplicate_enum_name(): with pytest.raises(ValueError) as excinfo: m.register_bad_enum() assert str(excinfo.value) == 'SimpleEnum: element "ONE" already exists!' + + +def test_docstring_signatures(): + for enum_type in [m.ScopedEnum, m.UnscopedEnum]: + for attr in enum_type.__dict__.values(): + # Issue #2623/PR #2637: Add argument names to enum_ methods + assert "arg0" not in (attr.__doc__ or "")