Repository navigation
Commit 119e7d5
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.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>1 parent 01d8827 commit 119e7d5
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 | |
|---|---|---|---|
| |||
568 | 568 | | |
569 | 569 | | |
570 | 570 | | |
571 | | - | |
572 | | - | |
573 | | - | |
574 | | - | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
575 | 581 | | |
576 | 582 | | |
577 | | - | |
578 | | - | |
| 583 | + | |
579 | 584 | | |
580 | 585 | | |
581 | 586 | | |
| |||
1002 | 1007 | | |
1003 | 1008 | | |
1004 | 1009 | | |
1005 | | - | |
| 1010 | + | |
1006 | 1011 | | |
1007 | 1012 | | |
1008 | 1013 | | |
1009 | 1014 | | |
1010 | 1015 | | |
1011 | 1016 | | |
1012 | | - | |
1013 | | - | |
1014 | | - | |
| 1017 | + | |
| 1018 | + | |
1015 | 1019 | | |
1016 | 1020 | | |
1017 | 1021 | | |
1018 | 1022 | | |
1019 | 1023 | | |
1020 | 1024 | | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
1021 | 1030 | | |
1022 | 1031 | | |
1023 | 1032 | | |
1024 | 1033 | | |
1025 | | - | |
1026 | | - | |
1027 | | - | |
1028 | | - | |
1029 | | - | |
1030 | | - | |
1031 | | - | |
1032 | | - | |
| 1034 | + | |
| 1035 | + | |
1033 | 1036 | | |
1034 | 1037 | | |
1035 | 1038 | | |
| |||
1049 | 1052 | | |
1050 | 1053 | | |
1051 | 1054 | | |
1052 | | - | |
1053 | | - | |
1054 | | - | |
| 1055 | + | |
| 1056 | + | |
1055 | 1057 | | |
1056 | 1058 | | |
1057 | 1059 | | |
| |||
1068 | 1070 | | |
1069 | 1071 | | |
1070 | 1072 | | |
1071 | | - | |
1072 | | - | |
| 1073 | + | |
| 1074 | + | |
1073 | 1075 | | |
1074 | | - | |
| 1076 | + | |
1075 | 1077 | | |
1076 | 1078 | | |
1077 | 1079 | | |
| |||
1205 | 1207 | | |
1206 | 1208 | | |
1207 | 1209 | | |
| 1210 | + | |
| 1211 | + | |
| 1212 | + | |
| 1213 | + | |
| 1214 | + | |
1208 | 1215 | | |
1209 | 1216 | | |
1210 | 1217 | | |
| |||
| 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