Skip to content

Commit d52bf69

Browse files
Seppli11sonartech
authored andcommitted
SONARPY-3583 Set classmethod cls parameter to SelfType (#728)
GitOrigin-RevId: c66c9c45948a5b824e4b67b4883a4b2c8bf939c8
1 parent 5ae3765 commit d52bf69

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def foo(): pass
102102
@classmethod
103103
def bar_class_method(cls):
104104
cls.bar_instance_method(cls)
105-
cls.bar_instance_method(cls, 1) # FN
105+
cls.bar_instance_method(cls, 1) # Noncompliant
106106
def bar_instance_method(self): pass
107107

108108
A.class_meth(42) # FN {{'class_meth' expects 2 positional arguments, but 1 was provided}}

python-frontend/src/main/java/org/sonar/python/semantic/v2/types/TrivialTypeInferenceVisitor.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -460,10 +460,6 @@ private void setSelfParameterType(FunctionDef functionDef) {
460460
return;
461461
}
462462

463-
if (!functionType.isInstanceMethod()) {
464-
return;
465-
}
466-
467463
var parameterList = functionDef.parameters();
468464
if (parameterList == null) {
469465
return;
@@ -476,10 +472,15 @@ private void setSelfParameterType(FunctionDef functionDef) {
476472

477473
var firstParam = parameters.get(0);
478474
var paramName = firstParam.name();
479-
// Set the type to ObjectType[SelfType[ClassType]]
480-
if (paramName != null) {
481-
var classObjectType = ObjectType.fromType(classType);
482-
var selfType = SelfType.of(classObjectType);
475+
if (paramName == null) {
476+
return;
477+
}
478+
479+
var selfType = SelfType.of(classType);
480+
if (functionType.isInstanceMethod()) {
481+
var selfObjectType = ObjectType.fromType(selfType);
482+
setTypeToName(paramName, selfObjectType);
483+
} else if (functionType.isClassMethod()) {
483484
setTypeToName(paramName, selfType);
484485
}
485486
}

python-frontend/src/test/java/org/sonar/python/semantic/v2/TypeInferenceV2Test.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4204,10 +4204,9 @@ def foo(cls):
42044204
var clsExpressionStatement = (ExpressionStatement) fooMethodDef.body().statements().get(0);
42054205
var clsExpression = clsExpressionStatement.expressions().get(0);
42064206

4207-
assertThat(clsExpression.typeV2()).isNotInstanceOf(SelfType.class);
42084207
assertThat(clsExpression.typeV2())
4209-
.extracting(PythonType::unwrappedType)
4210-
.isNotInstanceOf(SelfType.class);
4208+
.isInstanceOfSatisfying(SelfType.class, selfType -> assertThat(selfType.innerType())
4209+
.isEqualTo(classDef.name().typeV2()));
42114210
}
42124211

42134212
@Test

0 commit comments

Comments
 (0)