diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 0ab0b73e1f..f240ba92e7 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -1640,6 +1640,9 @@ class str : public object { str(std::u8string_view s) : str(reinterpret_cast(s.data()), s.size()) {} # endif + // Avoid ambiguity when converting from kwargs (GCC) + explicit str(const kwargs &k) : str(static_cast(k)) {} + #endif explicit str(const bytes &b); diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 7d5423e549..948101b9f5 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -1212,3 +1212,7 @@ TEST_SUBMODULE(pytypes, m) { return py::isinstance(x); }); } + +TEST_SUBMODULE(pytypes, kwargs_str) { + m.def("kwargs_to_str", [](py::kwargs kwargs) { return py::str(kwargs); }); +}