@@ -303,9 +303,20 @@ class cpp_function : public function {
303303 constexpr bool has_kw_only_args = any_of<std::is_same<kw_only, Extra>...>::value,
304304 has_pos_only_args = any_of<std::is_same<pos_only, Extra>...>::value,
305305 has_arg_annotations = any_of<is_keyword<Extra>...>::value;
306+ constexpr bool has_is_method = any_of<std::is_same<is_method, Extra>...>::value;
307+ // The implicit `self` argument is not present and not counted in method definitions.
308+ constexpr bool has_args = cast_in::args_pos >= 0 ;
309+ constexpr bool is_method_with_self_arg_only = has_is_method && !has_args;
306310 static_assert (has_arg_annotations || !has_kw_only_args,
307311 " py::kw_only requires the use of argument annotations" );
308- static_assert (has_arg_annotations || !has_pos_only_args,
312+ static_assert (((/* Need `py::arg("arg_name")` annotation in function/method. */
313+ has_arg_annotations)
314+ || (/* Allow methods with no arguments `def method(self, /): ...`.
315+ * A method has at least one argument `self`. There can be no
316+ * `py::arg` annotation. E.g. `class.def("method", py::pos_only())`.
317+ */
318+ is_method_with_self_arg_only))
319+ || !has_pos_only_args,
309320 " py::pos_only requires the use of argument annotations (for docstrings "
310321 " and aligning the annotations to the argument)" );
311322
@@ -2361,17 +2372,20 @@ struct enum_base {
23612372 .format (std::move (type_name), enum_name (arg), int_ (arg));
23622373 },
23632374 name (" __repr__" ),
2364- is_method (m_base));
2375+ is_method (m_base),
2376+ pos_only ());
23652377
2366- m_base.attr (" name" ) = property (cpp_function (&enum_name, name (" name" ), is_method (m_base)));
2378+ m_base.attr (" name" )
2379+ = property (cpp_function (&enum_name, name (" name" ), is_method (m_base), pos_only ()));
23672380
23682381 m_base.attr (" __str__" ) = cpp_function (
23692382 [](handle arg) -> str {
23702383 object type_name = type::handle_of (arg).attr (" __name__" );
23712384 return pybind11::str (" {}.{}" ).format (std::move (type_name), enum_name (arg));
23722385 },
23732386 name (" __str__" ),
2374- is_method (m_base));
2387+ is_method (m_base),
2388+ pos_only ());
23752389
23762390 if (options::show_enum_members_docstring ()) {
23772391 m_base.attr (" __doc__" ) = static_property (
@@ -2426,7 +2440,8 @@ struct enum_base {
24262440 }, \
24272441 name (op), \
24282442 is_method (m_base), \
2429- arg (" other" ))
2443+ arg (" other" ), \
2444+ pos_only ())
24302445
24312446#define PYBIND11_ENUM_OP_CONV (op, expr ) \
24322447 m_base.attr (op) = cpp_function ( \
@@ -2436,7 +2451,8 @@ struct enum_base {
24362451 }, \
24372452 name (op), \
24382453 is_method (m_base), \
2439- arg (" other" ))
2454+ arg (" other" ), \
2455+ pos_only ())
24402456
24412457#define PYBIND11_ENUM_OP_CONV_LHS (op, expr ) \
24422458 m_base.attr (op) = cpp_function ( \
@@ -2446,7 +2462,8 @@ struct enum_base {
24462462 }, \
24472463 name (op), \
24482464 is_method (m_base), \
2449- arg (" other" ))
2465+ arg (" other" ), \
2466+ pos_only ())
24502467
24512468 if (is_convertible) {
24522469 PYBIND11_ENUM_OP_CONV_LHS (" __eq__" , !b.is_none () && a.equal (b));
@@ -2466,7 +2483,8 @@ struct enum_base {
24662483 m_base.attr (" __invert__" )
24672484 = cpp_function ([](const object &arg) { return ~(int_ (arg)); },
24682485 name (" __invert__" ),
2469- is_method (m_base));
2486+ is_method (m_base),
2487+ pos_only ());
24702488 }
24712489 } else {
24722490 PYBIND11_ENUM_OP_STRICT (" __eq__" , int_ (a).equal (int_ (b)), return false );
@@ -2486,11 +2504,15 @@ struct enum_base {
24862504#undef PYBIND11_ENUM_OP_CONV
24872505#undef PYBIND11_ENUM_OP_STRICT
24882506
2489- m_base.attr (" __getstate__" ) = cpp_function (
2490- [](const object &arg) { return int_ (arg); }, name (" __getstate__" ), is_method (m_base));
2507+ m_base.attr (" __getstate__" ) = cpp_function ([](const object &arg) { return int_ (arg); },
2508+ name (" __getstate__" ),
2509+ is_method (m_base),
2510+ pos_only ());
24912511
2492- m_base.attr (" __hash__" ) = cpp_function (
2493- [](const object &arg) { return int_ (arg); }, name (" __hash__" ), is_method (m_base));
2512+ m_base.attr (" __hash__" ) = cpp_function ([](const object &arg) { return int_ (arg); },
2513+ name (" __hash__" ),
2514+ is_method (m_base),
2515+ pos_only ());
24942516 }
24952517
24962518 PYBIND11_NOINLINE void value (char const *name_, object value, const char *doc = nullptr ) {
@@ -2582,9 +2604,9 @@ class enum_ : public class_<Type> {
25822604 m_base.init (is_arithmetic, is_convertible);
25832605
25842606 def (init ([](Scalar i) { return static_cast <Type>(i); }), arg (" value" ));
2585- def_property_readonly (" value" , [](Type value) { return (Scalar) value; });
2586- def (" __int__" , [](Type value) { return (Scalar) value; });
2587- def (" __index__" , [](Type value) { return (Scalar) value; });
2607+ def_property_readonly (" value" , [](Type value) { return (Scalar) value; }, pos_only () );
2608+ def (" __int__" , [](Type value) { return (Scalar) value; }, pos_only () );
2609+ def (" __index__" , [](Type value) { return (Scalar) value; }, pos_only () );
25882610 attr (" __setstate__" ) = cpp_function (
25892611 [](detail::value_and_holder &v_h, Scalar arg) {
25902612 detail::initimpl::setstate<Base>(
@@ -2593,7 +2615,8 @@ class enum_ : public class_<Type> {
25932615 detail::is_new_style_constructor (),
25942616 pybind11::name (" __setstate__" ),
25952617 is_method (*this ),
2596- arg (" state" ));
2618+ arg (" state" ),
2619+ pos_only ());
25972620 }
25982621
25992622 // / Export enumeration entries into the parent scope
@@ -2779,7 +2802,8 @@ iterator make_iterator_impl(Iterator first, Sentinel last, Extra &&...extra) {
27792802
27802803 if (!detail::get_type_info (typeid (state), false )) {
27812804 class_<state>(handle (), " iterator" , pybind11::module_local ())
2782- .def (" __iter__" , [](state &s) -> state & { return s; })
2805+ .def (
2806+ " __iter__" , [](state &s) -> state & { return s; }, pos_only ())
27832807 .def (
27842808 " __next__" ,
27852809 [](state &s) -> ValueType {
@@ -2796,6 +2820,7 @@ iterator make_iterator_impl(Iterator first, Sentinel last, Extra &&...extra) {
27962820 // NOLINTNEXTLINE(readability-const-return-type) // PR #3263
27972821 },
27982822 std::forward<Extra>(extra)...,
2823+ pos_only (),
27992824 Policy);
28002825 }
28012826
0 commit comments