From 19dd58cbadfa2a7cad2e476dea1b8dda1bafdc7c Mon Sep 17 00:00:00 2001 From: Windel Bouwman Date: Fri, 2 Aug 2019 20:55:37 +0200 Subject: [PATCH] Add star expression in set literals. --- parser/src/python.lalrpop | 10 +++++----- tests/snippets/dict.py | 10 ++++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/parser/src/python.lalrpop b/parser/src/python.lalrpop index def4bef4a..db1a33312 100644 --- a/parser/src/python.lalrpop +++ b/parser/src/python.lalrpop @@ -890,11 +890,11 @@ Atom: ast::Expression = { node: ast::ExpressionType::Tuple { elements: Vec::new() } }) }, - "(" ")" => { + "(" ")" => { ast::Expression { location, node: ast::ExpressionType::Comprehension { - kind: Box::new(ast::ComprehensionKind::GeneratorExpression { element: e }), + kind: Box::new(ast::ComprehensionKind::GeneratorExpression { element }), generators: c, } } @@ -904,9 +904,9 @@ Atom: ast::Expression = { node: ast::ExpressionType::Dict { elements: e.unwrap_or_default() } }, "{" "}" => e, - "{" "}" => ast::Expression { + "{" "}" => ast::Expression { location, - node: ast::ExpressionType::Set { elements: e } + node: ast::ExpressionType::Set { elements } }, "{" "}" => e, "True" => ast::Expression { location, node: ast::ExpressionType::True }, @@ -957,7 +957,7 @@ DictElement: (Option, ast::Expression) = { }; TestSet: Vec = { - > ","? => e1 + > ","? => e1 }; TestSetComp: ast::Expression = { diff --git a/tests/snippets/dict.py b/tests/snippets/dict.py index c453cff2b..1ea2c6692 100644 --- a/tests/snippets/dict.py +++ b/tests/snippets/dict.py @@ -241,3 +241,13 @@ assert len(s) == 3 assert s[1] == 0 assert s[A()] == 1 assert s[B()] == 2 + +# Test dict usage in set with star expressions! +a = {'bla': 2} +b = {'c': 44, 'bla': 332, 'd': 6} +x = ['bla', 'c', 'd', 'f'] +c = {*a, *b, *x} +# print(c, type(c)) +assert isinstance(c, set) +assert c == {'bla', 'c', 'd', 'f'} +