Commit 896d8d7
Python: add query tests for unparenthesized except chains of every length
The parser test added with the fix pins the AST. These pin the behaviour a user
actually sees, through the real extraction path including the tree-sitter
fallback, and they cover chains longer than two.
Chains of three or more behave differently from chains of two, which is worth
having written down. `except A, B, C:` fails the default parser outright, so
`Module.py_ast` falls back to tree-sitter and the result is already correct.
`except A, B:` parses successfully under the Python 2 reading, so the fallback
never fires and the bad AST reaches the queries. That is why only the two-type
form produced a false positive.
It also means the two cases cannot share a file: any three-type clause sends
the whole file to tree-sitter and masks the two-type behaviour. Hence
relaxed_except.py and relaxed_except_long.py, with a comment in each saying so.
Each name is used in exactly one clause for the same reason -- a name reused in
a parenthesized clause is a use regardless, and hides the defect.
Verified by reverting the extractor fix in a 2.26.3 bundle: relaxed_except.py
then reports `Import of 'Beta' is not used.` and the test fails.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>1 parent 66573bb commit 896d8d7
4 files changed
Lines changed: 70 additions & 0 deletions
File tree
- python/ql/test/query-tests/Imports/unused
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
Lines changed: 26 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
Lines changed: 26 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
Lines changed: 17 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
0 commit comments