Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SONARPY-1801 infer type of subscription expression when it is a class with generic type #1776

Open
wants to merge 1 commit into
base: rnd/type-inference-engine-specification
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.sonar.plugins.python.api.tree.Token;
import org.sonar.plugins.python.api.tree.Tree;
import org.sonar.plugins.python.api.tree.TreeVisitor;
import org.sonar.python.types.v2.ClassType;
import org.sonar.python.types.v2.PythonType;

public class SubscriptionExpressionImpl extends PyTree implements SubscriptionExpression {

Expand Down Expand Up @@ -77,4 +79,13 @@ public List<Tree> computeChildren() {
public Kind getKind() {
return Kind.SUBSCRIPTION;
}

@Override
public PythonType typeV2() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instinctively, I believe typeV2() should ideally only be a getter for types.
I believe the logic of this method would make more sense in TypeInferenceV2 itself.

I see in the previous implementation this was done in CallExpressionImpl directly, using a similar logic, though. It did have some checks to ensure we were indeed dealing with a generic type, I think it would make sense to have it as well to avoid edge cases (I guess you could create a class where the class object itself has a __getitem__ method...

var objectType = object().typeV2();
if (objectType instanceof ClassType classType) {
return classType;
}
return PythonType.UNKNOWN;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.sonar.python.types.v2.ClassType;
import org.sonar.python.types.v2.FunctionType;
import org.sonar.python.types.v2.ModuleType;
import org.sonar.python.types.v2.ObjectType;
import org.sonar.python.types.v2.PythonType;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -264,6 +265,35 @@ def foo():
Assertions.assertThat(expressionStatement.expressions().get(0).typeV2()).isEqualTo(PythonType.UNKNOWN);
}

@Test
void inferForSubscriptionExpressionFromClassType() {
FileInput root = inferTypes("""
def foo():
a = list[int](1, 2, 3)
""");

var functionDef = (FunctionDef) root.statements().statements().get(0);
var assignmentStatement = (AssignmentStatement) functionDef.body().statements().get(0);
var type = (ObjectType) assignmentStatement.assignedValue().typeV2();
assertThat(type.type()).isInstanceOf(ClassType.class)
.extracting(PythonType::name)
.isEqualTo("list");
}

@Test
void inferTypeForSubscriptionExpressionFromObjectType() {
FileInput root = inferTypes("""
def foo():
my_list = ["hello"]
a = my_list[0]
""");

var functionDef = (FunctionDef) root.statements().statements().get(0);
var assignmentStatement = (AssignmentStatement) functionDef.body().statements().get(1);
var type = assignmentStatement.assignedValue().typeV2();
assertThat(type).isEqualTo(PythonType.UNKNOWN);
}

private FileInput inferTypes(String lines) {
return inferTypes(lines, new HashMap<>());
}
Expand Down