Repository navigation
Commit 382db6b
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 77dd763 commit 382db6b
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 | |
|---|---|---|---|
| |||
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 | + | |
| 1001 | + | |
993 | 1002 | | |
994 | 1003 | | |
995 | 1004 | | |
996 | 1005 | | |
997 | | - | |
998 | | - | |
999 | | - | |
1000 | | - | |
1001 | | - | |
1002 | | - | |
1003 | | - | |
1004 | | - | |
| 1006 | + | |
| 1007 | + | |
1005 | 1008 | | |
1006 | 1009 | | |
1007 | 1010 | | |
| |||
1021 | 1024 | | |
1022 | 1025 | | |
1023 | 1026 | | |
1024 | | - | |
1025 | | - | |
1026 | | - | |
| 1027 | + | |
| 1028 | + | |
1027 | 1029 | | |
1028 | 1030 | | |
1029 | 1031 | | |
| |||
1040 | 1042 | | |
1041 | 1043 | | |
1042 | 1044 | | |
1043 | | - | |
1044 | | - | |
| 1045 | + | |
| 1046 | + | |
1045 | 1047 | | |
1046 | | - | |
| 1048 | + | |
1047 | 1049 | | |
1048 | 1050 | | |
1049 | 1051 | | |
| |||
1177 | 1179 | | |
1178 | 1180 | | |
1179 | 1181 | | |
| 1182 | + | |
| 1183 | + | |
| 1184 | + | |
| 1185 | + | |
| 1186 | + | |
1180 | 1187 | | |
1181 | 1188 | | |
1182 | 1189 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1141 | 1141 | | |
1142 | 1142 | | |
1143 | 1143 | | |
| 1144 | + | |
| 1145 | + | |
| 1146 | + | |
| 1147 | + | |
| 1148 | + | |
| 1149 | + | |
| 1150 | + | |
| 1151 | + | |
| 1152 | + | |
| 1153 | + | |
| 1154 | + | |
| 1155 | + | |
| 1156 | + | |
| 1157 | + | |
| 1158 | + | |
| 1159 | + | |
| 1160 | + | |
| 1161 | + | |
| 1162 | + | |
| 1163 | + | |
| 1164 | + | |
| 1165 | + | |
| 1166 | + | |
| 1167 | + | |
| 1168 | + | |
| 1169 | + | |
| 1170 | + | |
| 1171 | + | |
| 1172 | + | |
| 1173 | + | |
| 1174 | + | |
| 1175 | + | |
| 1176 | + | |
| 1177 | + | |
| 1178 | + | |
| 1179 | + | |
| 1180 | + | |
| 1181 | + | |
| 1182 | + | |
| 1183 | + | |
| 1184 | + | |
| 1185 | + | |
| 1186 | + | |
| 1187 | + | |
| 1188 | + | |
| 1189 | + | |
| 1190 | + | |
| 1191 | + | |
| 1192 | + | |
| 1193 | + | |
| 1194 | + | |
| 1195 | + | |
1144 | 1196 | | |
1145 | 1197 | | |
1146 | 1198 | | |
| |||
0 commit comments