diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 0ab0b73e1f..e7290fc451 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -1641,6 +1641,9 @@ class str : public object { # endif #endif + // Avoid ambiguity when converting from kwargs (GCC) + template ::value, int> = 0> + explicit str(const T &k) : str(static_cast(k)) {} 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); }); +}