diff --git a/mypyc/lib-rt/getargs.c b/mypyc/lib-rt/getargs.c index 4f2f8aa0be83..163b9ac2b163 100644 --- a/mypyc/lib-rt/getargs.c +++ b/mypyc/lib-rt/getargs.c @@ -250,13 +250,12 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format, current_arg = Py_NewRef(PyTuple_GET_ITEM(args, i)); } else if (nkwargs && i >= pos) { - int res = PyDict_GetItemStringRef(kwargs, kwlist[i], ¤t_arg); - if (res == 1) { - --nkwargs; - } - else if (res == -1) { + if (unlikely(PyDict_GetItemStringRef(kwargs, kwlist[i], ¤t_arg) < 0)) { return 0; } + if (current_arg) { + --nkwargs; + } } else { current_arg = NULL; @@ -371,11 +370,12 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format, Py_ssize_t j; /* make sure there are no arguments given by name and position */ for (i = pos; i < bound_pos_args && i < len; i++) { - int res = PyDict_GetItemStringRef(kwargs, kwlist[i], ¤t_arg); - if (res == 1) { - Py_DECREF(current_arg); + PyObject *current_arg; + if (unlikely(PyDict_GetItemStringRef(kwargs, kwlist[i], ¤t_arg) < 0)) { + goto latefail; } - else if (unlikely(res == 0)) { + if (unlikely(current_arg != NULL)) { + Py_DECREF(current_arg); /* arg present in tuple and in dict */ PyErr_Format(PyExc_TypeError, "argument for %.200s%s given by name ('%s') " @@ -385,9 +385,6 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format, kwlist[i], i+1); goto latefail; } - else if (unlikely(res == -1)) { - goto latefail; - } } /* make sure there are no extraneous keyword arguments */ j = 0; diff --git a/mypyc/test-data/run-classes.test b/mypyc/test-data/run-classes.test index cf30bddbef64..387f91e489ee 100644 --- a/mypyc/test-data/run-classes.test +++ b/mypyc/test-data/run-classes.test @@ -467,6 +467,21 @@ a = A(10) assert a.foo() == 11 assert foo() == 21 +[case testClassKwargs] +class X: + def __init__(self, msg: str, **variables: int) -> None: + pass +[file driver.py] +import traceback +from native import X +X('hello', a=0) +try: + X('hello', msg='hello') +except TypeError as e: + print(f"{type(e).__name__}: {e}") +[out] +TypeError: argument for __init__() given by name ('msg') and position (1) + [case testGenericClass] from typing import TypeVar, Generic, Sequence T = TypeVar('T')