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'
```
This commit is contained in:
Jeff Epler 2023-09-15 10:39:14 -05:00
parent ef9f75ead1
commit a8b81ce943
No known key found for this signature in database
GPG Key ID: D5BF15AB975AB4DE

View File

@ -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
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
mp_arg_error_terse_mismatch();
#else
// TODO better error message
#if CIRCUITPY_FULL_BUILD
mp_map_elem_t *elem = kws->table;
size_t alloc = kws->alloc;
for (size_t i = 0; i < alloc; i++) {
mp_obj_t key = elem[i].key;
if (key == MP_OBJ_NULL) {
continue;
}
bool seen = false;
for (size_t j = n_pos; j < n_allowed; j++) {
if (mp_obj_equal(MP_OBJ_NEW_QSTR(allowed[j].qst), key)) {
seen = true;
break;
}
}
if (!seen) {
mp_raise_msg_varg(&mp_type_TypeError,
MP_ERROR_TEXT("unexpected keyword argument '%q'"), mp_obj_str_get_qstr(key));
}
}
#endif
// (for the !FULL_BUILD case, and as a fallthrough for the FULL_BUILD case, even though it SHOULD be unreachable in that case)
mp_raise_TypeError(MP_ERROR_TEXT("extra keyword arguments given"));
#endif
}