mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-07 21:34:32 +08:00
Revision on assignment errors (#97)
* Revision on assignment expression errors * added rule for __debug__ (should be a keyword) * reviewed error messages * added new failing samples * Adjustment upon Dave's review * rewind several changes in assignment errors * patched is_definition: command not found for assignment expressions * patched Python 2 inconsistent error messages in test_python_errors.py: command not found
This commit is contained in:
@@ -914,6 +914,14 @@ class _CheckAssignmentRule(SyntaxRule):
|
|||||||
if second.type == 'yield_expr':
|
if second.type == 'yield_expr':
|
||||||
error = 'yield expression'
|
error = 'yield expression'
|
||||||
elif second.type == 'testlist_comp':
|
elif second.type == 'testlist_comp':
|
||||||
|
# ([a, b] := [1, 2])
|
||||||
|
# ((a, b) := [1, 2])
|
||||||
|
if is_namedexpr:
|
||||||
|
if first == '(':
|
||||||
|
error = 'tuple'
|
||||||
|
elif first == '[':
|
||||||
|
error = 'list'
|
||||||
|
|
||||||
# This is not a comprehension, they were handled
|
# This is not a comprehension, they were handled
|
||||||
# further above.
|
# further above.
|
||||||
for child in second.children[::2]:
|
for child in second.children[::2]:
|
||||||
@@ -963,6 +971,8 @@ class _CheckAssignmentRule(SyntaxRule):
|
|||||||
|
|
||||||
if error is not None:
|
if error is not None:
|
||||||
if is_namedexpr:
|
if is_namedexpr:
|
||||||
|
# c.f. CPython bpo-39176, should be changed in next release
|
||||||
|
# message = 'cannot use assignment expressions with %s' % error
|
||||||
message = 'cannot use named assignment with %s' % error
|
message = 'cannot use named assignment with %s' % error
|
||||||
else:
|
else:
|
||||||
cannot = "can't" if self._normalizer.version < (3, 8) else "cannot"
|
cannot = "can't" if self._normalizer.version < (3, 8) else "cannot"
|
||||||
|
|||||||
@@ -233,6 +233,8 @@ class Name(_LeafWithoutNewlines):
|
|||||||
while node is not None:
|
while node is not None:
|
||||||
if node.type == 'suite':
|
if node.type == 'suite':
|
||||||
return None
|
return None
|
||||||
|
if node.type == 'namedexpr_test':
|
||||||
|
return node.children[0]
|
||||||
if node.type in _GET_DEFINITION_TYPES:
|
if node.type in _GET_DEFINITION_TYPES:
|
||||||
if self in node.get_defined_names(include_setitem):
|
if self in node.get_defined_names(include_setitem):
|
||||||
return node
|
return node
|
||||||
|
|||||||
@@ -347,4 +347,14 @@ if sys.version_info[:2] >= (3, 8):
|
|||||||
# Not in that issue
|
# Not in that issue
|
||||||
'(await a := x)',
|
'(await a := x)',
|
||||||
'((await a) := x)',
|
'((await a) := x)',
|
||||||
|
# new discoveries
|
||||||
|
'((a, b) := (1, 2))',
|
||||||
|
'([a, b] := [1, 2])',
|
||||||
|
'({a, b} := {1, 2})',
|
||||||
|
'({a: b} := {1: 2})',
|
||||||
|
'(a + b := 1)',
|
||||||
|
'(True := 1)',
|
||||||
|
'(False := 1)',
|
||||||
|
'(None := 1)',
|
||||||
|
'(__debug__ := 1)',
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -321,4 +321,3 @@ def test_invalid_fstrings(code, message):
|
|||||||
def test_trailing_comma(code):
|
def test_trailing_comma(code):
|
||||||
errors = _get_error_list(code)
|
errors = _get_error_list(code)
|
||||||
assert not errors
|
assert not errors
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user