Skip to content

Commit

Permalink
fix: add reasonable argument names to enum_ methods (#2637)
Browse files Browse the repository at this point in the history
* Add argument names to enum_ methods

* Add test_enum::test_docstring_signatures
  • Loading branch information
YannickJadoul authored Nov 10, 2020
1 parent b72cebe commit c58758d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
10 changes: 5 additions & 5 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -1628,23 +1628,23 @@ 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( \
[](object a_, object b_) { \
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( \
[](object a_, object b) { \
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));
Expand Down Expand Up @@ -1730,7 +1730,7 @@ template <typename Type> class enum_ : public class_<Type> {
constexpr bool is_convertible = std::is_convertible<Type, Scalar>::value;
m_base.init(is_arithmetic, is_convertible);

def(init([](Scalar i) { return static_cast<Type>(i); }));
def(init([](Scalar i) { return static_cast<Type>(i); }), arg("value"));
def("__int__", [](Type value) { return (Scalar) value; });
#if PY_MAJOR_VERSION < 3
def("__long__", [](Type value) { return (Scalar) value; });
Expand All @@ -1744,7 +1744,7 @@ template <typename Type> class enum_ : public class_<Type> {
detail::initimpl::setstate<Base>(v_h, static_cast<Type>(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
Expand Down
7 changes: 7 additions & 0 deletions tests/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "")

0 comments on commit c58758d

Please sign in to comment.