Commit 3f3c20b
patchedast: don't match tokens inside string literals
The source walker locates tokens with a plain `str.index`, which cannot
tell a real token from the same characters inside a literal. Two ways
that went wrong:
- `_good_token` looked for the last `#` before the token to decide
whether it sat in a comment. A `#` inside a string opens no comment,
but it made the walker skip the rest of that line -- and the closing
paren with it.
- `consume(")")` matched the `)` of `f"see (#123)"`, or one buried in a
triple-quoted SQL blob, and carried on from a position the tree knows
nothing about.
Neither fails where it happens. The walker keeps going against a
mis-parsed offset and surfaces much later as a MismatchedTokenError, or
an AttributeError, naming an unrelated node several statements away.
Replace the textual guess with the tokenizer: `_Source` now records the
offset range of every string literal and comment once, and looks a
candidate offset up in it. That is exact, where a scan for quotes cannot
know whether it began inside one -- the reason the previous heuristic
could not be repaired in place.
f-strings need one distinction. From 3.12 the tokenizer splits them, so
taking only FSTRING_MIDDLE keeps the replacement fields visible: they
are code, and the walker consumes them. Before 3.12 an f-string is a
single STRING token, fields included, so `consume` compares the literal
the walker is itself working inside against the match's, rather than
skipping every literal it meets.
On an unparseable source the tokenizer yields nothing and the plain
textual search of before is left untouched.
Measured over a 250-file private codebase: 4 files failed to walk
before, 2 after, with no file newly broken. Both remaining failures are
a separate defect.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01V5GqJKaXZzFzmFEYgo4pPR1 parent d2c5127 commit 3f3c20b
2 files changed
Lines changed: 120 additions & 15 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
| 3 | + | |
2 | 4 | | |
3 | 5 | | |
| 6 | + | |
4 | 7 | | |
5 | 8 | | |
6 | 9 | | |
| |||
907 | 910 | | |
908 | 911 | | |
909 | 912 | | |
| 913 | + | |
910 | 914 | | |
911 | 915 | | |
912 | 916 | | |
913 | 917 | | |
914 | 918 | | |
915 | | - | |
| 919 | + | |
916 | 920 | | |
917 | | - | |
918 | | - | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
919 | 928 | | |
920 | 929 | | |
921 | 930 | | |
| |||
957 | 966 | | |
958 | 967 | | |
959 | 968 | | |
960 | | - | |
| 969 | + | |
961 | 970 | | |
962 | | - | |
963 | | - | |
964 | | - | |
965 | | - | |
966 | | - | |
967 | | - | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
968 | 1032 | | |
969 | | - | |
970 | | - | |
971 | | - | |
972 | | - | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
| 1037 | + | |
| 1038 | + | |
| 1039 | + | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
| 1043 | + | |
| 1044 | + | |
| 1045 | + | |
973 | 1046 | | |
974 | 1047 | | |
975 | 1048 | | |
| |||
999 | 1072 | | |
1000 | 1073 | | |
1001 | 1074 | | |
1002 | | - | |
| 1075 | + | |
1003 | 1076 | | |
1004 | 1077 | | |
1005 | 1078 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
280 | 280 | | |
281 | 281 | | |
282 | 282 | | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
283 | 315 | | |
284 | 316 | | |
285 | 317 | | |
| |||
0 commit comments