Add cpp_name_needs_typing_annotated()

This commit is contained in:
Ralf W. Grosse-Kunstleve 2023-10-19 16:18:24 -07:00
parent 794d97e54e
commit 2cafdab279
2 changed files with 15 additions and 1 deletions

View File

@ -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;

View File

@ -113,5 +113,15 @@ struct handle_type_name<typing::Callable<Return(Args...)>> {
+ 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)