Skip to content

Commit b4e561a

Browse files
committed
Python: Split up except Alpha, Beta: case across language versions
This is the case where the difference in semantics actually changes the output of the query, so we have to maintain language-specific tests for this. The remaining tests stay in the shared directory, as they have the same behaviour on both versions of Python. I've also added a comment pointing this out, in case future readers get confused as to why `except Alpha, Beta: ...` is missing.
1 parent 7d9ae21 commit b4e561a

11 files changed

Lines changed: 42 additions & 27 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| relaxed_except.py:2:1:2:43 | Import | Import of 'Beta' is not used. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Imports/UnusedImport.ql
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# In Python 2, Beta is an alias binding; in Python 3, it is an exception type.
2+
from relaxed_except_defs import Alpha, Beta
3+
4+
5+
def unparenthesized():
6+
try:
7+
pass
8+
except Alpha, Beta:
9+
raise
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Alpha(Exception):
2+
pass
3+
4+
5+
class Beta(Exception):
6+
pass

python/ql/test/3/query-tests/Imports/unused/UnusedImport.expected

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Imports/UnusedImport.ql
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# In Python 2, Beta is an alias binding; in Python 3, it is an exception type.
2+
from relaxed_except_defs import Alpha, Beta
3+
4+
5+
def unparenthesized():
6+
try:
7+
pass
8+
except Alpha, Beta:
9+
raise
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Alpha(Exception):
2+
pass
3+
4+
5+
class Beta(Exception):
6+
pass

python/ql/test/query-tests/Imports/unused/UnusedImport.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
| imports_test.py:27:1:27:25 | Import | Import of 'func2' is not used. |
77
| imports_test.py:34:1:34:14 | Import | Import of 'module2' is not used. |
88
| imports_test.py:116:1:116:41 | Import | Import of 'not_a_fixture' is not used. |
9-
| relaxed_except.py:12:1:12:68 | Import | Import of 'NeverUsed' is not used. |
9+
| relaxed_except.py:3:1:3:55 | Import | Import of 'NeverUsed' is not used. |

python/ql/test/query-tests/Imports/unused/relaxed_except.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,14 @@
11
# PEP 758 allows unparenthesized exception types when there is no `as` clause.
2-
# Every name below is used as an exception type, so no import here is unused.
3-
# `NeverUsed` is imported and never used, and is the one expected result.
4-
#
5-
# Each name appears in exactly one clause on purpose: a name that also appeared
6-
# in a parenthesized clause would be a use regardless, and would mask the
7-
# behaviour under test.
8-
#
9-
# This file deliberately contains no `except A, B, C:` clause. Three or more
10-
# unparenthesized types fail the default parser, which sends the whole file to
11-
# the tree-sitter parser and would likewise mask it.
12-
from relaxed_except_defs import Alpha, Beta, Delta, Gamma, NeverUsed
13-
14-
15-
def unparenthesized():
16-
try:
17-
pass
18-
except Alpha, Beta:
19-
raise
2+
# Every import below except `NeverUsed` is used, leaving one expected result.
3+
from relaxed_except_defs import Delta, Gamma, NeverUsed
204

215

6+
# The `except Alpha, Beta:` case is covered separately in the Python 2 and
7+
# Python 3 test directories because its semantics differ between versions.
8+
#
9+
# The version-specific files deliberately contain no `except A, B, C:` clause.
10+
# Three or more unparenthesized types fail the default parser, which sends the
11+
# whole file to the tree-sitter parser and would mask the behavior under test.
2212
def parenthesized():
2313
try:
2414
pass

0 commit comments

Comments
 (0)