From 5ddc551bc48b30342bdb02a855c6a05267d4c322 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 2 Apr 2026 21:40:11 -0500 Subject: [PATCH] py/compile: Reject *arg after keyword argument. Document this in cpydiff and add a test with expected output for coverage testing. As discussed in #11441, the code growth from handling this case seems to outweigh the benefit of implementing it properly. Closes: #11439 Signed-off-by: Jeff Epler --- py/compile.c | 8 ++++++++ tests/basics/fun_callstar_kwarg.py | 8 ++++++++ tests/basics/fun_callstar_kwarg.py.exp | 1 + tests/cpydiff/core_function_star.py | 16 ++++++++++++++++ 4 files changed, 33 insertions(+) create mode 100644 tests/basics/fun_callstar_kwarg.py create mode 100644 tests/basics/fun_callstar_kwarg.py.exp create mode 100644 tests/cpydiff/core_function_star.py diff --git a/py/compile.c b/py/compile.c index 72dad8dc31..37a3e6d32c 100644 --- a/py/compile.c +++ b/py/compile.c @@ -2421,6 +2421,14 @@ static void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_ar compile_syntax_error(comp, (mp_parse_node_t)pns_arg, MP_ERROR_TEXT("* arg after **")); return; } + if (n_keyword) { + // Support for *arg after kwarg is a CPython feature omitted + // from MicroPython in order to reduce code size. See + // https://github.com/micropython/micropython/issues/11439 for + // more info. + compile_syntax_error(comp, (mp_parse_node_t)pns_arg, MP_ERROR_TEXT("* arg after kwarg")); + return; + } #if MICROPY_DYNAMIC_COMPILER if (i >= (size_t)mp_dynamic_compiler.small_int_bits - 1) #else diff --git a/tests/basics/fun_callstar_kwarg.py b/tests/basics/fun_callstar_kwarg.py new file mode 100644 index 0000000000..15bc19e216 --- /dev/null +++ b/tests/basics/fun_callstar_kwarg.py @@ -0,0 +1,8 @@ +# A CPython syntax feature not supported by MicroPython. For more information +# see `this issue `_. +# and tests/cpydiff/core_function_star.py + +try: + exec("f(y=1, *(3,))") +except SyntaxError as e: + print("SyntaxError") diff --git a/tests/basics/fun_callstar_kwarg.py.exp b/tests/basics/fun_callstar_kwarg.py.exp new file mode 100644 index 0000000000..8729fc4343 --- /dev/null +++ b/tests/basics/fun_callstar_kwarg.py.exp @@ -0,0 +1 @@ +SyntaxError diff --git a/tests/cpydiff/core_function_star.py b/tests/cpydiff/core_function_star.py new file mode 100644 index 0000000000..26f5effd4e --- /dev/null +++ b/tests/cpydiff/core_function_star.py @@ -0,0 +1,16 @@ +""" +categories: Core,Functions +description: ``*args`` cannot follow a keyword argument +cause: MicroPython is optimised for code space. For more information see `this issue `_. +workaround: Re-order the arguments +""" + + +def f(x, y): + return x + y + + +try: + print(f(y=1, *(3,))) +except Exception as e: + print(e)