From 7f49381b1224c4cb81d434d2faca3333f24b4aa4 Mon Sep 17 00:00:00 2001 From: Mohataseem Khan Date: Wed, 24 Dec 2025 18:04:53 +0000 Subject: [PATCH 1/6] 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/6] 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); }); } From 3a395e64faf26b51d25c145722b69ba46552aae4 Mon Sep 17 00:00:00 2001 From: Mohataseem Khan Date: Mon, 29 Dec 2025 11:51:58 +0000 Subject: [PATCH 3/6] Fix ambiguous str(kwargs) overload using SFINAE --- include/pybind11/pytypes.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index f240ba92e7..ea16cb9f63 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -1640,10 +1640,12 @@ 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 + // 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); From e79330d8cb08772b8b537262db8f1ce3bb83d105 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 29 Dec 2025 11:52:38 +0000 Subject: [PATCH 4/6] style: pre-commit fixes --- include/pybind11/pytypes.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index ea16cb9f63..e7290fc451 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -1640,11 +1640,9 @@ class str : public object { str(std::u8string_view s) : str(reinterpret_cast(s.data()), s.size()) {} # endif - #endif // Avoid ambiguity when converting from kwargs (GCC) - template ::value, int> = 0> + template ::value, int> = 0> explicit str(const T &k) : str(static_cast(k)) {} explicit str(const bytes &b); From 995ee839c267a8da8cda86488d55cfa16c909a12 Mon Sep 17 00:00:00 2001 From: Mohataseem Khan Date: Mon, 29 Dec 2025 12:21:31 +0000 Subject: [PATCH 5/6] Fix ambiguous py::str(kwargs) overload on GCC --- include/pybind11/pytypes.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index e7290fc451..f637672b31 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -1642,7 +1642,8 @@ class str : public object { #endif // Avoid ambiguity when converting from kwargs (GCC) - template ::value, int> = 0> + template ::value, int> = 0> explicit str(const T &k) : str(static_cast(k)) {} explicit str(const bytes &b); From 311b02131b9fea50ed6cd4360fdc84c48ddcfe59 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 29 Dec 2025 12:27:15 +0000 Subject: [PATCH 6/6] style: pre-commit fixes --- include/pybind11/pytypes.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index f637672b31..e7290fc451 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -1642,8 +1642,7 @@ class str : public object { #endif // Avoid ambiguity when converting from kwargs (GCC) - template ::value, int> = 0> + template ::value, int> = 0> explicit str(const T &k) : str(static_cast(k)) {} explicit str(const bytes &b);