| 1 |
import unittest |
| 2 |
|
| 3 |
GLOBAL_VAR = None |
| 4 |
|
| 5 |
class NamedExpressionInvalidTest(unittest.TestCase): |
| 6 |
|
| 7 |
def test_named_expression_invalid_01(self): |
| 8 |
code = """x := 0""" |
| 9 |
|
| 10 |
with self.assertRaisesRegex(SyntaxError, "invalid syntax"): |
| 11 |
exec(code, {}, {}) |
| 12 |
|
| 13 |
def test_named_expression_invalid_02(self): |
| 14 |
code = """x = y := 0""" |
| 15 |
|
| 16 |
with self.assertRaisesRegex(SyntaxError, "invalid syntax"): |
| 17 |
exec(code, {}, {}) |
| 18 |
|
| 19 |
def test_named_expression_invalid_03(self): |
| 20 |
code = """y := f(x)""" |
| 21 |
|
| 22 |
with self.assertRaisesRegex(SyntaxError, "invalid syntax"): |
| 23 |
exec(code, {}, {}) |
| 24 |
|
| 25 |
def test_named_expression_invalid_04(self): |
| 26 |
code = """y0 = y1 := f(x)""" |
| 27 |
|
| 28 |
with self.assertRaisesRegex(SyntaxError, "invalid syntax"): |
| 29 |
exec(code, {}, {}) |
| 30 |
|
| 31 |
def test_named_expression_invalid_06(self): |
| 32 |
code = """((a, b) := (1, 2))""" |
| 33 |
|
| 34 |
with self.assertRaisesRegex(SyntaxError, "cannot use assignment expressions with tuple"): |
| 35 |
exec(code, {}, {}) |
| 36 |
|
| 37 |
def test_named_expression_invalid_07(self): |
| 38 |
code = """def spam(a = b := 42): pass""" |
| 39 |
|
| 40 |
with self.assertRaisesRegex(SyntaxError, "invalid syntax"): |
| 41 |
exec(code, {}, {}) |
| 42 |
|
| 43 |
def test_named_expression_invalid_08(self): |
| 44 |
code = """def spam(a: b := 42 = 5): pass""" |
| 45 |
|
| 46 |
with self.assertRaisesRegex(SyntaxError, "invalid syntax"): |
| 47 |
exec(code, {}, {}) |
| 48 |
|
| 49 |
def test_named_expression_invalid_09(self): |
| 50 |
code = """spam(a=b := 'c')""" |
| 51 |
|
| 52 |
with self.assertRaisesRegex(SyntaxError, "invalid syntax"): |
| 53 |
exec(code, {}, {}) |
| 54 |
|
| 55 |
def test_named_expression_invalid_10(self): |
| 56 |
code = """spam(x = y := f(x))""" |
| 57 |
|
| 58 |
with self.assertRaisesRegex(SyntaxError, "invalid syntax"): |
| 59 |
exec(code, {}, {}) |
| 60 |
|
| 61 |
def test_named_expression_invalid_11(self): |
| 62 |
code = """spam(a=1, b := 2)""" |
| 63 |
|
| 64 |
with self.assertRaisesRegex(SyntaxError, |
| 65 |
"positional argument follows keyword argument"): |
| 66 |
exec(code, {}, {}) |
| 67 |
|
| 68 |
def test_named_expression_invalid_12(self): |
| 69 |
code = """spam(a=1, (b := 2))""" |
| 70 |
|
| 71 |
with self.assertRaisesRegex(SyntaxError, |
| 72 |
"positional argument follows keyword argument"): |
| 73 |
exec(code, {}, {}) |
| 74 |
|
| 75 |
def test_named_expression_invalid_13(self): |
| 76 |
code = """spam(a=1, (b := 2))""" |
| 77 |
|
| 78 |
with self.assertRaisesRegex(SyntaxError, |
| 79 |
"positional argument follows keyword argument"): |
| 80 |
exec(code, {}, {}) |
| 81 |
|
| 82 |
def test_named_expression_invalid_14(self): |
| 83 |
code = """(x := lambda: y := 1)""" |
| 84 |
|
| 85 |
with self.assertRaisesRegex(SyntaxError, "invalid syntax"): |
| 86 |
exec(code, {}, {}) |
| 87 |
|
| 88 |
def test_named_expression_invalid_15(self): |
| 89 |
code = """(lambda: x := 1)""" |
| 90 |
|
| 91 |
with self.assertRaisesRegex(SyntaxError, |
| 92 |
"cannot use assignment expressions with lambda"): |
| 93 |
exec(code, {}, {}) |
| 94 |
|
| 95 |
def test_named_expression_invalid_16(self): |
| 96 |
code = "[i + 1 for i in i := [1,2]]" |
| 97 |
|
| 98 |
with self.assertRaisesRegex(SyntaxError, "invalid syntax"): |
| 99 |
exec(code, {}, {}) |
| 100 |
|
| 101 |
def test_named_expression_invalid_17(self): |
| 102 |
code = "[i := 0, j := 1 for i, j in [(1, 2), (3, 4)]]" |
| 103 |
|
| 104 |
with self.assertRaisesRegex(SyntaxError, |
| 105 |
"did you forget parentheses around the comprehension target?"): |
| 106 |
exec(code, {}, {}) |
| 107 |
|
| 108 |
def test_named_expression_invalid_in_class_body(self): |
| 109 |
code = """class Foo(): |
| 110 |
[(42, 1 + ((( j := i )))) for i in range(5)] |
| 111 |
""" |
| 112 |
|
| 113 |
with self.assertRaisesRegex(SyntaxError, |
| 114 |
"assignment expression within a comprehension cannot be used in a class body"): |
| 115 |
exec(code, {}, {}) |
| 116 |
|
| 117 |
def test_named_expression_valid_rebinding_iteration_variable(self): |
| 118 |
# This test covers that we can reassign variables |
| 119 |
# that are not directly assigned in the |
| 120 |
# iterable part of a comprehension. |
| 121 |
cases = [ |
| 122 |
# Regression tests from https://github.com/python/cpython/issues/87447 |
| 123 |
("Complex expression: c", |
| 124 |
"{0}(c := 1) for a, (*b, c[d+e::f(g)], h.i) in j{1}"), |
| 125 |
("Complex expression: d", |
| 126 |
"{0}(d := 1) for a, (*b, c[d+e::f(g)], h.i) in j{1}"), |
| 127 |
("Complex expression: e", |
| 128 |
"{0}(e := 1) for a, (*b, c[d+e::f(g)], h.i) in j{1}"), |
| 129 |
("Complex expression: f", |
| 130 |
"{0}(f := 1) for a, (*b, c[d+e::f(g)], h.i) in j{1}"), |
| 131 |
("Complex expression: g", |
| 132 |
"{0}(g := 1) for a, (*b, c[d+e::f(g)], h.i) in j{1}"), |
| 133 |
("Complex expression: h", |
| 134 |
"{0}(h := 1) for a, (*b, c[d+e::f(g)], h.i) in j{1}"), |
| 135 |
("Complex expression: i", |
| 136 |
"{0}(i := 1) for a, (*b, c[d+e::f(g)], h.i) in j{1}"), |
| 137 |
("Complex expression: j", |
| 138 |
"{0}(j := 1) for a, (*b, c[d+e::f(g)], h.i) in j{1}"), |
| 139 |
] |
| 140 |
for test_case, code in cases: |
| 141 |
for lpar, rpar in [('(', ')'), ('[', ']'), ('{', '}')]: |
| 142 |
code = code.format(lpar, rpar) |
| 143 |
with self.subTest(case=test_case, lpar=lpar, rpar=rpar): |
| 144 |
# Names used in snippets are not defined, |
| 145 |
# but we are fine with it: just must not be a SyntaxError. |
| 146 |
# Names used in snippets are not defined, |
| 147 |
# but we are fine with it: just must not be a SyntaxError. |
| 148 |
with self.assertRaises(NameError): |
| 149 |
exec(code, {}) # Module scope |
| 150 |
with self.assertRaises(NameError): |
| 151 |
exec(code, {}, {}) # Class scope |
| 152 |
exec(f"lambda: {code}", {}) # Function scope |
| 153 |
|
| 154 |
def test_named_expression_invalid_rebinding_iteration_variable(self): |
| 155 |
# This test covers that we cannot reassign variables |
| 156 |
# that are directly assigned in the iterable part of a comprehension. |
| 157 |
cases = [ |
| 158 |
# Regression tests from https://github.com/python/cpython/issues/87447 |
| 159 |
("Complex expression: a", "a", |
| 160 |
"{0}(a := 1) for a, (*b, c[d+e::f(g)], h.i) in j{1}"), |
| 161 |
("Complex expression: b", "b", |
| 162 |
"{0}(b := 1) for a, (*b, c[d+e::f(g)], h.i) in j{1}"), |
| 163 |
] |
| 164 |
for test_case, target, code in cases: |
| 165 |
msg = f"assignment expression cannot rebind comprehension iteration variable '{target}'" |
| 166 |
for lpar, rpar in [('(', ')'), ('[', ']'), ('{', '}')]: |
| 167 |
code = code.format(lpar, rpar) |
| 168 |
with self.subTest(case=test_case, lpar=lpar, rpar=rpar): |
| 169 |
# Names used in snippets are not defined, |
| 170 |
# but we are fine with it: just must not be a SyntaxError. |
| 171 |
# Names used in snippets are not defined, |
| 172 |
# but we are fine with it: just must not be a SyntaxError. |
| 173 |
with self.assertRaisesRegex(SyntaxError, msg): |
| 174 |
exec(code, {}) # Module scope |
| 175 |
with self.assertRaisesRegex(SyntaxError, msg): |
| 176 |
exec(code, {}, {}) # Class scope |
| 177 |
with self.assertRaisesRegex(SyntaxError, msg): |
| 178 |
exec(f"lambda: {code}", {}) # Function scope |
| 179 |
|
| 180 |
def test_named_expression_invalid_rebinding_list_comprehension_iteration_variable(self): |
| 181 |
cases = [ |
| 182 |
("Local reuse", 'i', "[i := 0 for i in range(5)]"), |
| 183 |
("Nested reuse", 'j', "[[(j := 0) for i in range(5)] for j in range(5)]"), |
| 184 |
("Reuse inner loop target", 'j', "[(j := 0) for i in range(5) for j in range(5)]"), |
| 185 |
("Unpacking reuse", 'i', "[i := 0 for i, j in [(0, 1)]]"), |
| 186 |
("Reuse in loop condition", 'i', "[i+1 for i in range(5) if (i := 0)]"), |
| 187 |
("Unreachable reuse", 'i', "[False or (i:=0) for i in range(5)]"), |
| 188 |
("Unreachable nested reuse", 'i', |
| 189 |
"[(i, j) for i in range(5) for j in range(5) if True or (i:=10)]"), |
| 190 |
] |
| 191 |
for case, target, code in cases: |
| 192 |
msg = f"assignment expression cannot rebind comprehension iteration variable '{target}'" |
| 193 |
with self.subTest(case=case): |
| 194 |
with self.assertRaisesRegex(SyntaxError, msg): |
| 195 |
exec(code, {}) # Module scope |
| 196 |
with self.assertRaisesRegex(SyntaxError, msg): |
| 197 |
exec(code, {}, {}) # Class scope |
| 198 |
with self.assertRaisesRegex(SyntaxError, msg): |
| 199 |
exec(f"lambda: {code}", {}) # Function scope |
| 200 |
|
| 201 |
def test_named_expression_invalid_rebinding_list_comprehension_inner_loop(self): |
| 202 |
cases = [ |
| 203 |
("Inner reuse", 'j', "[i for i in range(5) if (j := 0) for j in range(5)]"), |
| 204 |
("Inner unpacking reuse", 'j', "[i for i in range(5) if (j := 0) for j, k in [(0, 1)]]"), |
| 205 |
] |
| 206 |
for case, target, code in cases: |
| 207 |
msg = f"comprehension inner loop cannot rebind assignment expression target '{target}'" |
| 208 |
with self.subTest(case=case): |
| 209 |
with self.assertRaisesRegex(SyntaxError, msg): |
| 210 |
exec(code, {}) # Module scope |
| 211 |
with self.assertRaisesRegex(SyntaxError, msg): |
| 212 |
exec(code, {}, {}) # Class scope |
| 213 |
with self.assertRaisesRegex(SyntaxError, msg): |
| 214 |
exec(f"lambda: {code}", {}) # Function scope |
| 215 |
|
| 216 |
def test_named_expression_invalid_list_comprehension_iterable_expression(self): |
| 217 |
cases = [ |
| 218 |
("Top level", "[i for i in (i := range(5))]"), |
| 219 |
("Inside tuple", "[i for i in (2, 3, i := range(5))]"), |
| 220 |
("Inside list", "[i for i in [2, 3, i := range(5)]]"), |
| 221 |
("Different name", "[i for i in (j := range(5))]"), |
| 222 |
("Lambda expression", "[i for i in (lambda:(j := range(5)))()]"), |
| 223 |
("Inner loop", "[i for i in range(5) for j in (i := range(5))]"), |
| 224 |
("Nested comprehension", "[i for i in [j for j in (k := range(5))]]"), |
| 225 |
("Nested comprehension condition", "[i for i in [j for j in range(5) if (j := True)]]"), |
| 226 |
("Nested comprehension body", "[i for i in [(j := True) for j in range(5)]]"), |
| 227 |
] |
| 228 |
msg = "assignment expression cannot be used in a comprehension iterable expression" |
| 229 |
for case, code in cases: |
| 230 |
with self.subTest(case=case): |
| 231 |
with self.assertRaisesRegex(SyntaxError, msg): |
| 232 |
exec(code, {}) # Module scope |
| 233 |
with self.assertRaisesRegex(SyntaxError, msg): |
| 234 |
exec(code, {}, {}) # Class scope |
| 235 |
with self.assertRaisesRegex(SyntaxError, msg): |
| 236 |
exec(f"lambda: {code}", {}) # Function scope |
| 237 |
|
| 238 |
def test_named_expression_invalid_rebinding_set_comprehension_iteration_variable(self): |
| 239 |
cases = [ |
| 240 |
("Local reuse", 'i', "{i := 0 for i in range(5)}"), |
| 241 |
("Nested reuse", 'j', "{{(j := 0) for i in range(5)} for j in range(5)}"), |
| 242 |
("Reuse inner loop target", 'j', "{(j := 0) for i in range(5) for j in range(5)}"), |
| 243 |
("Unpacking reuse", 'i', "{i := 0 for i, j in {(0, 1)}}"), |
| 244 |
("Reuse in loop condition", 'i', "{i+1 for i in range(5) if (i := 0)}"), |
| 245 |
("Unreachable reuse", 'i', "{False or (i:=0) for i in range(5)}"), |
| 246 |
("Unreachable nested reuse", 'i', |
| 247 |
"{(i, j) for i in range(5) for j in range(5) if True or (i:=10)}"), |
| 248 |
# Regression tests from https://github.com/python/cpython/issues/87447 |
| 249 |
("Complex expression: a", "a", |
| 250 |
"{(a := 1) for a, (*b, c[d+e::f(g)], h.i) in j}"), |
| 251 |
("Complex expression: b", "b", |
| 252 |
"{(b := 1) for a, (*b, c[d+e::f(g)], h.i) in j}"), |
| 253 |
] |
| 254 |
for case, target, code in cases: |
| 255 |
msg = f"assignment expression cannot rebind comprehension iteration variable '{target}'" |
| 256 |
with self.subTest(case=case): |
| 257 |
with self.assertRaisesRegex(SyntaxError, msg): |
| 258 |
exec(code, {}) # Module scope |
| 259 |
with self.assertRaisesRegex(SyntaxError, msg): |
| 260 |
exec(code, {}, {}) # Class scope |
| 261 |
with self.assertRaisesRegex(SyntaxError, msg): |
| 262 |
exec(f"lambda: {code}", {}) # Function scope |
| 263 |
|
| 264 |
def test_named_expression_invalid_rebinding_set_comprehension_inner_loop(self): |
| 265 |
cases = [ |
| 266 |
("Inner reuse", 'j', "{i for i in range(5) if (j := 0) for j in range(5)}"), |
| 267 |
("Inner unpacking reuse", 'j', "{i for i in range(5) if (j := 0) for j, k in {(0, 1)}}"), |
| 268 |
] |
| 269 |
for case, target, code in cases: |
| 270 |
msg = f"comprehension inner loop cannot rebind assignment expression target '{target}'" |
| 271 |
with self.subTest(case=case): |
| 272 |
with self.assertRaisesRegex(SyntaxError, msg): |
| 273 |
exec(code, {}) # Module scope |
| 274 |
with self.assertRaisesRegex(SyntaxError, msg): |
| 275 |
exec(code, {}, {}) # Class scope |
| 276 |
with self.assertRaisesRegex(SyntaxError, msg): |
| 277 |
exec(f"lambda: {code}", {}) # Function scope |
| 278 |
|
| 279 |
def test_named_expression_invalid_set_comprehension_iterable_expression(self): |
| 280 |
cases = [ |
| 281 |
("Top level", "{i for i in (i := range(5))}"), |
| 282 |
("Inside tuple", "{i for i in (2, 3, i := range(5))}"), |
| 283 |
("Inside list", "{i for i in {2, 3, i := range(5)}}"), |
| 284 |
("Different name", "{i for i in (j := range(5))}"), |
| 285 |
("Lambda expression", "{i for i in (lambda:(j := range(5)))()}"), |
| 286 |
("Inner loop", "{i for i in range(5) for j in (i := range(5))}"), |
| 287 |
("Nested comprehension", "{i for i in {j for j in (k := range(5))}}"), |
| 288 |
("Nested comprehension condition", "{i for i in {j for j in range(5) if (j := True)}}"), |
| 289 |
("Nested comprehension body", "{i for i in {(j := True) for j in range(5)}}"), |
| 290 |
] |
| 291 |
msg = "assignment expression cannot be used in a comprehension iterable expression" |
| 292 |
for case, code in cases: |
| 293 |
with self.subTest(case=case): |
| 294 |
with self.assertRaisesRegex(SyntaxError, msg): |
| 295 |
exec(code, {}) # Module scope |
| 296 |
with self.assertRaisesRegex(SyntaxError, msg): |
| 297 |
exec(code, {}, {}) # Class scope |
| 298 |
with self.assertRaisesRegex(SyntaxError, msg): |
| 299 |
exec(f"lambda: {code}", {}) # Function scope |
| 300 |
|
| 301 |
def test_named_expression_invalid_rebinding_dict_comprehension_iteration_variable(self): |
| 302 |
cases = [ |
| 303 |
("Key reuse", 'i', "{(i := 0): 1 for i in range(5)}"), |
| 304 |
("Value reuse", 'i', "{1: (i := 0) for i in range(5)}"), |
| 305 |
("Both reuse", 'i', "{(i := 0): (i := 0) for i in range(5)}"), |
| 306 |
("Nested reuse", 'j', "{{(j := 0): 1 for i in range(5)} for j in range(5)}"), |
| 307 |
("Reuse inner loop target", 'j', "{(j := 0): 1 for i in range(5) for j in range(5)}"), |
| 308 |
("Unpacking key reuse", 'i', "{(i := 0): 1 for i, j in {(0, 1)}}"), |
| 309 |
("Unpacking value reuse", 'i', "{1: (i := 0) for i, j in {(0, 1)}}"), |
| 310 |
("Reuse in loop condition", 'i', "{i+1: 1 for i in range(5) if (i := 0)}"), |
| 311 |
("Unreachable reuse", 'i', "{(False or (i:=0)): 1 for i in range(5)}"), |
| 312 |
("Unreachable nested reuse", 'i', |
| 313 |
"{i: j for i in range(5) for j in range(5) if True or (i:=10)}"), |
| 314 |
# Regression tests from https://github.com/python/cpython/issues/87447 |
| 315 |
("Complex expression: a", "a", |
| 316 |
"{(a := 1): 1 for a, (*b, c[d+e::f(g)], h.i) in j}"), |
| 317 |
("Complex expression: b", "b", |
| 318 |
"{(b := 1): 1 for a, (*b, c[d+e::f(g)], h.i) in j}"), |
| 319 |
] |
| 320 |
for case, target, code in cases: |
| 321 |
msg = f"assignment expression cannot rebind comprehension iteration variable '{target}'" |
| 322 |
with self.subTest(case=case): |
| 323 |
with self.assertRaisesRegex(SyntaxError, msg): |
| 324 |
exec(code, {}) # Module scope |
| 325 |
with self.assertRaisesRegex(SyntaxError, msg): |
| 326 |
exec(code, {}, {}) # Class scope |
| 327 |
with self.assertRaisesRegex(SyntaxError, msg): |
| 328 |
exec(f"lambda: {code}", {}) # Function scope |
| 329 |
|
| 330 |
def test_named_expression_invalid_rebinding_dict_comprehension_inner_loop(self): |
| 331 |
cases = [ |
| 332 |
("Inner reuse", 'j', "{i: 1 for i in range(5) if (j := 0) for j in range(5)}"), |
| 333 |
("Inner unpacking reuse", 'j', "{i: 1 for i in range(5) if (j := 0) for j, k in {(0, 1)}}"), |
| 334 |
] |
| 335 |
for case, target, code in cases: |
| 336 |
msg = f"comprehension inner loop cannot rebind assignment expression target '{target}'" |
| 337 |
with self.subTest(case=case): |
| 338 |
with self.assertRaisesRegex(SyntaxError, msg): |
| 339 |
exec(code, {}) # Module scope |
| 340 |
with self.assertRaisesRegex(SyntaxError, msg): |
| 341 |
exec(code, {}, {}) # Class scope |
| 342 |
with self.assertRaisesRegex(SyntaxError, msg): |
| 343 |
exec(f"lambda: {code}", {}) # Function scope |
| 344 |
|
| 345 |
def test_named_expression_invalid_dict_comprehension_iterable_expression(self): |
| 346 |
cases = [ |
| 347 |
("Top level", "{i: 1 for i in (i := range(5))}"), |
| 348 |
("Inside tuple", "{i: 1 for i in (2, 3, i := range(5))}"), |
| 349 |
("Inside list", "{i: 1 for i in [2, 3, i := range(5)]}"), |
| 350 |
("Different name", "{i: 1 for i in (j := range(5))}"), |
| 351 |
("Lambda expression", "{i: 1 for i in (lambda:(j := range(5)))()}"), |
| 352 |
("Inner loop", "{i: 1 for i in range(5) for j in (i := range(5))}"), |
| 353 |
("Nested comprehension", "{i: 1 for i in {j: 2 for j in (k := range(5))}}"), |
| 354 |
("Nested comprehension condition", "{i: 1 for i in {j: 2 for j in range(5) if (j := True)}}"), |
| 355 |
("Nested comprehension body", "{i: 1 for i in {(j := True) for j in range(5)}}"), |
| 356 |
] |
| 357 |
msg = "assignment expression cannot be used in a comprehension iterable expression" |
| 358 |
for case, code in cases: |
| 359 |
with self.subTest(case=case): |
| 360 |
with self.assertRaisesRegex(SyntaxError, msg): |
| 361 |
exec(code, {}) # Module scope |
| 362 |
with self.assertRaisesRegex(SyntaxError, msg): |
| 363 |
exec(code, {}, {}) # Class scope |
| 364 |
with self.assertRaisesRegex(SyntaxError, msg): |
| 365 |
exec(f"lambda: {code}", {}) # Function scope |
| 366 |
|
| 367 |
def test_named_expression_invalid_mangled_class_variables(self): |
| 368 |
code = """class Foo: |
| 369 |
def bar(self): |
| 370 |
[[(__x:=2) for _ in range(2)] for __x in range(2)] |
| 371 |
""" |
| 372 |
|
| 373 |
with self.assertRaisesRegex(SyntaxError, |
| 374 |
"assignment expression cannot rebind comprehension iteration variable '__x'"): |
| 375 |
exec(code, {}, {}) |
| 376 |
|
| 377 |
|
| 378 |
class NamedExpressionAssignmentTest(unittest.TestCase): |
| 379 |
|
| 380 |
def test_named_expression_assignment_01(self): |
| 381 |
(a := 10) |
| 382 |
|
| 383 |
self.assertEqual(a, 10) |
| 384 |
|
| 385 |
def test_named_expression_assignment_02(self): |
| 386 |
a = 20 |
| 387 |
(a := a) |
| 388 |
|
| 389 |
self.assertEqual(a, 20) |
| 390 |
|
| 391 |
def test_named_expression_assignment_03(self): |
| 392 |
(total := 1 + 2) |
| 393 |
|
| 394 |
self.assertEqual(total, 3) |
| 395 |
|
| 396 |
def test_named_expression_assignment_04(self): |
| 397 |
(info := (1, 2, 3)) |
| 398 |
|
| 399 |
self.assertEqual(info, (1, 2, 3)) |
| 400 |
|
| 401 |
def test_named_expression_assignment_05(self): |
| 402 |
(x := 1, 2) |
| 403 |
|
| 404 |
self.assertEqual(x, 1) |
| 405 |
|
| 406 |
def test_named_expression_assignment_06(self): |
| 407 |
(z := (y := (x := 0))) |
| 408 |
|
| 409 |
self.assertEqual(x, 0) |
| 410 |
self.assertEqual(y, 0) |
| 411 |
self.assertEqual(z, 0) |
| 412 |
|
| 413 |
def test_named_expression_assignment_07(self): |
| 414 |
(loc := (1, 2)) |
| 415 |
|
| 416 |
self.assertEqual(loc, (1, 2)) |
| 417 |
|
| 418 |
def test_named_expression_assignment_08(self): |
| 419 |
if spam := "eggs": |
| 420 |
self.assertEqual(spam, "eggs") |
| 421 |
else: self.fail("variable was not assigned using named expression") |
| 422 |
|
| 423 |
def test_named_expression_assignment_09(self): |
| 424 |
if True and (spam := True): |
| 425 |
self.assertTrue(spam) |
| 426 |
else: self.fail("variable was not assigned using named expression") |
| 427 |
|
| 428 |
def test_named_expression_assignment_10(self): |
| 429 |
if (match := 10) == 10: |
| 430 |
self.assertEqual(match, 10) |
| 431 |
else: self.fail("variable was not assigned using named expression") |
| 432 |
|
| 433 |
def test_named_expression_assignment_11(self): |
| 434 |
def spam(a): |
| 435 |
return a |
| 436 |
input_data = [1, 2, 3] |
| 437 |
res = [(x, y, x/y) for x in input_data if (y := spam(x)) > 0] |
| 438 |
|
| 439 |
self.assertEqual(res, [(1, 1, 1.0), (2, 2, 1.0), (3, 3, 1.0)]) |
| 440 |
|
| 441 |
def test_named_expression_assignment_12(self): |
| 442 |
def spam(a): |
| 443 |
return a |
| 444 |
res = [[y := spam(x), x/y] for x in range(1, 5)] |
| 445 |
|
| 446 |
self.assertEqual(res, [[1, 1.0], [2, 1.0], [3, 1.0], [4, 1.0]]) |
| 447 |
|
| 448 |
def test_named_expression_assignment_13(self): |
| 449 |
length = len(lines := [1, 2]) |
| 450 |
|
| 451 |
self.assertEqual(length, 2) |
| 452 |
self.assertEqual(lines, [1,2]) |
| 453 |
|
| 454 |
def test_named_expression_assignment_14(self): |
| 455 |
""" |
| 456 |
Where all variables are positive integers, and a is at least as large |
| 457 |
as the n'th root of x, this algorithm returns the floor of the n'th |
| 458 |
root of x (and roughly doubling the number of accurate bits per |
| 459 |
iteration): |
| 460 |
""" |
| 461 |
a = 9 |
| 462 |
n = 2 |
| 463 |
x = 3 |
| 464 |
|
| 465 |
while a > (d := x // a**(n-1)): |
| 466 |
a = ((n-1)*a + d) // n |
| 467 |
|
| 468 |
self.assertEqual(a, 1) |
| 469 |
|
| 470 |
def test_named_expression_assignment_15(self): |
| 471 |
while a := False: |
| 472 |
self.fail("While body executed") # This will not run |
| 473 |
|
| 474 |
self.assertEqual(a, False) |
| 475 |
|
| 476 |
def test_named_expression_assignment_16(self): |
| 477 |
a, b = 1, 2 |
| 478 |
fib = {(c := a): (a := b) + (b := a + c) - b for __ in range(6)} |
| 479 |
self.assertEqual(fib, {1: 2, 2: 3, 3: 5, 5: 8, 8: 13, 13: 21}) |
| 480 |
|
| 481 |
def test_named_expression_assignment_17(self): |
| 482 |
a = [1] |
| 483 |
element = a[b:=0] |
| 484 |
self.assertEqual(b, 0) |
| 485 |
self.assertEqual(element, a[0]) |
| 486 |
|
| 487 |
def test_named_expression_assignment_18(self): |
| 488 |
class TwoDimensionalList: |
| 489 |
def __init__(self, two_dimensional_list): |
| 490 |
self.two_dimensional_list = two_dimensional_list |
| 491 |
|
| 492 |
def __getitem__(self, index): |
| 493 |
return self.two_dimensional_list[index[0]][index[1]] |
| 494 |
|
| 495 |
a = TwoDimensionalList([[1], [2]]) |
| 496 |
element = a[b:=0, c:=0] |
| 497 |
self.assertEqual(b, 0) |
| 498 |
self.assertEqual(c, 0) |
| 499 |
self.assertEqual(element, a.two_dimensional_list[b][c]) |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
class NamedExpressionScopeTest(unittest.TestCase): |
| 504 |
|
| 505 |
def test_named_expression_scope_01(self): |
| 506 |
code = """def spam(): |
| 507 |
(a := 5) |
| 508 |
print(a)""" |
| 509 |
|
| 510 |
with self.assertRaisesRegex(NameError, "name 'a' is not defined"): |
| 511 |
exec(code, {}, {}) |
| 512 |
|
| 513 |
def test_named_expression_scope_02(self): |
| 514 |
total = 0 |
| 515 |
partial_sums = [total := total + v for v in range(5)] |
| 516 |
|
| 517 |
self.assertEqual(partial_sums, [0, 1, 3, 6, 10]) |
| 518 |
self.assertEqual(total, 10) |
| 519 |
|
| 520 |
def test_named_expression_scope_03(self): |
| 521 |
containsOne = any((lastNum := num) == 1 for num in [1, 2, 3]) |
| 522 |
|
| 523 |
self.assertTrue(containsOne) |
| 524 |
self.assertEqual(lastNum, 1) |
| 525 |
|
| 526 |
def test_named_expression_scope_04(self): |
| 527 |
def spam(a): |
| 528 |
return a |
| 529 |
res = [[y := spam(x), x/y] for x in range(1, 5)] |
| 530 |
|
| 531 |
self.assertEqual(y, 4) |
| 532 |
|
| 533 |
def test_named_expression_scope_05(self): |
| 534 |
def spam(a): |
| 535 |
return a |
| 536 |
input_data = [1, 2, 3] |
| 537 |
res = [(x, y, x/y) for x in input_data if (y := spam(x)) > 0] |
| 538 |
|
| 539 |
self.assertEqual(res, [(1, 1, 1.0), (2, 2, 1.0), (3, 3, 1.0)]) |
| 540 |
self.assertEqual(y, 3) |
| 541 |
|
| 542 |
def test_named_expression_scope_06(self): |
| 543 |
res = [[spam := i for i in range(3)] for j in range(2)] |
| 544 |
|
| 545 |
self.assertEqual(res, [[0, 1, 2], [0, 1, 2]]) |
| 546 |
self.assertEqual(spam, 2) |
| 547 |
|
| 548 |
def test_named_expression_scope_07(self): |
| 549 |
len(lines := [1, 2]) |
| 550 |
|
| 551 |
self.assertEqual(lines, [1, 2]) |
| 552 |
|
| 553 |
def test_named_expression_scope_08(self): |
| 554 |
def spam(a): |
| 555 |
return a |
| 556 |
|
| 557 |
def eggs(b): |
| 558 |
return b * 2 |
| 559 |
|
| 560 |
res = [spam(a := eggs(b := h)) for h in range(2)] |
| 561 |
|
| 562 |
self.assertEqual(res, [0, 2]) |
| 563 |
self.assertEqual(a, 2) |
| 564 |
self.assertEqual(b, 1) |
| 565 |
|
| 566 |
def test_named_expression_scope_09(self): |
| 567 |
def spam(a): |
| 568 |
return a |
| 569 |
|
| 570 |
def eggs(b): |
| 571 |
return b * 2 |
| 572 |
|
| 573 |
res = [spam(a := eggs(a := h)) for h in range(2)] |
| 574 |
|
| 575 |
self.assertEqual(res, [0, 2]) |
| 576 |
self.assertEqual(a, 2) |
| 577 |
|
| 578 |
def test_named_expression_scope_10(self): |
| 579 |
res = [b := [a := 1 for i in range(2)] for j in range(2)] |
| 580 |
|
| 581 |
self.assertEqual(res, [[1, 1], [1, 1]]) |
| 582 |
self.assertEqual(a, 1) |
| 583 |
self.assertEqual(b, [1, 1]) |
| 584 |
|
| 585 |
def test_named_expression_scope_11(self): |
| 586 |
res = [j := i for i in range(5)] |
| 587 |
|
| 588 |
self.assertEqual(res, [0, 1, 2, 3, 4]) |
| 589 |
self.assertEqual(j, 4) |
| 590 |
|
| 591 |
def test_named_expression_scope_17(self): |
| 592 |
b = 0 |
| 593 |
res = [b := i + b for i in range(5)] |
| 594 |
|
| 595 |
self.assertEqual(res, [0, 1, 3, 6, 10]) |
| 596 |
self.assertEqual(b, 10) |
| 597 |
|
| 598 |
def test_named_expression_scope_18(self): |
| 599 |
def spam(a): |
| 600 |
return a |
| 601 |
|
| 602 |
res = spam(b := 2) |
| 603 |
|
| 604 |
self.assertEqual(res, 2) |
| 605 |
self.assertEqual(b, 2) |
| 606 |
|
| 607 |
def test_named_expression_scope_19(self): |
| 608 |
def spam(a): |
| 609 |
return a |
| 610 |
|
| 611 |
res = spam((b := 2)) |
| 612 |
|
| 613 |
self.assertEqual(res, 2) |
| 614 |
self.assertEqual(b, 2) |
| 615 |
|
| 616 |
def test_named_expression_scope_20(self): |
| 617 |
def spam(a): |
| 618 |
return a |
| 619 |
|
| 620 |
res = spam(a=(b := 2)) |
| 621 |
|
| 622 |
self.assertEqual(res, 2) |
| 623 |
self.assertEqual(b, 2) |
| 624 |
|
| 625 |
def test_named_expression_scope_21(self): |
| 626 |
def spam(a, b): |
| 627 |
return a + b |
| 628 |
|
| 629 |
res = spam(c := 2, b=1) |
| 630 |
|
| 631 |
self.assertEqual(res, 3) |
| 632 |
self.assertEqual(c, 2) |
| 633 |
|
| 634 |
def test_named_expression_scope_22(self): |
| 635 |
def spam(a, b): |
| 636 |
return a + b |
| 637 |
|
| 638 |
res = spam((c := 2), b=1) |
| 639 |
|
| 640 |
self.assertEqual(res, 3) |
| 641 |
self.assertEqual(c, 2) |
| 642 |
|
| 643 |
def test_named_expression_scope_23(self): |
| 644 |
def spam(a, b): |
| 645 |
return a + b |
| 646 |
|
| 647 |
res = spam(b=(c := 2), a=1) |
| 648 |
|
| 649 |
self.assertEqual(res, 3) |
| 650 |
self.assertEqual(c, 2) |
| 651 |
|
| 652 |
def test_named_expression_scope_24(self): |
| 653 |
a = 10 |
| 654 |
def spam(): |
| 655 |
nonlocal a |
| 656 |
(a := 20) |
| 657 |
spam() |
| 658 |
|
| 659 |
self.assertEqual(a, 20) |
| 660 |
|
| 661 |
def test_named_expression_scope_25(self): |
| 662 |
ns = {} |
| 663 |
code = """a = 10 |
| 664 |
def spam(): |
| 665 |
global a |
| 666 |
(a := 20) |
| 667 |
spam()""" |
| 668 |
|
| 669 |
exec(code, ns, {}) |
| 670 |
|
| 671 |
self.assertEqual(ns["a"], 20) |
| 672 |
|
| 673 |
def test_named_expression_variable_reuse_in_comprehensions(self): |
| 674 |
# The compiler is expected to raise syntax error for comprehension |
| 675 |
# iteration variables, but should be fine with rebinding of other |
| 676 |
# names (e.g. globals, nonlocals, other assignment expressions) |
| 677 |
|
| 678 |
# The cases are all defined to produce the same expected result |
| 679 |
# Each comprehension is checked at both function scope and module scope |
| 680 |
rebinding = "[x := i for i in range(3) if (x := i) or not x]" |
| 681 |
filter_ref = "[x := i for i in range(3) if x or not x]" |
| 682 |
body_ref = "[x for i in range(3) if (x := i) or not x]" |
| 683 |
nested_ref = "[j for i in range(3) if x or not x for j in range(3) if (x := i)][:-3]" |
| 684 |
cases = [ |
| 685 |
("Rebind global", f"x = 1; result = {rebinding}"), |
| 686 |
("Rebind nonlocal", f"result, x = (lambda x=1: ({rebinding}, x))()"), |
| 687 |
("Filter global", f"x = 1; result = {filter_ref}"), |
| 688 |
("Filter nonlocal", f"result, x = (lambda x=1: ({filter_ref}, x))()"), |
| 689 |
("Body global", f"x = 1; result = {body_ref}"), |
| 690 |
("Body nonlocal", f"result, x = (lambda x=1: ({body_ref}, x))()"), |
| 691 |
("Nested global", f"x = 1; result = {nested_ref}"), |
| 692 |
("Nested nonlocal", f"result, x = (lambda x=1: ({nested_ref}, x))()"), |
| 693 |
] |
| 694 |
for case, code in cases: |
| 695 |
with self.subTest(case=case): |
| 696 |
ns = {} |
| 697 |
exec(code, ns) |
| 698 |
self.assertEqual(ns["x"], 2) |
| 699 |
self.assertEqual(ns["result"], [0, 1, 2]) |
| 700 |
|
| 701 |
def test_named_expression_global_scope(self): |
| 702 |
sentinel = object() |
| 703 |
global GLOBAL_VAR |
| 704 |
def f(): |
| 705 |
global GLOBAL_VAR |
| 706 |
[GLOBAL_VAR := sentinel for _ in range(1)] |
| 707 |
self.assertEqual(GLOBAL_VAR, sentinel) |
| 708 |
try: |
| 709 |
f() |
| 710 |
self.assertEqual(GLOBAL_VAR, sentinel) |
| 711 |
finally: |
| 712 |
GLOBAL_VAR = None |
| 713 |
|
| 714 |
def test_named_expression_global_scope_no_global_keyword(self): |
| 715 |
sentinel = object() |
| 716 |
def f(): |
| 717 |
GLOBAL_VAR = None |
| 718 |
[GLOBAL_VAR := sentinel for _ in range(1)] |
| 719 |
self.assertEqual(GLOBAL_VAR, sentinel) |
| 720 |
f() |
| 721 |
self.assertEqual(GLOBAL_VAR, None) |
| 722 |
|
| 723 |
def test_named_expression_nonlocal_scope(self): |
| 724 |
sentinel = object() |
| 725 |
def f(): |
| 726 |
nonlocal_var = None |
| 727 |
def g(): |
| 728 |
nonlocal nonlocal_var |
| 729 |
[nonlocal_var := sentinel for _ in range(1)] |
| 730 |
g() |
| 731 |
self.assertEqual(nonlocal_var, sentinel) |
| 732 |
f() |
| 733 |
|
| 734 |
def test_named_expression_nonlocal_scope_no_nonlocal_keyword(self): |
| 735 |
sentinel = object() |
| 736 |
def f(): |
| 737 |
nonlocal_var = None |
| 738 |
def g(): |
| 739 |
[nonlocal_var := sentinel for _ in range(1)] |
| 740 |
g() |
| 741 |
self.assertEqual(nonlocal_var, None) |
| 742 |
f() |
| 743 |
|
| 744 |
def test_named_expression_scope_in_genexp(self): |
| 745 |
a = 1 |
| 746 |
b = [1, 2, 3, 4] |
| 747 |
genexp = (c := i + a for i in b) |
| 748 |
|
| 749 |
self.assertNotIn("c", locals()) |
| 750 |
for idx, elem in enumerate(genexp): |
| 751 |
self.assertEqual(elem, b[idx] + a) |
| 752 |
|
| 753 |
def test_named_expression_scope_mangled_names(self): |
| 754 |
class Foo: |
| 755 |
def f(self_): |
| 756 |
global __x1 |
| 757 |
__x1 = 0 |
| 758 |
[_Foo__x1 := 1 for a in [2]] |
| 759 |
self.assertEqual(__x1, 1) |
| 760 |
[__x1 := 2 for a in [3]] |
| 761 |
self.assertEqual(__x1, 2) |
| 762 |
|
| 763 |
Foo().f() |
| 764 |
self.assertEqual(_Foo__x1, 2) |
| 765 |
|
| 766 |
if __name__ == "__main__": |
| 767 |
unittest.main() |