mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Merge pull request #1199 from RustPython/set-star-expr
Add star expression in set literals.
This commit is contained in:
2 files changed
+15
-5
No files matched your search
@@ -890,11 +890,11 @@ Atom: ast::Expression = {
|
||||
node: ast::ExpressionType::Tuple { elements: Vec::new() }
|
||||
})
|
||||
},
|
||||
<location:@L> "(" <e:Test> <c:CompFor> ")" => {
|
||||
<location:@L> "(" <element:Test> <c:CompFor> ")" => {
|
||||
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:TestDictComp> "}" => e,
|
||||
<location:@L> "{" <e:TestSet> "}" => ast::Expression {
|
||||
<location:@L> "{" <elements:TestSet> "}" => ast::Expression {
|
||||
location,
|
||||
node: ast::ExpressionType::Set { elements: e }
|
||||
node: ast::ExpressionType::Set { elements }
|
||||
},
|
||||
"{" <e:TestSetComp> "}" => e,
|
||||
<location:@L> "True" => ast::Expression { location, node: ast::ExpressionType::True },
|
||||
@@ -957,7 +957,7 @@ DictElement: (Option<ast::Expression>, ast::Expression) = {
|
||||
};
|
||||
|
||||
TestSet: Vec<ast::Expression> = {
|
||||
<e1:OneOrMore<Test>> ","? => e1
|
||||
<e1:OneOrMore<TestOrStarExpr>> ","? => e1
|
||||
};
|
||||
|
||||
TestSetComp: ast::Expression = {
|
||||
|
||||
@@ -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'}
|
||||
|
||||
Reference in new issue
Block a user