Skip to content

Commit

Permalink
Merge pull request #45 from apple1417/master
Browse files Browse the repository at this point in the history
fix error message when missing a single required argument
  • Loading branch information
apple1417 authored Sep 5, 2024
2 parents 6b589e8 + 136f318 commit b7b33fe
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions src/pyunrealsdk/unreal_bindings/bound_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<FName>& 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,
Expand Down Expand Up @@ -86,16 +115,7 @@ std::pair<UProperty*, std::vector<UProperty*>> 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()) {
Expand Down

0 comments on commit b7b33fe

Please sign in to comment.