Skip to content

bpo-36541: lib2to3: Support named assignment expressions #12702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Lib/lib2to3/Grammar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ assert_stmt: 'assert' test [',' test]

compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt
async_stmt: ASYNC (funcdef | with_stmt | for_stmt)
if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
while_stmt: 'while' test ':' suite ['else' ':' suite]
if_stmt: 'if' namedexpr_test ':' suite ('elif' namedexpr_test ':' suite)* ['else' ':' suite]
while_stmt: 'while' namedexpr_test ':' suite ['else' ':' suite]
for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
try_stmt: ('try' ':' suite
((except_clause ':' suite)+
Expand All @@ -91,6 +91,7 @@ testlist_safe: old_test [(',' old_test)+ [',']]
old_test: or_test | old_lambdef
old_lambdef: 'lambda' [varargslist] ':' old_test

namedexpr_test: test [':=' test]
test: or_test ['if' or_test 'else' test] | lambdef
or_test: and_test ('or' and_test)*
and_test: not_test ('and' not_test)*
Expand All @@ -111,8 +112,8 @@ atom: ('(' [yield_expr|testlist_gexp] ')' |
'{' [dictsetmaker] '}' |
'`' testlist1 '`' |
NAME | NUMBER | STRING+ | '.' '.' '.')
listmaker: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
testlist_gexp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
listmaker: (namedexpr_test|star_expr) ( comp_for | (',' (namedexpr_test|star_expr))* [','] )
testlist_gexp: (namedexpr_test|star_expr) ( comp_for | (',' (namedexpr_test|star_expr))* [','] )
lambdef: 'lambda' [varargslist] ':' test
trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
subscriptlist: subscript (',' subscript)* [',']
Expand All @@ -137,6 +138,7 @@ arglist: argument (',' argument)* [',']
# multiple (test comp_for) arguments are blocked; keyword unpackings
# that precede iterable unpackings are blocked; etc.
argument: ( test [comp_for] |
test ':=' test |
test '=' test |
'**' test |
'*' test )
Expand Down
1 change: 1 addition & 0 deletions Lib/lib2to3/pgen2/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ def report(self):
// DOUBLESLASH
//= DOUBLESLASHEQUAL
-> RARROW
:= COLONEQUAL
"""

opmap = {}
Expand Down
3 changes: 2 additions & 1 deletion Lib/lib2to3/pgen2/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
AWAIT = 56
ASYNC = 57
ERRORTOKEN = 58
N_TOKENS = 59
COLONEQUAL = 59
N_TOKENS = 60
NT_OFFSET = 256
#--end constants--

Expand Down
2 changes: 1 addition & 1 deletion Lib/lib2to3/pgen2/tokenize.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def _combinations(*l):
r"~")

Bracket = '[][(){}]'
Special = group(r'\r?\n', r'[:;.,`@]')
Special = group(r'\r?\n', r':=', r'[:;.,`@]')
Funny = group(Operator, Bracket, Special)

PlainToken = group(Number, Funny, String, Name)
Expand Down
15 changes: 15 additions & 0 deletions Lib/lib2to3/tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,21 @@ def test_multiline_str_literals(self):
self.validate(s)


class TestNamedAssignments(GrammarTest):

def test_named_assignment_if(self):
driver.parse_string("if f := x(): pass\n")

def test_named_assignment_while(self):
driver.parse_string("while f := x(): pass\n")

def test_named_assignment_generator(self):
driver.parse_string("any((lastNum := num) == 1 for num in [1, 2, 3])\n")

def test_named_assignment_listcomp(self):
driver.parse_string("[(lastNum := num) == 1 for num in [1, 2, 3]]\n")


class TestPickleableException(unittest.TestCase):
def test_ParseError(self):
err = ParseError('msg', 2, None, (1, 'context'))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lib2to3 now recognizes named assignment expressions (the walrus operator,
``:=``)