diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 1806583e6..d71068bae 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -101,6 +101,31 @@ inline std::string replace_newlines_and_squash(const char *text) { # define PYBIND11_COMPAT_STRDUP strdup #endif +/// Type trait checker for `descr`. +template +struct is_descr : std::false_type {}; + +template +struct is_descr> : std::true_type {}; + +template +struct is_descr> : std::true_type {}; + +/// Checks for `return_name` in `type_caster` to replace `name` for return type hints. +/// this is useful for having a different python type hint for args vs return value, +/// e.g., `std::filesystem::path` -> Arg: `Union[os.PathLike, str, bytes]`, return: `Path`. +template +struct as_return_type +{ + static constexpr auto name = T::name; +}; + +template +struct as_return_type::value>> +{ + static constexpr auto name = T::return_name; +}; + PYBIND11_NAMESPACE_END(detail) /// Wraps an arbitrary C++ function/method/lambda function/.. into a callable Python object @@ -320,7 +345,8 @@ protected: /* Generate a readable signature describing the function's arguments and return value types */ static constexpr auto signature - = const_name("(") + cast_in::arg_names + const_name(") -> ") + cast_out::name; + = const_name("(") + cast_in::arg_names + const_name(") -> ") + + as_return_type::name; PYBIND11_DESCR_CONSTEXPR auto types = decltype(signature)::types(); /* Register the function with Python from generic (non-templated) code */