Repository navigation
Commit 56e3c3b
committed
fix(rewrite): freeze operands against any later side effect (#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 aa87473 commit 56e3c3b
3 files changed
Lines changed: 84 additions & 24 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 | |
|---|---|---|---|
| |||
530 | 530 | | |
531 | 531 | | |
532 | 532 | | |
533 | | - | |
534 | | - | |
535 | | - | |
536 | | - | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
537 | 543 | | |
538 | 544 | | |
539 | | - | |
540 | | - | |
| 545 | + | |
541 | 546 | | |
542 | 547 | | |
543 | 548 | | |
| |||
964 | 969 | | |
965 | 970 | | |
966 | 971 | | |
967 | | - | |
| 972 | + | |
968 | 973 | | |
969 | 974 | | |
970 | 975 | | |
971 | 976 | | |
972 | 977 | | |
973 | 978 | | |
974 | | - | |
975 | | - | |
976 | | - | |
| 979 | + | |
| 980 | + | |
977 | 981 | | |
978 | 982 | | |
979 | 983 | | |
980 | 984 | | |
981 | 985 | | |
982 | 986 | | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
983 | 992 | | |
984 | 993 | | |
985 | 994 | | |
986 | 995 | | |
987 | | - | |
988 | | - | |
989 | | - | |
990 | | - | |
991 | | - | |
992 | | - | |
993 | | - | |
994 | | - | |
| 996 | + | |
| 997 | + | |
995 | 998 | | |
996 | 999 | | |
997 | 1000 | | |
| |||
1011 | 1014 | | |
1012 | 1015 | | |
1013 | 1016 | | |
1014 | | - | |
1015 | | - | |
1016 | | - | |
| 1017 | + | |
| 1018 | + | |
1017 | 1019 | | |
1018 | 1020 | | |
1019 | 1021 | | |
| |||
1030 | 1032 | | |
1031 | 1033 | | |
1032 | 1034 | | |
1033 | | - | |
1034 | | - | |
| 1035 | + | |
| 1036 | + | |
1035 | 1037 | | |
1036 | | - | |
| 1038 | + | |
1037 | 1039 | | |
1038 | 1040 | | |
1039 | 1041 | | |
| |||
1167 | 1169 | | |
1168 | 1170 | | |
1169 | 1171 | | |
| 1172 | + | |
| 1173 | + | |
| 1174 | + | |
| 1175 | + | |
| 1176 | + | |
1170 | 1177 | | |
1171 | 1178 | | |
1172 | 1179 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1201 | 1201 | | |
1202 | 1202 | | |
1203 | 1203 | | |
| 1204 | + | |
| 1205 | + | |
| 1206 | + | |
| 1207 | + | |
| 1208 | + | |
| 1209 | + | |
| 1210 | + | |
| 1211 | + | |
| 1212 | + | |
| 1213 | + | |
| 1214 | + | |
| 1215 | + | |
| 1216 | + | |
| 1217 | + | |
| 1218 | + | |
| 1219 | + | |
| 1220 | + | |
| 1221 | + | |
| 1222 | + | |
| 1223 | + | |
| 1224 | + | |
| 1225 | + | |
| 1226 | + | |
| 1227 | + | |
| 1228 | + | |
| 1229 | + | |
| 1230 | + | |
| 1231 | + | |
| 1232 | + | |
| 1233 | + | |
| 1234 | + | |
| 1235 | + | |
| 1236 | + | |
| 1237 | + | |
| 1238 | + | |
| 1239 | + | |
| 1240 | + | |
| 1241 | + | |
| 1242 | + | |
| 1243 | + | |
| 1244 | + | |
| 1245 | + | |
| 1246 | + | |
| 1247 | + | |
| 1248 | + | |
| 1249 | + | |
| 1250 | + | |
| 1251 | + | |
| 1252 | + | |
| 1253 | + | |
| 1254 | + | |
| 1255 | + | |
1204 | 1256 | | |
1205 | 1257 | | |
1206 | 1258 | | |
| |||
0 commit comments