Skip to content

Commit

Permalink
Allow same r.h.s. in annotated assignments as in normal ones.
Browse files Browse the repository at this point in the history
cpython commit: 62c35a8a8ff5854ed470b1c16a7a14f3bb80368c
  • Loading branch information
progval committed Sep 9, 2020
1 parent e92da74 commit 3fb429c
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ named!(small_stmt<StrSpan, Statement>,

// expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
// ('=' (yield_expr|testlist_star_expr))*)
// annassign: ':' test ['=' test]
// annassign: ':' test ['=' (yield_expr|testlist)]
named!(expr_stmt<StrSpan, Statement>,
do_parse!(
lhs: call!(ExpressionParser::<NewlinesAreNotSpaces>::testlist_star_expr) >>
Expand All @@ -67,12 +67,13 @@ named!(expr_stmt<StrSpan, Statement>,
do_parse!(
char!(':') >>
typed: call!(ExpressionParser::<NewlinesAreNotSpaces>::test) >>
rhs: opt!(ws_nonl!(preceded!(char!('='),
call!(ExpressionParser::<NewlinesAreNotSpaces>::test)
))) >> (
rhs: opt!(ws_nonl!(preceded!(char!('='), alt!(
call!(ExpressionParser::<NewlinesAreNotSpaces>::yield_expr) => { |e| vec![e] }
| call!(ExpressionParser::<NewlinesAreNotSpaces>::testlist)
)))) >> (
match rhs {
None => Statement::TypeAnnotation(lhs.clone(), *typed),
Some(rhs) => Statement::TypedAssignment(lhs.clone(), *typed, vec![*rhs]),
Some(rhs) => Statement::TypedAssignment(lhs.clone(), *typed, rhs),
}
)
)
Expand Down Expand Up @@ -1093,6 +1094,21 @@ mod tests {
);
}

#[test]
fn test_augassign_yield() {
assert_parse_eq(
small_stmt(make_strspan("foo:bar = yield baz")),
Ok((
make_strspan(""),
Statement::TypedAssignment(
vec![Expression::Name("foo".to_string())],
Expression::Name("bar".to_string()),
vec![Expression::Yield(vec![Expression::Name("baz".to_string())])],
),
)),
);
}

#[test]
fn test_unpack_assign() {
assert_parse_eq(
Expand Down

0 comments on commit 3fb429c

Please sign in to comment.