Skip to content

Commit

Permalink
Add cpp_name_needs_typing_annotated()
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Oct 19, 2023
1 parent 794d97e commit 2cafdab
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,11 @@ class cpp_function : public function {
} 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;
Expand Down
10 changes: 10 additions & 0 deletions include/pybind11/typing.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 2cafdab

Please sign in to comment.