Skip to content

Commit a8b81ce

Browse files
committed
Enhance unexpected keyword argument detection
Before, incorrect use when calling a core function would just say "extra keyword arguments given"; now, it will name the argument: ```python >>> Synthesizer(bad_kwarg="boo") TypeError: unexpected keyword argument 'bad_kwarg' ```
1 parent ef9f75e commit a8b81ce

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

py/argcheck.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,28 @@ void mp_arg_parse_all(size_t n_pos, const mp_obj_t *pos, mp_map_t *kws, size_t n
133133
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
134134
mp_arg_error_terse_mismatch();
135135
#else
136-
// TODO better error message
136+
#if CIRCUITPY_FULL_BUILD
137+
mp_map_elem_t *elem = kws->table;
138+
size_t alloc = kws->alloc;
139+
for (size_t i = 0; i < alloc; i++) {
140+
mp_obj_t key = elem[i].key;
141+
if (key == MP_OBJ_NULL) {
142+
continue;
143+
}
144+
bool seen = false;
145+
for (size_t j = n_pos; j < n_allowed; j++) {
146+
if (mp_obj_equal(MP_OBJ_NEW_QSTR(allowed[j].qst), key)) {
147+
seen = true;
148+
break;
149+
}
150+
}
151+
if (!seen) {
152+
mp_raise_msg_varg(&mp_type_TypeError,
153+
MP_ERROR_TEXT("unexpected keyword argument '%q'"), mp_obj_str_get_qstr(key));
154+
}
155+
}
156+
#endif
157+
// (for the !FULL_BUILD case, and as a fallthrough for the FULL_BUILD case, even though it SHOULD be unreachable in that case)
137158
mp_raise_TypeError(MP_ERROR_TEXT("extra keyword arguments given"));
138159
#endif
139160
}

0 commit comments

Comments
 (0)