Add issue 'too many expressions in star-unpacking assignment'

This commit is contained in:
Dave Halter
2017-07-30 15:13:21 +02:00
parent cc24ba61ef
commit 94ce899a86
3 changed files with 20 additions and 14 deletions
+15 -7
View File
@@ -286,19 +286,27 @@ class ErrorFinder(Normalizer):
if node.parent.type == 'del_stmt': if node.parent.type == 'del_stmt':
self._add_syntax_error("can't use starred expression here", node.parent) self._add_syntax_error("can't use starred expression here", node.parent)
else: else:
starred = [c for c in node.children if c.type == 'star_expr'] is_definition = True
if len(starred) > 1: if is_definition:
message = "two starred expressions in assignment" args = [c for c in node.children if c != ',']
self._add_syntax_error(message, starred[1]) starred = [c for c in args if c.type == 'star_expr']
"can't use starred expression here" if len(starred) > 1:
message = "two starred expressions in assignment"
self._add_syntax_error(message, starred[1])
elif starred:
count = args.index(starred[0])
if count >= 256:
message = "too many expressions in star-unpacking assignment"
self._add_syntax_error(message, starred[0])
elif node.type == 'star_expr': elif node.type == 'star_expr':
if node.parent.type not in _STAR_EXPR_PARENTS: if node.parent.type not in _STAR_EXPR_PARENTS:
message = "starred assignment target must be in a list or tuple" message = "starred assignment target must be in a list or tuple"
self._add_syntax_error(message, node) self._add_syntax_error(message, node)
if node.parent.type == 'testlist_comp': if node.parent.type == 'testlist_comp':
# [*[] for a in [1]] # [*[] for a in [1]]
message = "iterable unpacking cannot be used in comprehension" if node.parent.children[0] == node:
self._add_syntax_error(message, node) message = "iterable unpacking cannot be used in comprehension"
self._add_syntax_error(message, node)
elif node.type == 'comp_for': elif node.type == 'comp_for':
# Some of the nodes here are already used, so no else if # Some of the nodes here are already used, so no else if
expr_list = node.children[1 + int(node.children[0] == 'async')] expr_list = node.children[1 + int(node.children[0] == 'async')]
@@ -51,6 +51,9 @@ class X():
nonlocal a nonlocal a
a = *args, *args
def glob(): def glob():
global x global x
y: foo = x y: foo = x
+2 -7
View File
@@ -290,20 +290,15 @@ def test_default_except_error_postition():
('{**{} for a in [1]}', '3.5'), ('{**{} for a in [1]}', '3.5'),
('"s" b""', '3.5'), ('"s" b""', '3.5'),
('b"ä"', '3.5'), ('b"ä"', '3.5'),
#('(%s *d) = x' % ('a,' * 256), '3.5') ('(%s *d) = x' % ('a,' * 256), '3.6')
] ]
) )
def test_python_exception_matches_version(code, version): def test_python_exception_matches_version(code, version):
if '.'.join(str(v) for v in sys.version_info[:2]) != version: if '.'.join(str(v) for v in sys.version_info[:2]) != version:
pytest.skip() pytest.skip()
wanted, line_nr = _get_actual_exception(code)
error, = _get_error_list(code) error, = _get_error_list(code)
try:
compile(code, '<unknown>', 'exec')
except (SyntaxError, IndentationError) as e:
wanted = e.__class__.__name__ + ': ' + e.msg
else:
assert False, "The piece of code should raise an exception."
assert wanted == error.message assert wanted == error.message