Skip to content

Commit

Permalink
SONARPY-2275 Fix FP on S5756 for calls on class variables defined as …
Browse files Browse the repository at this point in the history
…classmethods
  • Loading branch information
guillaume-dequenne-sonarsource authored and Seppli11 committed Oct 29, 2024
1 parent 7fb2249 commit 68137af
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 21 deletions.
4 changes: 3 additions & 1 deletion python-checks/src/test/resources/checks/nonCallableCalled.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,12 @@ def call_non_callable_property():

class MyClass:
x = 42
my_classmethod = classmethod(...)

def foo():
mc = MyClass()
mc.x() #Noncompliant
mc.x() # FN
mc.my_classmethod() # OK

def using_isinstance_with_runtime_type():
my_non_callable = MyNonCallable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,7 @@ private static Optional<NameImpl> getFirstAssignmentName(AssignmentStatement ass

private void addStaticFieldToClass(Name name) {
if (currentType() instanceof ClassType ownerClass) {
PythonType memberType = name.symbolV2().hasSingleBindingUsage() ? name.typeV2() : PythonType.UNKNOWN;
ownerClass.members().add(new Member(name.name(), memberType));
ownerClass.members().add(new Member(name.name(), PythonType.UNKNOWN));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,7 @@ class D(A, C): pass
D.B
""");
assertThat(exprWithMultiInheritance1.typeV2())
.isInstanceOf(ObjectType.class)
.extracting(PythonType::unwrappedType)
.isEqualTo(STR_TYPE);
.isInstanceOf(UnknownType.class);

Expression exprWithMultiInheritance2 = lastExpression("""
class A:
Expand All @@ -444,9 +442,7 @@ class A:
A.test
""");
assertThat(expr.typeV2())
.isInstanceOf(ObjectType.class)
.extracting(PythonType::unwrappedType)
.isEqualTo(STR_TYPE);
.isInstanceOf(UnknownType.class);

Expression expr2 = lastExpression("""
class A:
Expand All @@ -468,9 +464,7 @@ class B(A):
B.test
""");
assertThat(exprWithInheritance.typeV2())
.isInstanceOf(ObjectType.class)
.extracting(PythonType::unwrappedType)
.isEqualTo(STR_TYPE);
.isInstanceOf(UnknownType.class);

Expression exprWithInheritance2 = lastExpression("""
class A:
Expand All @@ -482,9 +476,7 @@ class C(B):
C.test
""");
assertThat(exprWithInheritance2.typeV2())
.isInstanceOf(ObjectType.class)
.extracting(PythonType::unwrappedType)
.isEqualTo(STR_TYPE);
.isInstanceOf(UnknownType.class);

Expression exprWithMultiInheritance = lastExpression("""
class A:
Expand All @@ -495,9 +487,7 @@ class C(A, B):
C.test
""");
assertThat(exprWithMultiInheritance.typeV2())
.isInstanceOf(ObjectType.class)
.extracting(PythonType::unwrappedType)
.isEqualTo(STR_TYPE);
.isInstanceOf(UnknownType.class);

Expression exprWithMultiInheritance2 = lastExpression("""
class A:
Expand All @@ -509,9 +499,7 @@ class C(A, B):
C.test
""");
assertThat(exprWithMultiInheritance2.typeV2())
.isInstanceOf(ObjectType.class)
.extracting(PythonType::unwrappedType)
.isEqualTo(STR_TYPE);
.isInstanceOf(UnknownType.class);
}

@Test
Expand Down

0 comments on commit 68137af

Please sign in to comment.