Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add argument names to enum_ methods #2637

Merged
merged 2 commits into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 "")