mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-07 13:24:39 +08:00
Allow unparenthesized walrus in set literals, set comprehensions and indexes
This commit is contained in:
@@ -124,14 +124,14 @@ atom: ('(' [yield_expr|testlist_comp] ')' |
|
|||||||
testlist_comp: (namedexpr_test|star_expr) ( comp_for | (',' (namedexpr_test|star_expr))* [','] )
|
testlist_comp: (namedexpr_test|star_expr) ( comp_for | (',' (namedexpr_test|star_expr))* [','] )
|
||||||
trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
|
trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
|
||||||
subscriptlist: subscript (',' subscript)* [',']
|
subscriptlist: subscript (',' subscript)* [',']
|
||||||
subscript: test | [test] ':' [test] [sliceop]
|
subscript: test [':=' test] | [test] ':' [test] [sliceop]
|
||||||
sliceop: ':' [test]
|
sliceop: ':' [test]
|
||||||
exprlist: (expr|star_expr) (',' (expr|star_expr))* [',']
|
exprlist: (expr|star_expr) (',' (expr|star_expr))* [',']
|
||||||
testlist: test (',' test)* [',']
|
testlist: test (',' test)* [',']
|
||||||
dictorsetmaker: ( ((test ':' test | '**' expr)
|
dictorsetmaker: ( ((test ':' test | '**' expr)
|
||||||
(comp_for | (',' (test ':' test | '**' expr))* [','])) |
|
(comp_for | (',' (test ':' test | '**' expr))* [','])) |
|
||||||
((test | star_expr)
|
((test [':=' test] | star_expr)
|
||||||
(comp_for | (',' (test | star_expr))* [','])) )
|
(comp_for | (',' (test [':=' test] | star_expr))* [','])) )
|
||||||
|
|
||||||
classdef: 'class' NAME ['(' [arglist] ')'] ':' suite
|
classdef: 'class' NAME ['(' [arglist] ')'] ':' suite
|
||||||
|
|
||||||
|
|||||||
@@ -130,8 +130,8 @@ exprlist: (expr|star_expr) (',' (expr|star_expr))* [',']
|
|||||||
testlist: test (',' test)* [',']
|
testlist: test (',' test)* [',']
|
||||||
dictorsetmaker: ( ((test ':' test | '**' expr)
|
dictorsetmaker: ( ((test ':' test | '**' expr)
|
||||||
(comp_for | (',' (test ':' test | '**' expr))* [','])) |
|
(comp_for | (',' (test ':' test | '**' expr))* [','])) |
|
||||||
((test | star_expr)
|
((test [':=' test] | star_expr)
|
||||||
(comp_for | (',' (test | star_expr))* [','])) )
|
(comp_for | (',' (test [':=' test] | star_expr))* [','])) )
|
||||||
|
|
||||||
classdef: 'class' NAME ['(' [arglist] ')'] ':' suite
|
classdef: 'class' NAME ['(' [arglist] ')'] ':' suite
|
||||||
|
|
||||||
|
|||||||
@@ -356,6 +356,10 @@ if sys.version_info[:2] >= (3, 8):
|
|||||||
'(False := 1)',
|
'(False := 1)',
|
||||||
'(None := 1)',
|
'(None := 1)',
|
||||||
'(__debug__ := 1)',
|
'(__debug__ := 1)',
|
||||||
|
# Unparenthesized walrus not allowed in dict literals, dict comprehensions and slices
|
||||||
|
'{a:="a": b:=1}',
|
||||||
|
'{y:=1: 2 for x in range(5)}',
|
||||||
|
'a[b:=0:1:2]',
|
||||||
]
|
]
|
||||||
# f-string debugging syntax with invalid conversion character
|
# f-string debugging syntax with invalid conversion character
|
||||||
FAILING_EXAMPLES += [
|
FAILING_EXAMPLES += [
|
||||||
|
|||||||
@@ -289,12 +289,37 @@ def test_valid_fstrings(code):
|
|||||||
'[total := total + v for v in range(10)]',
|
'[total := total + v for v in range(10)]',
|
||||||
'while chunk := file.read(2):\n pass',
|
'while chunk := file.read(2):\n pass',
|
||||||
'numbers = [y := math.factorial(x), y**2, y**3]',
|
'numbers = [y := math.factorial(x), y**2, y**3]',
|
||||||
|
'{(a:="a"): (b:=1)}',
|
||||||
|
'{(y:=1): 2 for x in range(5)}',
|
||||||
|
'a[(b:=0)]',
|
||||||
|
'a[(b:=0, c:=0)]',
|
||||||
|
'a[(b:=0):1:2]',
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
def test_valid_namedexpr(code):
|
def test_valid_namedexpr(code):
|
||||||
assert not _get_error_list(code, version='3.8')
|
assert not _get_error_list(code, version='3.8')
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
'code', [
|
||||||
|
'{x := 1, 2, 3}',
|
||||||
|
'{x4 := x ** 5 for x in range(7)}',
|
||||||
|
]
|
||||||
|
)
|
||||||
|
def test_valid_namedexpr_set(code):
|
||||||
|
assert not _get_error_list(code, version='3.9')
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
'code', [
|
||||||
|
'a[b:=0]',
|
||||||
|
'a[b:=0, c:=0]',
|
||||||
|
]
|
||||||
|
)
|
||||||
|
def test_valid_namedexpr_index(code):
|
||||||
|
assert not _get_error_list(code, version='3.10')
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
('code', 'message'), [
|
('code', 'message'), [
|
||||||
("f'{1+}'", ('invalid syntax')),
|
("f'{1+}'", ('invalid syntax')),
|
||||||
|
|||||||
Reference in New Issue
Block a user