pybind11/include/pybind11
Jason Rhinelander e7c9753f1d
feat: allow kw-only args after a py::args (#3402)
* Simply has_kw_only_args handling

This simplifies tracking the number of kw-only args by instead tracking
the number of positional arguments (which is really what we care about
everywhere this is used).

* Allow keyword-only arguments to follow py::args

This removes the constraint that py::args has to be last (or
second-last, with py::kwargs) and instead makes py::args imply
py::kw_only for any remaining arguments, allowing you to bind a function
that works the same way as a Python function such as:

    def f(a, *args, b):
        return a * b + sum(args)

    f(10, 1, 2, 3, b=20)  # == 206

With this change, you can bind such a function using:

    m.def("f", [](int a, py::args args, int b) { /* ... */ },
        "a"_a, "b"_a);

Or, to be more explicit about the keyword-only arguments:

    m.def("g", [](int a, py::args args, int b) { /* ... */ },
        "a"_a, py::kw_only{}, "b"_a);

(The only difference between the two is that the latter will fail at
binding time if the `kw_only{}` doesn't match the `py::args` position).

This doesn't affect backwards compatibility at all because, currently,
you can't have a py::args anywhere except the end/2nd-last.

* Take args/kwargs by const lvalue ref

Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>

Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
2021-10-28 23:16:55 -04:00
..
detail chore: get back to work after 2.8.1 2021-10-27 15:15:11 -07:00
stl maint(clang-tidy): Improve code readability with explicit boolean casts (#3148) 2021-07-27 15:32:26 -07:00
attr.h feat: allow kw-only args after a py::args (#3402) 2021-10-28 23:16:55 -04:00
buffer_info.h Removing MSVC C4800 from pragma block at the top of pybind11.h (#3141) 2021-07-26 13:26:36 -07:00
cast.h feat: allow kw-only args after a py::args (#3402) 2021-10-28 23:16:55 -04:00
chrono.h Accommodating environments that define __STDC_WANT_LIB_EXT1__ even if __STDC_LIB_EXT1__ is not defined by the implementation. (#3151) 2021-07-28 08:58:36 -07:00
common.h Add a dummy common.h header with a deprecation warning 2017-09-06 15:22:26 +02:00
complex.h Change NAMESPACE_* macros into PYBIND11_NAMESPACE_* (#2283) 2020-07-08 18:14:41 -04:00
eigen.h Correct options on Eigen::MappedSparseMatrix & adding MSVC C4127 suppression around Eigen includes. (#3352) 2021-10-11 13:13:01 -07:00
embed.h CodeHealth: Enabling clang-tidy google-explicit-constructor (#3250) 2021-09-08 18:53:38 -07:00
eval.h Update eval.h (#3344) 2021-10-09 11:38:34 -04:00
functional.h fix: MSVC 2017 C++17 on Python 3 regression (#3407) 2021-10-25 16:01:19 -04:00
gil.h Eliminate duplicate TLS keys for loader_life_support stack (#3275) 2021-09-20 04:57:38 -07:00
iostream.h CodeHealth: Enabling clang-tidy google-explicit-constructor (#3250) 2021-09-08 18:53:38 -07:00
numpy.h docs: fix minor typo (#3390) 2021-10-21 10:37:54 -04:00
operators.h Cleanup of file-scoped and globally-scoped warning suppression pragmas across pybind11 header files. (#3201) 2021-08-19 11:37:04 -07:00
options.h Change NAMESPACE_* macros into PYBIND11_NAMESPACE_* (#2283) 2020-07-08 18:14:41 -04:00
pybind11.h feat: allow kw-only args after a py::args (#3402) 2021-10-28 23:16:55 -04:00
pytypes.h Add custom_type_setup attribute (#3287) 2021-09-24 12:08:22 -07:00
stl_bind.h feat: add .keys and .values to bind_map (#3310) 2021-10-01 09:24:36 -04:00
stl.h fix: the types for return_value_policy_override in optional_caster (#3376) 2021-10-25 19:04:45 -07:00