Fix pos-only parameters not requiring comma before slash

This fixes `def foo(x /, y):` being accepted as valid syntax.
This commit is contained in:
David Strobach 2020-08-27 01:37:49 +02:00
parent b5575947ad
commit 79eccc488c
2 changed files with 21 additions and 7 deletions

View file

@ -3132,3 +3132,11 @@ def test_syntax_error_augassign_cmp(exp):
def test_syntax_error_bar_kwonlyargs():
with pytest.raises(SyntaxError):
PARSER.parse("def spam(*):\n pass\n", mode="exec")
def test_syntax_error_bar_posonlyargs():
with pytest.raises(SyntaxError):
PARSER.parse("def spam(/):\n pass\n", mode="exec")
def test_syntax_error_bar_posonlyargs_no_comma():
with pytest.raises(SyntaxError):
PARSER.parse("def spam(x /, y):\n pass\n", mode="exec")

View file

@ -240,16 +240,19 @@ class Parser(ThreeSixParser):
p[0] = p0
def p_typedargslist_t12(self, p):
"""typedargslist : posonlyargslist typedargslist_opt"""
if p[2] is None:
p0 = p[1]
else:
p0 = p[2]
"""typedargslist : posonlyargslist comma_opt
| posonlyargslist COMMA typedargslist
"""
if len(p) == 4:
p0 = p[3]
p0.posonlyargs = p[1].posonlyargs
else:
p0 = p[1]
p[0] = p0
def p_posonlyargslist(self, p):
"""posonlyargslist : tfpdef equals_test_opt comma_tfpdef_list_opt DIVIDE comma_opt"""
"""posonlyargslist : tfpdef equals_test_opt COMMA DIVIDE
| tfpdef equals_test_opt comma_tfpdef_list COMMA DIVIDE"""
p0 = ast.arguments(
posonlyargs=[],
args=[],
@ -259,7 +262,10 @@ class Parser(ThreeSixParser):
kwarg=None,
defaults=[],
)
self._set_posonly_args(p0, p[1], p[2], p[3])
if p[3] == ",":
self._set_posonly_args(p0, p[1], p[2], None)
else:
self._set_posonly_args(p0, p[1], p[2], p[3])
p[0] = p0
def p_varargslist_kwargs(self, p):