Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix error message when missing a single required argument #45

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading