From 233b9a683f98fd6dc6277489b802885961d4b739 Mon Sep 17 00:00:00 2001 From: Uno Date: Fri, 15 Aug 2025 12:34:51 +0900 Subject: [PATCH 1/2] fix: error msg for *args, **kwargs default value --- Grammar/python.gram | 8 ++++---- Lib/test/test_syntax.py | 12 ++++++------ .../2025-08-15-12-31-45.gh-issue-137786.HkKBqf.rst | 2 ++ Parser/parser.c | 8 ++++---- 4 files changed, 16 insertions(+), 14 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-08-15-12-31-45.gh-issue-137786.HkKBqf.rst diff --git a/Grammar/python.gram b/Grammar/python.gram index ff54e42111005a..3da69fe152448b 100644 --- a/Grammar/python.gram +++ b/Grammar/python.gram @@ -1331,11 +1331,11 @@ invalid_default: invalid_star_etc: | a='*' (')' | ',' (')' | '**')) { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "named parameters must follow bare *") } | '*' ',' TYPE_COMMENT { RAISE_SYNTAX_ERROR("bare * has associated type comment") } - | '*' param a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "var-positional parameter cannot have default value") } + | '*' param a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "var-positional parameter's default value cannot be changed because it has the immutable default value ()") } | '*' (param_no_default | ',') param_maybe_default* a='*' (param_no_default | ',') { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "* may appear only once") } invalid_kwds: - | '**' param a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "var-keyword parameter cannot have default value") } + | '**' param a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "var-keyword parameter's default value cannot be changed because it has the immutable default value {}") } | '**' param ',' a=param { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "parameters cannot follow var-keyword parameter") } | '**' param ',' a[Token*]=('*'|'**'|'/') { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "parameters cannot follow var-keyword parameter") } invalid_parameters_helper: # This is only there to avoid type errors @@ -1359,11 +1359,11 @@ invalid_lambda_parameters_helper: | lambda_param_with_default+ invalid_lambda_star_etc: | '*' (':' | ',' (':' | '**')) { RAISE_SYNTAX_ERROR("named parameters must follow bare *") } - | '*' lambda_param a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "var-positional parameter cannot have default value") } + | '*' lambda_param a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "var-positional parameter's default value cannot be changed because it has the immutable default value ()") } | '*' (lambda_param_no_default | ',') lambda_param_maybe_default* a='*' (lambda_param_no_default | ',') { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "* may appear only once") } invalid_lambda_kwds: - | '**' lambda_param a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "var-keyword parameter cannot have default value") } + | '**' lambda_param a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "var-keyword parameter's default value cannot be changed because it has the immutable default value {}") } | '**' lambda_param ',' a=lambda_param { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "parameters cannot follow var-keyword parameter") } | '**' lambda_param ',' a[Token*]=('*'|'**'|'/') { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "parameters cannot follow var-keyword parameter") } invalid_double_type_comments: diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py index c52d24219410c2..8046dc58425106 100644 --- a/Lib/test/test_syntax.py +++ b/Lib/test/test_syntax.py @@ -461,22 +461,22 @@ >>> def foo(a,*b=3,c): ... pass Traceback (most recent call last): -SyntaxError: var-positional parameter cannot have default value +SyntaxError: var-positional parameter's default value cannot be changed because it has the immutable default value () >>> def foo(a,*b: int=,c): ... pass Traceback (most recent call last): -SyntaxError: var-positional parameter cannot have default value +SyntaxError: var-positional parameter's default value cannot be changed because it has the immutable default value () >>> def foo(a,**b=3): ... pass Traceback (most recent call last): -SyntaxError: var-keyword parameter cannot have default value +SyntaxError: var-keyword parameter's default value cannot be changed because it has the immutable default value {} >>> def foo(a,**b: int=3): ... pass Traceback (most recent call last): -SyntaxError: var-keyword parameter cannot have default value +SyntaxError: var-keyword parameter's default value cannot be changed because it has the immutable default value {} >>> def foo(a,*a, b, **c, d): ... pass @@ -577,11 +577,11 @@ >>> lambda a,*b=3,c: None Traceback (most recent call last): -SyntaxError: var-positional parameter cannot have default value +SyntaxError: var-positional parameter's default value cannot be changed because it has the immutable default value () >>> lambda a,**b=3: None Traceback (most recent call last): -SyntaxError: var-keyword parameter cannot have default value +SyntaxError: var-keyword parameter's default value cannot be changed because it has the immutable default value {} >>> lambda a, *a, b, **c, d: None Traceback (most recent call last): diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-08-15-12-31-45.gh-issue-137786.HkKBqf.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-08-15-12-31-45.gh-issue-137786.HkKBqf.rst new file mode 100644 index 00000000000000..01e02c18e64a06 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-08-15-12-31-45.gh-issue-137786.HkKBqf.rst @@ -0,0 +1,2 @@ +fix error msg for *args, **kwargs default value +Yoonho Hann. diff --git a/Parser/parser.c b/Parser/parser.c index de5cdc9b6f7f34..e5db365345e8bd 100644 --- a/Parser/parser.c +++ b/Parser/parser.c @@ -22710,7 +22710,7 @@ invalid_star_etc_rule(Parser *p) ) { D(fprintf(stderr, "%*c+ invalid_star_etc[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'*' param '='")); - _res = RAISE_SYNTAX_ERROR_KNOWN_LOCATION ( a , "var-positional parameter cannot have default value" ); + _res = RAISE_SYNTAX_ERROR_KNOWN_LOCATION ( a , "var-positional parameter's default value cannot be changed because it has the immutable default value ()" ); if (_res == NULL && PyErr_Occurred()) { p->error_indicator = 1; p->level--; @@ -22795,7 +22795,7 @@ invalid_kwds_rule(Parser *p) ) { D(fprintf(stderr, "%*c+ invalid_kwds[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'**' param '='")); - _res = RAISE_SYNTAX_ERROR_KNOWN_LOCATION ( a , "var-keyword parameter cannot have default value" ); + _res = RAISE_SYNTAX_ERROR_KNOWN_LOCATION ( a , "var-keyword parameter's default value cannot be changed because it has the immutable default value {}" ); if (_res == NULL && PyErr_Occurred()) { p->error_indicator = 1; p->level--; @@ -23289,7 +23289,7 @@ invalid_lambda_star_etc_rule(Parser *p) ) { D(fprintf(stderr, "%*c+ invalid_lambda_star_etc[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'*' lambda_param '='")); - _res = RAISE_SYNTAX_ERROR_KNOWN_LOCATION ( a , "var-positional parameter cannot have default value" ); + _res = RAISE_SYNTAX_ERROR_KNOWN_LOCATION ( a , "var-positional parameter's default value cannot be changed because it has the immutable default value ()" ); if (_res == NULL && PyErr_Occurred()) { p->error_indicator = 1; p->level--; @@ -23377,7 +23377,7 @@ invalid_lambda_kwds_rule(Parser *p) ) { D(fprintf(stderr, "%*c+ invalid_lambda_kwds[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'**' lambda_param '='")); - _res = RAISE_SYNTAX_ERROR_KNOWN_LOCATION ( a , "var-keyword parameter cannot have default value" ); + _res = RAISE_SYNTAX_ERROR_KNOWN_LOCATION ( a , "var-keyword parameter's default value cannot be changed because it has the immutable default value {}" ); if (_res == NULL && PyErr_Occurred()) { p->error_indicator = 1; p->level--; From 98cbe835da54e301889ce3132a647af6363793b0 Mon Sep 17 00:00:00 2001 From: Uno Date: Fri, 15 Aug 2025 15:21:07 +0900 Subject: [PATCH 2/2] fix: add explanation without modifying the original message --- Grammar/python.gram | 8 ++++---- Lib/test/test_syntax.py | 12 ++++++------ Parser/parser.c | 8 ++++---- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Grammar/python.gram b/Grammar/python.gram index 3da69fe152448b..12c06cb5b8b5fb 100644 --- a/Grammar/python.gram +++ b/Grammar/python.gram @@ -1331,11 +1331,11 @@ invalid_default: invalid_star_etc: | a='*' (')' | ',' (')' | '**')) { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "named parameters must follow bare *") } | '*' ',' TYPE_COMMENT { RAISE_SYNTAX_ERROR("bare * has associated type comment") } - | '*' param a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "var-positional parameter's default value cannot be changed because it has the immutable default value ()") } + | '*' param a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "var-positional parameter cannot have default value, pass values explicitly") } | '*' (param_no_default | ',') param_maybe_default* a='*' (param_no_default | ',') { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "* may appear only once") } invalid_kwds: - | '**' param a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "var-keyword parameter's default value cannot be changed because it has the immutable default value {}") } + | '**' param a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "var-keyword parameter cannot have default value, pass values explicitly") } | '**' param ',' a=param { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "parameters cannot follow var-keyword parameter") } | '**' param ',' a[Token*]=('*'|'**'|'/') { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "parameters cannot follow var-keyword parameter") } invalid_parameters_helper: # This is only there to avoid type errors @@ -1359,11 +1359,11 @@ invalid_lambda_parameters_helper: | lambda_param_with_default+ invalid_lambda_star_etc: | '*' (':' | ',' (':' | '**')) { RAISE_SYNTAX_ERROR("named parameters must follow bare *") } - | '*' lambda_param a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "var-positional parameter's default value cannot be changed because it has the immutable default value ()") } + | '*' lambda_param a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "var-positional parameter cannot have default value, pass values explicitly") } | '*' (lambda_param_no_default | ',') lambda_param_maybe_default* a='*' (lambda_param_no_default | ',') { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "* may appear only once") } invalid_lambda_kwds: - | '**' lambda_param a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "var-keyword parameter's default value cannot be changed because it has the immutable default value {}") } + | '**' lambda_param a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "var-keyword parameter cannot have default value, pass values explicitly") } | '**' lambda_param ',' a=lambda_param { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "parameters cannot follow var-keyword parameter") } | '**' lambda_param ',' a[Token*]=('*'|'**'|'/') { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "parameters cannot follow var-keyword parameter") } invalid_double_type_comments: diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py index 8046dc58425106..bc2070d5bfbb67 100644 --- a/Lib/test/test_syntax.py +++ b/Lib/test/test_syntax.py @@ -461,22 +461,22 @@ >>> def foo(a,*b=3,c): ... pass Traceback (most recent call last): -SyntaxError: var-positional parameter's default value cannot be changed because it has the immutable default value () +SyntaxError: var-positional parameter cannot have default value, pass values explicitly >>> def foo(a,*b: int=,c): ... pass Traceback (most recent call last): -SyntaxError: var-positional parameter's default value cannot be changed because it has the immutable default value () +SyntaxError: var-positional parameter cannot have default value, pass values explicitly >>> def foo(a,**b=3): ... pass Traceback (most recent call last): -SyntaxError: var-keyword parameter's default value cannot be changed because it has the immutable default value {} +SyntaxError: var-keyword parameter cannot have default value, pass values explicitly >>> def foo(a,**b: int=3): ... pass Traceback (most recent call last): -SyntaxError: var-keyword parameter's default value cannot be changed because it has the immutable default value {} +SyntaxError: var-keyword parameter cannot have default value, pass values explicitly >>> def foo(a,*a, b, **c, d): ... pass @@ -577,11 +577,11 @@ >>> lambda a,*b=3,c: None Traceback (most recent call last): -SyntaxError: var-positional parameter's default value cannot be changed because it has the immutable default value () +SyntaxError: var-positional parameter cannot have default value, pass values explicitly >>> lambda a,**b=3: None Traceback (most recent call last): -SyntaxError: var-keyword parameter's default value cannot be changed because it has the immutable default value {} +SyntaxError: var-keyword parameter cannot have default value, pass values explicitly >>> lambda a, *a, b, **c, d: None Traceback (most recent call last): diff --git a/Parser/parser.c b/Parser/parser.c index e5db365345e8bd..82656345e9b332 100644 --- a/Parser/parser.c +++ b/Parser/parser.c @@ -22710,7 +22710,7 @@ invalid_star_etc_rule(Parser *p) ) { D(fprintf(stderr, "%*c+ invalid_star_etc[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'*' param '='")); - _res = RAISE_SYNTAX_ERROR_KNOWN_LOCATION ( a , "var-positional parameter's default value cannot be changed because it has the immutable default value ()" ); + _res = RAISE_SYNTAX_ERROR_KNOWN_LOCATION ( a , "var-positional parameter cannot have default value, pass values explicitly" ); if (_res == NULL && PyErr_Occurred()) { p->error_indicator = 1; p->level--; @@ -22795,7 +22795,7 @@ invalid_kwds_rule(Parser *p) ) { D(fprintf(stderr, "%*c+ invalid_kwds[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'**' param '='")); - _res = RAISE_SYNTAX_ERROR_KNOWN_LOCATION ( a , "var-keyword parameter's default value cannot be changed because it has the immutable default value {}" ); + _res = RAISE_SYNTAX_ERROR_KNOWN_LOCATION ( a , "var-keyword parameter cannot have default value, pass values explicitly" ); if (_res == NULL && PyErr_Occurred()) { p->error_indicator = 1; p->level--; @@ -23289,7 +23289,7 @@ invalid_lambda_star_etc_rule(Parser *p) ) { D(fprintf(stderr, "%*c+ invalid_lambda_star_etc[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'*' lambda_param '='")); - _res = RAISE_SYNTAX_ERROR_KNOWN_LOCATION ( a , "var-positional parameter's default value cannot be changed because it has the immutable default value ()" ); + _res = RAISE_SYNTAX_ERROR_KNOWN_LOCATION ( a , "var-positional parameter cannot have default value, pass values explicitly" ); if (_res == NULL && PyErr_Occurred()) { p->error_indicator = 1; p->level--; @@ -23377,7 +23377,7 @@ invalid_lambda_kwds_rule(Parser *p) ) { D(fprintf(stderr, "%*c+ invalid_lambda_kwds[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'**' lambda_param '='")); - _res = RAISE_SYNTAX_ERROR_KNOWN_LOCATION ( a , "var-keyword parameter's default value cannot be changed because it has the immutable default value {}" ); + _res = RAISE_SYNTAX_ERROR_KNOWN_LOCATION ( a , "var-keyword parameter cannot have default value, pass values explicitly" ); if (_res == NULL && PyErr_Occurred()) { p->error_indicator = 1; p->level--;