From 2cafdab279d080838b2219f5d66daaeb1a2fe1ba Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 19 Oct 2023 16:18:24 -0700 Subject: [PATCH] Add `cpp_name_needs_typing_annotated()` --- include/pybind11/pybind11.h | 6 +++++- include/pybind11/typing.h | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 0b4f46f6d..35f5c13ba 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -493,7 +493,11 @@ protected: } else { std::string tname(t->name()); detail::clean_type_id(tname); - signature += "Annotated[Any, \"" + tname + "\"]"; + if (detail::cpp_name_needs_typing_annotated(tname.c_str())) { + signature += "Annotated[Any, \"" + tname + "\"]"; + } else { + signature += tname; + } } } else { signature += c; diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index b7b1a4e54..6c2bea28a 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -113,5 +113,15 @@ struct handle_type_name> { + const_name("]"); }; +inline bool cpp_name_needs_typing_annotated(const char *cpp_name) { + while (*cpp_name) { + char c = *cpp_name++; + if (c == ':' || c == '<') { // Assuming valid names, there is no need to check for '>'. + return true; + } + } + return false; +} + PYBIND11_NAMESPACE_END(detail) PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)