Skip to content

Commit 2a172a6

Browse files
committed
commented and cleaned up
1 parent ddd8151 commit 2a172a6

File tree

2 files changed

+41
-115
lines changed

2 files changed

+41
-115
lines changed

Lib/test/test_named_expression.py

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,120 +6,116 @@
66

77
class NamedExpressionInvalidTest(unittest.TestCase):
88

9+
# Test modified to accept syntax error with deviating message
910
def test_named_expression_invalid_01(self):
1011
code = """x := 0"""
1112

12-
#with self.assertRaisesRegex(SyntaxError, "invalid syntax"):
13-
with self.assertRaises(SyntaxError): # TODO RustPython
13+
with self.assertRaises(SyntaxError):
1414
exec(code, {}, {})
1515

16+
# Test modified to accept syntax error with deviating message
1617
def test_named_expression_invalid_02(self):
1718
code = """x = y := 0"""
1819

19-
#with self.assertRaisesRegex(SyntaxError, "invalid syntax"):
20-
with self.assertRaises(SyntaxError): # TODO RustPython
20+
with self.assertRaises(SyntaxError):
2121
exec(code, {}, {})
2222

23+
# Test modified to accept syntax error with deviating message
2324
def test_named_expression_invalid_03(self):
2425
code = """y := f(x)"""
2526

26-
#with self.assertRaisesRegex(SyntaxError, "invalid syntax"):
27-
with self.assertRaises(SyntaxError): # TODO RustPython
27+
with self.assertRaises(SyntaxError):
2828
exec(code, {}, {})
2929

30+
# Test modified to accept syntax error with deviating message
3031
def test_named_expression_invalid_04(self):
3132
code = """y0 = y1 := f(x)"""
3233

33-
#with self.assertRaisesRegex(SyntaxError, "invalid syntax"):
34-
with self.assertRaises(SyntaxError): # TODO RustPython
34+
with self.assertRaises(SyntaxError):
3535
exec(code, {}, {})
3636

37+
# Test modified to accept syntax error with deviating message
3738
def test_named_expression_invalid_06(self):
3839
code = """((a, b) := (1, 2))"""
3940

40-
#with self.assertRaisesRegex(SyntaxError, "cannot use assignment expressions with tuple"):
41-
with self.assertRaises(SyntaxError): # TODO RustPython
41+
with self.assertRaises(SyntaxError):
4242
exec(code, {}, {})
4343

44+
# Test modified to accept syntax error with deviating message
4445
def test_named_expression_invalid_07(self):
4546
code = """def spam(a = b := 42): pass"""
4647

47-
#with self.assertRaisesRegex(SyntaxError, "invalid syntax"):
48-
with self.assertRaises(SyntaxError): # TODO RustPython
48+
with self.assertRaises(SyntaxError):
4949
exec(code, {}, {})
5050

51+
# Test modified to accept syntax error with deviating message
5152
def test_named_expression_invalid_08(self):
5253
code = """def spam(a: b := 42 = 5): pass"""
5354

54-
#with self.assertRaisesRegex(SyntaxError, "invalid syntax"):
55-
with self.assertRaises(SyntaxError): # TODO RustPython
55+
with self.assertRaises(SyntaxError):
5656
exec(code, {}, {})
5757

58+
# Test modified to accept syntax error with deviating message
5859
def test_named_expression_invalid_09(self):
5960
code = """spam(a=b := 'c')"""
6061

61-
#with self.assertRaisesRegex(SyntaxError, "invalid syntax"):
62-
with self.assertRaises(SyntaxError): # TODO RustPython
62+
with self.assertRaises(SyntaxError):
6363
exec(code, {}, {})
6464

65+
# Test modified to accept syntax error with deviating message
6566
def test_named_expression_invalid_10(self):
6667
code = """spam(x = y := f(x))"""
6768

68-
#with self.assertRaisesRegex(SyntaxError, "invalid syntax"):
69-
with self.assertRaises(SyntaxError): # TODO RustPython
69+
with self.assertRaises(SyntaxError):
7070
exec(code, {}, {})
7171

72+
# Test modified to accept syntax error with deviating message
7273
def test_named_expression_invalid_11(self):
7374
code = """spam(a=1, b := 2)"""
7475

75-
#with self.assertRaisesRegex(SyntaxError,
76-
# "positional argument follows keyword argument"):
77-
with self.assertRaises(SyntaxError): # TODO RustPython
76+
with self.assertRaises(SyntaxError):
7877
exec(code, {}, {})
7978

79+
# Test modified to accept syntax error with deviating message
8080
def test_named_expression_invalid_12(self):
8181
code = """spam(a=1, (b := 2))"""
8282

83-
#with self.assertRaisesRegex(SyntaxError,
84-
# "positional argument follows keyword argument"):
85-
with self.assertRaises(SyntaxError): # TODO RustPython
83+
with self.assertRaises(SyntaxError):
8684
exec(code, {}, {})
8785

86+
# Test modified to accept syntax error with deviating message
8887
def test_named_expression_invalid_13(self):
8988
code = """spam(a=1, (b := 2))"""
9089

91-
#with self.assertRaisesRegex(SyntaxError,
92-
# "positional argument follows keyword argument"):
93-
with self.assertRaises(SyntaxError): # TODO RustPython
90+
with self.assertRaises(SyntaxError):
9491
exec(code, {}, {})
9592

93+
# Test modified to accept syntax error with deviating message
9694
def test_named_expression_invalid_14(self):
9795
code = """(x := lambda: y := 1)"""
9896

99-
#with self.assertRaisesRegex(SyntaxError, "invalid syntax"):
100-
with self.assertRaises(SyntaxError): # TODO RustPython
97+
with self.assertRaises(SyntaxError):
10198
exec(code, {}, {})
10299

100+
# Test modified to accept syntax error with deviating message
103101
def test_named_expression_invalid_15(self):
104102
code = """(lambda: x := 1)"""
105103

106-
#with self.assertRaisesRegex(SyntaxError,
107-
# "cannot use assignment expressions with lambda"):
108-
with self.assertRaises(SyntaxError): # TODO RustPython
104+
with self.assertRaises(SyntaxError):
109105
exec(code, {}, {})
110106

107+
# Test modified to accept syntax error with deviating message
111108
def test_named_expression_invalid_16(self):
112109
code = "[i + 1 for i in i := [1,2]]"
113110

114-
#with self.assertRaisesRegex(SyntaxError, "invalid syntax"):
115-
with self.assertRaises(SyntaxError): # TODO RustPython
111+
with self.assertRaises(SyntaxError):
116112
exec(code, {}, {})
117113

114+
# Test modified to accept syntax error with deviating message
118115
def test_named_expression_invalid_17(self):
119116
code = "[i := 0, j := 1 for i, j in [(1, 2), (3, 4)]]"
120117

121-
#with self.assertRaisesRegex(SyntaxError, "invalid syntax"):
122-
with self.assertRaises(SyntaxError): # TODO RustPython
118+
with self.assertRaises(SyntaxError):
123119
exec(code, {}, {})
124120

125121
def test_named_expression_invalid_in_class_body(self):
@@ -131,6 +127,7 @@ def test_named_expression_invalid_in_class_body(self):
131127
"assignment expression within a comprehension cannot be used in a class body"):
132128
exec(code, {}, {})
133129

130+
# Test modified to accept syntax error with deviating message
134131
def test_named_expression_invalid_rebinding_comprehension_iteration_variable(self):
135132
cases = [
136133
("Local reuse", 'i', "[i := 0 for i in range(5)]"),
@@ -143,18 +140,17 @@ def test_named_expression_invalid_rebinding_comprehension_iteration_variable(sel
143140
"[(i, j) for i in range(5) for j in range(5) if True or (i:=10)]"),
144141
]
145142
for case, target, code in cases:
146-
msg = f"assignment expression cannot rebind comprehension iteration variable '{target}'"
147143
with self.subTest(case=case):
148144
with self.assertRaises(SyntaxError):
149145
exec(code, {}, {})
150146

147+
# Test modified to accept syntax error with deviating message
151148
def test_named_expression_invalid_rebinding_comprehension_inner_loop(self):
152149
cases = [
153150
("Inner reuse", 'j', "[i for i in range(5) if (j := 0) for j in range(5)]"),
154151
("Inner unpacking reuse", 'j', "[i for i in range(5) if (j := 0) for j, k in [(0, 1)]]"),
155152
]
156153
for case, target, code in cases:
157-
#msg = f"comprehension inner loop cannot rebind assignment expression target '{target}'"
158154
with self.subTest(case=case):
159155
with self.assertRaises(SyntaxError):
160156
exec(code, {}) # Module scope
@@ -163,6 +159,7 @@ def test_named_expression_invalid_rebinding_comprehension_inner_loop(self):
163159
with self.assertRaises(SyntaxError):
164160
exec(f"lambda: {code}", {}) # Function scope
165161

162+
# Test modified to accept syntax error with deviating message
166163
def test_named_expression_invalid_comprehension_iterable_expression(self):
167164
cases = [
168165
("Top level", "[i for i in (i := range(5))]"),
@@ -175,7 +172,6 @@ def test_named_expression_invalid_comprehension_iterable_expression(self):
175172
("Nested comprehension condition", "[i for i in [j for j in range(5) if (j := True)]]"),
176173
("Nested comprehension body", "[i for i in [(j := True) for j in range(5)]]"),
177174
]
178-
#msg = "assignment expression cannot be used in a comprehension iterable expression"
179175
for case, code in cases:
180176
with self.subTest(case=case):
181177
with self.assertRaises(SyntaxError):
@@ -339,10 +335,10 @@ def spam(a):
339335
self.assertEqual(res, [(1, 1, 1.0), (2, 2, 1.0), (3, 3, 1.0)])
340336
self.assertEqual(y, 3)
341337

342-
# TODO RustPython,
343-
@unittest.expectedFailure # TODO RustPython
338+
# TODO RustPython, added test_named_expression_scope_06_rp_modified as
339+
# minimaly modified variant of this test.
340+
@unittest.expectedFailure
344341
def test_named_expression_scope_06(self):
345-
#spam=1000
346342
res = [[spam := i for i in range(3)] for j in range(2)]
347343

348344
self.assertEqual(res, [[0, 1, 2], [0, 1, 2]])
@@ -388,7 +384,7 @@ def eggs(b):
388384
self.assertEqual(res, [0, 2])
389385
self.assertEqual(a, 2)
390386

391-
# TODO RustPython,
387+
# TODO RustPython, added test_named_expression_scope_10_rp_modified
392388
@unittest.expectedFailure
393389
def test_named_expression_scope_10(self):
394390
res = [b := [a := 1 for i in range(2)] for j in range(2)]

compiler/src/symboltable.rs

Lines changed: 2 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -271,74 +271,6 @@ impl<'a> SymbolTableAnalyzer<'a> {
271271
}
272272
}
273273
Ok(())
274-
275-
/*let mut handled=false;
276-
if curr_st_typ == SymbolTableType::Comprehension
277-
{
278-
if symbol.is_assign_namedexpr_in_comprehension {
279-
// propagate symbol to next higher level that can hold it,
280-
// i.e., function or module. Comprehension is skipped and
281-
// Class is not allowed and detected as error.
282-
//symbol.scope = SymbolScope::Nonlocal;
283-
handled = true;
284-
self.analyze_symbol_comprehension(symbol, 0)?
285-
}
286-
if symbol.is_iter {
287-
if let Some(parent_symbol) = self.tables.last().unwrap().0.get(&symbol.name) {
288-
if parent_symbol.is_assigned {
289-
return Err(SymbolTableError {
290-
error: format!("assignment expression cannot rebind comprehension iteration variable {}", symbol.name),
291-
location: Default::default(),
292-
});
293-
}
294-
}
295-
}
296-
}
297-
298-
if !handled {
299-
match symbol.scope {
300-
SymbolScope::Nonlocal => {
301-
let scope_depth = self.tables.len();
302-
if scope_depth > 0 {
303-
// check if the name is already defined in any outer scope
304-
// therefore
305-
if scope_depth < 2
306-
|| !self
307-
.tables
308-
.iter()
309-
.skip(1) // omit the global scope as it is not non-local
310-
.rev() // revert the order for better performance
311-
.any(|t| t.0.contains_key(&symbol.name))
312-
// true when any of symbol tables contains the name -> then negate
313-
{
314-
return Err(SymbolTableError {
315-
error: format!("no binding for nonlocal '{}' found", symbol.name),
316-
location: Default::default(),
317-
});
318-
}
319-
} else {
320-
return Err(SymbolTableError {
321-
error: format!(
322-
"nonlocal {} defined at place without an enclosing scope",
323-
symbol.name
324-
),
325-
location: Default::default(),
326-
});
327-
}
328-
}
329-
SymbolScope::Global => {
330-
// TODO: add more checks for globals?
331-
}
332-
SymbolScope::Local => {
333-
// all is well
334-
}
335-
SymbolScope::Unknown => {
336-
// Try hard to figure out what the scope of this symbol is.
337-
self.analyze_unknown_symbol(symbol);
338-
}
339-
}
340-
}
341-
Ok(())*/
342274
}
343275

344276
fn analyze_unknown_symbol(&self, symbol: &mut Symbol) {
@@ -381,9 +313,7 @@ impl<'a> SymbolTableAnalyzer<'a> {
381313
let table_type = last.1;
382314

383315
// it is not allowed to use an iterator variable as assignee in a named expression
384-
if
385-
/*symbol.is_assign_namedexpr_in_comprehension && (maybe this could be asserted) */
386-
symbol.is_iter {
316+
if symbol.is_iter {
387317
return Err(SymbolTableError {
388318
error: format!(
389319
"assignment expression cannot rebind comprehension iteration variable {}",
@@ -903,7 +833,7 @@ impl SymbolTableBuilder {
903833
// named expressions are not allowed in the definiton of
904834
// comprehension iterator definitions
905835
if let ExpressionContext::IterDefinitionExp = *context {
906-
return Err(SymbolTableError {
836+
return Err(SymbolTableError {
907837
error: "assignment expression cannot be used in a comprehension iterable expression".to_string(),
908838
location: Default::default(),
909839
});

0 commit comments

Comments
 (0)