@@ -301,9 +301,20 @@ class cpp_function : public function {
301301 constexpr bool has_kw_only_args = any_of<std::is_same<kw_only, Extra>...>::value,
302302 has_pos_only_args = any_of<std::is_same<pos_only, Extra>...>::value,
303303 has_arg_annotations = any_of<is_keyword<Extra>...>::value;
304+ constexpr bool has_is_method = any_of<std::is_same<is_method, Extra>...>::value;
305+ // The implicit `self` argument is not present and not counted in method definitions.
306+ constexpr bool has_args = cast_in::args_pos >= 0 ;
307+ constexpr bool is_method_with_self_arg_only = has_is_method && !has_args;
304308 static_assert (has_arg_annotations || !has_kw_only_args,
305309 " py::kw_only requires the use of argument annotations" );
306- static_assert (has_arg_annotations || !has_pos_only_args,
310+ static_assert (((/* Need `py::arg("arg_name")` annotation in function/method. */
311+ has_arg_annotations)
312+ || (/* Allow methods with no arguments `def method(self, /): ...`.
313+ * A method has at least one argument `self`. There can be no
314+ * `py::arg` annotation. E.g. `class.def("method", py::pos_only())`.
315+ */
316+ is_method_with_self_arg_only))
317+ || !has_pos_only_args,
307318 " py::pos_only requires the use of argument annotations (for docstrings "
308319 " and aligning the annotations to the argument)" );
309320
@@ -2022,17 +2033,20 @@ struct enum_base {
20222033 .format (std::move (type_name), enum_name (arg), int_ (arg));
20232034 },
20242035 name (" __repr__" ),
2025- is_method (m_base));
2036+ is_method (m_base),
2037+ pos_only ());
20262038
2027- m_base.attr (" name" ) = property (cpp_function (&enum_name, name (" name" ), is_method (m_base)));
2039+ m_base.attr (" name" )
2040+ = property (cpp_function (&enum_name, name (" name" ), is_method (m_base), pos_only ()));
20282041
20292042 m_base.attr (" __str__" ) = cpp_function (
20302043 [](handle arg) -> str {
20312044 object type_name = type::handle_of (arg).attr (" __name__" );
20322045 return pybind11::str (" {}.{}" ).format (std::move (type_name), enum_name (arg));
20332046 },
20342047 name (" __str__" ),
2035- is_method (m_base));
2048+ is_method (m_base),
2049+ pos_only ());
20362050
20372051 if (options::show_enum_members_docstring ()) {
20382052 m_base.attr (" __doc__" ) = static_property (
@@ -2087,7 +2101,8 @@ struct enum_base {
20872101 }, \
20882102 name (op), \
20892103 is_method (m_base), \
2090- arg (" other" ))
2104+ arg (" other" ), \
2105+ pos_only ())
20912106
20922107#define PYBIND11_ENUM_OP_CONV (op, expr ) \
20932108 m_base.attr (op) = cpp_function ( \
@@ -2097,7 +2112,8 @@ struct enum_base {
20972112 }, \
20982113 name (op), \
20992114 is_method (m_base), \
2100- arg (" other" ))
2115+ arg (" other" ), \
2116+ pos_only ())
21012117
21022118#define PYBIND11_ENUM_OP_CONV_LHS (op, expr ) \
21032119 m_base.attr (op) = cpp_function ( \
@@ -2107,7 +2123,8 @@ struct enum_base {
21072123 }, \
21082124 name (op), \
21092125 is_method (m_base), \
2110- arg (" other" ))
2126+ arg (" other" ), \
2127+ pos_only ())
21112128
21122129 if (is_convertible) {
21132130 PYBIND11_ENUM_OP_CONV_LHS (" __eq__" , !b.is_none () && a.equal (b));
@@ -2127,7 +2144,8 @@ struct enum_base {
21272144 m_base.attr (" __invert__" )
21282145 = cpp_function ([](const object &arg) { return ~(int_ (arg)); },
21292146 name (" __invert__" ),
2130- is_method (m_base));
2147+ is_method (m_base),
2148+ pos_only ());
21312149 }
21322150 } else {
21332151 PYBIND11_ENUM_OP_STRICT (" __eq__" , int_ (a).equal (int_ (b)), return false );
@@ -2147,11 +2165,15 @@ struct enum_base {
21472165#undef PYBIND11_ENUM_OP_CONV
21482166#undef PYBIND11_ENUM_OP_STRICT
21492167
2150- m_base.attr (" __getstate__" ) = cpp_function (
2151- [](const object &arg) { return int_ (arg); }, name (" __getstate__" ), is_method (m_base));
2168+ m_base.attr (" __getstate__" ) = cpp_function ([](const object &arg) { return int_ (arg); },
2169+ name (" __getstate__" ),
2170+ is_method (m_base),
2171+ pos_only ());
21522172
2153- m_base.attr (" __hash__" ) = cpp_function (
2154- [](const object &arg) { return int_ (arg); }, name (" __hash__" ), is_method (m_base));
2173+ m_base.attr (" __hash__" ) = cpp_function ([](const object &arg) { return int_ (arg); },
2174+ name (" __hash__" ),
2175+ is_method (m_base),
2176+ pos_only ());
21552177 }
21562178
21572179 PYBIND11_NOINLINE void value (char const *name_, object value, const char *doc = nullptr ) {
@@ -2243,9 +2265,9 @@ class enum_ : public class_<Type> {
22432265 m_base.init (is_arithmetic, is_convertible);
22442266
22452267 def (init ([](Scalar i) { return static_cast <Type>(i); }), arg (" value" ));
2246- def_property_readonly (" value" , [](Type value) { return (Scalar) value; });
2247- def (" __int__" , [](Type value) { return (Scalar) value; });
2248- def (" __index__" , [](Type value) { return (Scalar) value; });
2268+ def_property_readonly (" value" , [](Type value) { return (Scalar) value; }, pos_only () );
2269+ def (" __int__" , [](Type value) { return (Scalar) value; }, pos_only () );
2270+ def (" __index__" , [](Type value) { return (Scalar) value; }, pos_only () );
22492271 attr (" __setstate__" ) = cpp_function (
22502272 [](detail::value_and_holder &v_h, Scalar arg) {
22512273 detail::initimpl::setstate<Base>(
@@ -2254,7 +2276,8 @@ class enum_ : public class_<Type> {
22542276 detail::is_new_style_constructor (),
22552277 pybind11::name (" __setstate__" ),
22562278 is_method (*this ),
2257- arg (" state" ));
2279+ arg (" state" ),
2280+ pos_only ());
22582281 }
22592282
22602283 // / Export enumeration entries into the parent scope
@@ -2440,7 +2463,8 @@ iterator make_iterator_impl(Iterator first, Sentinel last, Extra &&...extra) {
24402463
24412464 if (!detail::get_type_info (typeid (state), false )) {
24422465 class_<state>(handle (), " iterator" , pybind11::module_local ())
2443- .def (" __iter__" , [](state &s) -> state & { return s; })
2466+ .def (
2467+ " __iter__" , [](state &s) -> state & { return s; }, pos_only ())
24442468 .def (
24452469 " __next__" ,
24462470 [](state &s) -> ValueType {
@@ -2457,6 +2481,7 @@ iterator make_iterator_impl(Iterator first, Sentinel last, Extra &&...extra) {
24572481 // NOLINTNEXTLINE(readability-const-return-type) // PR #3263
24582482 },
24592483 std::forward<Extra>(extra)...,
2484+ pos_only (),
24602485 Policy);
24612486 }
24622487
0 commit comments