Repository navigation
Commit edc350c
committed
fix(rewrite): freeze operands against any later side effect (pytest-dev#14820)
visit_operand() froze a name only when a walrus operator in a later
operand targeted it. A call can rebind just as well, through global or
nonlocal, and then the name -- still unhoisted, still read when the
enclosing expression is assembled -- sees the new binding:
count = 0
def bump():
global count
count = 99
return 0
assert count == bump() # Python compares 0 == 0 and passes
There is no way to tell from the assert which names a call might rebind,
so _walrus_targets() becomes _can_rebind(): a name is frozen whenever
anything that follows it can execute code at all.
That sounds expensive and is not. An operand that was already hoisted
needs no freeze, so `assert len(items) == expected` rewrites unchanged;
only the bare-name-then-call shape gains one assignment. visit_BoolOp
used the same pre-scan and is generalized with it, which leaves one rule
in one place instead of two spellings of half of it.
Closes the order-name-rebound-by-call group in the coverage matrix.1 parent 2480b28 commit edc350c
3 files changed
Lines changed: 26 additions & 33 deletions
File tree
- changelog
- src/_pytest/assertion
- testing
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
540 | 540 | | |
541 | 541 | | |
542 | 542 | | |
543 | | - | |
544 | | - | |
545 | | - | |
546 | | - | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
547 | 553 | | |
548 | 554 | | |
549 | | - | |
550 | | - | |
| 555 | + | |
551 | 556 | | |
552 | 557 | | |
553 | 558 | | |
| |||
974 | 979 | | |
975 | 980 | | |
976 | 981 | | |
977 | | - | |
| 982 | + | |
978 | 983 | | |
979 | 984 | | |
980 | 985 | | |
981 | 986 | | |
982 | 987 | | |
983 | 988 | | |
984 | | - | |
985 | | - | |
986 | | - | |
| 989 | + | |
| 990 | + | |
987 | 991 | | |
988 | 992 | | |
989 | 993 | | |
990 | 994 | | |
991 | 995 | | |
992 | 996 | | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
993 | 1001 | | |
994 | 1002 | | |
995 | 1003 | | |
996 | 1004 | | |
997 | | - | |
998 | | - | |
999 | | - | |
1000 | | - | |
| 1005 | + | |
| 1006 | + | |
1001 | 1007 | | |
1002 | 1008 | | |
1003 | 1009 | | |
| |||
1019 | 1025 | | |
1020 | 1026 | | |
1021 | 1027 | | |
1022 | | - | |
1023 | | - | |
1024 | | - | |
| 1028 | + | |
| 1029 | + | |
1025 | 1030 | | |
1026 | 1031 | | |
1027 | 1032 | | |
| |||
1038 | 1043 | | |
1039 | 1044 | | |
1040 | 1045 | | |
1041 | | - | |
1042 | | - | |
| 1046 | + | |
| 1047 | + | |
1043 | 1048 | | |
1044 | | - | |
| 1049 | + | |
1045 | 1050 | | |
1046 | 1051 | | |
1047 | 1052 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
24 | | - | |
25 | 24 | | |
26 | 25 | | |
27 | 26 | | |
| |||
1284 | 1283 | | |
1285 | 1284 | | |
1286 | 1285 | | |
1287 | | - | |
1288 | | - | |
1289 | | - | |
1290 | | - | |
1291 | 1286 | | |
1292 | 1287 | | |
1293 | 1288 | | |
| |||
1305 | 1300 | | |
1306 | 1301 | | |
1307 | 1302 | | |
1308 | | - | |
1309 | | - | |
1310 | | - | |
1311 | | - | |
1312 | 1303 | | |
1313 | 1304 | | |
1314 | 1305 | | |
| |||
1324 | 1315 | | |
1325 | 1316 | | |
1326 | 1317 | | |
1327 | | - | |
1328 | | - | |
1329 | | - | |
1330 | | - | |
1331 | 1318 | | |
1332 | 1319 | | |
1333 | 1320 | | |
| |||
0 commit comments