Skip to content

Commit 457a6bc

Browse files
SONARPY-2183 Fix FP on S5708 when the caught object has type 'type' (#2039)
1 parent db0ac67 commit 457a6bc

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ private static boolean canBeOrExtendBaseException(Expression expression, Subscri
108108
PythonType pythonType = expression.typeV2();
109109
TriBool isBaseException = ctx.typeChecker().typeCheckBuilder().isInstanceOf("BaseException").check(pythonType);
110110
TriBool isTuple = ctx.typeChecker().typeCheckBuilder().isBuiltinWithName("tuple").check(pythonType);
111-
return isBaseException != TriBool.FALSE || isTuple != TriBool.FALSE;
111+
TriBool isType = ctx.typeChecker().typeCheckBuilder().isBuiltinWithName("type").check(pythonType);
112+
return isBaseException != TriBool.FALSE || isTuple != TriBool.FALSE || isType == TriBool.TRUE;
112113
}
113114

114115
private static boolean inheritsFromBaseException(@Nullable Symbol symbol) {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,10 @@ def reassigned_exception():
8686
...
8787
except my_exception:
8888
...
89+
90+
91+
def except_type():
92+
try:
93+
raise smth
94+
except type(smth) as e:
95+
...

0 commit comments

Comments
 (0)