From 7f49381b1224c4cb81d434d2faca3333f24b4aa4 Mon Sep 17 00:00:00 2001 From: Mohataseem Khan Date: Wed, 24 Dec 2025 18:04:53 +0000 Subject: [PATCH 1/2] Fix ambiguous `py::str(kwargs)` overload on GCC --- include/pybind11/pytypes.h | 5 +++++ tests/test_pytypes.cpp | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 0ab0b73e1f..19f5139ea7 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -1640,6 +1640,11 @@ 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..60a9af9992 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -1212,3 +1212,10 @@ 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); + }); +} From 4bbcdeeb6547fd46ecbff1a386302144d07efbcb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 24 Dec 2025 18:10:29 +0000 Subject: [PATCH 2/2] style: pre-commit fixes --- include/pybind11/pytypes.h | 4 +--- tests/test_pytypes.cpp | 5 +---- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 19f5139ea7..f240ba92e7 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -1641,9 +1641,7 @@ class str : public object { # endif // Avoid ambiguity when converting from kwargs (GCC) - explicit str(const kwargs &k) - : str(static_cast(k)) {} - + explicit str(const kwargs &k) : str(static_cast(k)) {} #endif diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 60a9af9992..948101b9f5 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -1213,9 +1213,6 @@ TEST_SUBMODULE(pytypes, m) { }); } - TEST_SUBMODULE(pytypes, kwargs_str) { - m.def("kwargs_to_str", [](py::kwargs kwargs) { - return py::str(kwargs); - }); + m.def("kwargs_to_str", [](py::kwargs kwargs) { return py::str(kwargs); }); }