From 136f31869de1fb668d5628432eeb04ec24c16b50 Mon Sep 17 00:00:00 2001 From: apple1417 Date: Sat, 31 Aug 2024 14:52:51 +1200 Subject: [PATCH] fix error message when missing a single required argument --- .../unreal_bindings/bound_function.cpp | 40 ++++++++++++++----- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/src/pyunrealsdk/unreal_bindings/bound_function.cpp b/src/pyunrealsdk/unreal_bindings/bound_function.cpp index b17bb42..f66bbd7 100644 --- a/src/pyunrealsdk/unreal_bindings/bound_function.cpp +++ b/src/pyunrealsdk/unreal_bindings/bound_function.cpp @@ -18,6 +18,35 @@ namespace pyunrealsdk::unreal { namespace { +/** + * @brief Throws a type error for missing required positional arguments. + * + * @param func_name The name of the function which was called. + * @param missing_required_args The names of the missing required args, + */ +[[noreturn]] void throw_missing_required_args(FName func_name, + const std::vector& missing_required_args) { + auto num_missing = missing_required_args.size(); + + std::ostringstream stream{}; + stream << func_name << "() missing " << num_missing << " required positional argument"; + if (num_missing > 1) { + stream << 's'; + } + stream << ": "; + + for (size_t i = 0; i < num_missing - 1; i++) { + stream << '\'' << missing_required_args[i] << "', "; + } + if (num_missing > 1) { + stream << "and "; + } + + stream << '\'' << missing_required_args.back() << '\''; + + throw py::type_error(stream.str()); +} + /** * @brief Fills the params struct for a function with args from python. * @note While this is similar to `make_struct`, we need to do some extra processing on the params, @@ -86,16 +115,7 @@ std::pair> fill_py_params(WrappedStruct& par } if (!missing_required_args.empty()) { - std::ostringstream stream{}; - stream << params.type->Name << "() missing " << missing_required_args.size() - << " required positional arguments: "; - - for (size_t i = 0; i < missing_required_args.size() - 1; i++) { - stream << '\'' << missing_required_args[i] << "', "; - } - stream << "and '" << missing_required_args.back() << '\''; - - throw py::type_error(stream.str()); + throw_missing_required_args(params.type->Name, missing_required_args); } if (!kwargs.empty()) {