Skip to content

Commit 204f70f

Browse files
committed
python: Fix type_name_short to return type name without module
1 parent e0bcfab commit 204f70f

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

bindings/python/src/helpers.hpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,28 @@
77
#include <nanobind/eigen/dense.h>
88
#include <nanobind/eigen/sparse.h>
99

10+
#include <string_view>
11+
1012
namespace proxsuite {
1113
namespace common {
1214
namespace python {
1315
namespace detail {
14-
inline auto
16+
inline nanobind::str
1517
type_name_short(nanobind::handle h)
1618
{
1719
namespace nb = nanobind;
1820
assert(h.is_type());
19-
return nb::type_name(h);
21+
// nb::type_name return the type_name with modules.
22+
// In the next step, we will trim the modules to only keep the type name.
23+
auto str = nb::type_name(h);
24+
std::string_view work_str(str.c_str());
25+
auto dot_index = work_str.find_last_of('.');
26+
if (dot_index == std::string_view::npos) {
27+
return str;
28+
} else {
29+
return nb::str(work_str.data() + dot_index + 1);
30+
}
31+
// return nb::str(dot_it++, str.end());
2032
}
2133
} // namespace detail
2234

0 commit comments

Comments
 (0)