Skip to content

Commit b7b33fe

Browse files
authored
Merge pull request #45 from apple1417/master
fix error message when missing a single required argument
2 parents 6b589e8 + 136f318 commit b7b33fe

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

src/pyunrealsdk/unreal_bindings/bound_function.cpp

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,35 @@ namespace pyunrealsdk::unreal {
1818

1919
namespace {
2020

21+
/**
22+
* @brief Throws a type error for missing required positional arguments.
23+
*
24+
* @param func_name The name of the function which was called.
25+
* @param missing_required_args The names of the missing required args,
26+
*/
27+
[[noreturn]] void throw_missing_required_args(FName func_name,
28+
const std::vector<FName>& missing_required_args) {
29+
auto num_missing = missing_required_args.size();
30+
31+
std::ostringstream stream{};
32+
stream << func_name << "() missing " << num_missing << " required positional argument";
33+
if (num_missing > 1) {
34+
stream << 's';
35+
}
36+
stream << ": ";
37+
38+
for (size_t i = 0; i < num_missing - 1; i++) {
39+
stream << '\'' << missing_required_args[i] << "', ";
40+
}
41+
if (num_missing > 1) {
42+
stream << "and ";
43+
}
44+
45+
stream << '\'' << missing_required_args.back() << '\'';
46+
47+
throw py::type_error(stream.str());
48+
}
49+
2150
/**
2251
* @brief Fills the params struct for a function with args from python.
2352
* @note While this is similar to `make_struct`, we need to do some extra processing on the params,
@@ -86,16 +115,7 @@ std::pair<UProperty*, std::vector<UProperty*>> fill_py_params(WrappedStruct& par
86115
}
87116

88117
if (!missing_required_args.empty()) {
89-
std::ostringstream stream{};
90-
stream << params.type->Name << "() missing " << missing_required_args.size()
91-
<< " required positional arguments: ";
92-
93-
for (size_t i = 0; i < missing_required_args.size() - 1; i++) {
94-
stream << '\'' << missing_required_args[i] << "', ";
95-
}
96-
stream << "and '" << missing_required_args.back() << '\'';
97-
98-
throw py::type_error(stream.str());
118+
throw_missing_required_args(params.type->Name, missing_required_args);
99119
}
100120

101121
if (!kwargs.empty()) {

0 commit comments

Comments
 (0)