Skip to content

Commit ad7c531

Browse files
SONARPY-1662 Fix FPs related to 'type' type (#1722)
1 parent d73c6e5 commit ad7c531

File tree

13 files changed

+89
-4
lines changed

13 files changed

+89
-4
lines changed

python-checks/src/main/java/org/sonar/python/checks/ArgumentTypeCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,6 @@ public static String matchBuiltinCategory(Predicate<String> predicate) {
197197
}
198198

199199
private static boolean isException(InferredType inferredType) {
200-
return inferredType.canBeOrExtend("unittest.mock.Mock");
200+
return inferredType.canBeOrExtend("unittest.mock.Mock") || inferredType.canBeOrExtend("type");
201201
}
202202
}

python-checks/src/main/java/org/sonar/python/checks/CaughtExceptionsCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ private static boolean canBeOrExtendBaseException(InferredType type) {
121121
// avoid FP on variables holding a tuple: SONARPY-713
122122
return true;
123123
}
124-
if (type.canOnlyBe("type")) {
124+
if (type.canBeOrExtend("type")) {
125125
// SONARPY-1666: Here we should only exclude type objects that represent Exception types
126126
return true;
127127
}

python-checks/src/main/java/org/sonar/python/checks/ExceptionCauseTypeCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private static void check(@Nullable Expression cause, SubscriptionContext ctx) {
6363
return;
6464
}
6565
InferredType causeType = cause.type();
66-
if (causeType.canOnlyBe("type")) {
66+
if (causeType.canBeOrExtend("type")) {
6767
// SONARPY-1666: Here we should only exclude type objects that represent Exception types
6868
return;
6969
}

python-checks/src/main/java/org/sonar/python/checks/IncorrectExceptionTypeCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void initialize(Context context) {
5555
if (hasGlobalOrNonLocalUsage(symbol)) {
5656
return;
5757
}
58-
if (!raisedExpression.type().canBeOrExtend(BASE_EXCEPTION) && !raisedExpression.type().canOnlyBe("type")) {
58+
if (!raisedExpression.type().canBeOrExtend(BASE_EXCEPTION) && !raisedExpression.type().canBeOrExtend("type")) {
5959
// SONARPY-1666: Here we should only exclude type objects that represent Exception types
6060
ctx.addIssue(raiseStatement, MESSAGE);
6161
return;

python-checks/src/test/resources/checks/argumentType.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,14 @@ def takes_int(some_parameter: int) -> int:
256256
takes_boolean(1.0) # Noncompliant
257257
takes_int(True)
258258
takes_boolean(42) # FN
259+
260+
261+
def no_fp_on_enum_types():
262+
from enum import Enum
263+
264+
class EnumA(Enum):
265+
A = 1
266+
B = 2
267+
C = 3
268+
269+
len(EnumA)

python-checks/src/test/resources/checks/caughtExceptions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,12 @@ def except_star():
7777
...
7878
except* list(): # Noncompliant
7979
...
80+
81+
82+
def reassigned_exception():
83+
my_exception = None
84+
my_exception = ZeroDivisionError
85+
try:
86+
...
87+
except my_exception:
88+
...

python-checks/src/test/resources/checks/exceptionCauseType.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,12 @@ class SomeClass:
6767
foo()
6868
except ValueError:
6969
raise ValueError("Caught some value error") from SomeClass # FN SONARPY-1666
70+
71+
72+
def reassigned_exception():
73+
my_exception = None
74+
my_exception = ZeroDivisionError
75+
try:
76+
...
77+
except my_exception:
78+
raise my_exception("Caught some value error") from my_exception

python-checks/src/test/resources/checks/incompatibleOperands/arithmetic.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,13 @@ class SomeCtypeClass(ctypes.Structure):
118118
class RandomClass:
119119
...
120120
z = RandomClass * 42 # FN
121+
122+
123+
def type_symbols_try_except():
124+
import ctypes
125+
some_variable = None
126+
some_variable = ctypes.c_int32
127+
try:
128+
x = some_variable * 42 # OK
129+
except:
130+
x = some_variable * 42 # OK

python-checks/src/test/resources/checks/incorrectExceptionType/incorrectExceptionType.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,12 @@ def do_something():
153153
do_something()
154154
if exception:
155155
raise exception
156+
157+
158+
def reassigned_exception():
159+
my_exception = None
160+
my_exception = ValueError
161+
try:
162+
...
163+
except my_exception:
164+
raise my_exception

python-checks/src/test/resources/checks/iterationOnNonIterable.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,15 @@ def custom_mock():
276276
a, *rest = extended_mock
277277
iter(extended_mock)
278278
for elem in extended_mock: ... # OK
279+
280+
281+
def no_fp_on_enum_types():
282+
from enum import Enum
283+
284+
class EnumA(Enum):
285+
A = 1
286+
B = 2
287+
C = 3
288+
289+
for elem in EnumA:
290+
...

0 commit comments

Comments
 (0)