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-2194 Prevent the creation of ObjectType[UnknownType] when converting VariableDescriptor #2095

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
SONARPY-21194 Prevent the creation of ObjectType[UnknownType] when co…
…nverting VariableDescriptor
guillaume-dequenne committed Oct 23, 2024
commit 29e98b3f7b74e67822975ff9ab1ec2fd3f13b355
Original file line number Diff line number Diff line change
@@ -24,15 +24,14 @@
import org.sonar.python.index.VariableDescriptor;
import org.sonar.python.types.v2.ObjectType;
import org.sonar.python.types.v2.PythonType;
import org.sonar.python.types.v2.TypeWrapper;

public class VariableDescriptorToPythonTypeConverter implements DescriptorToPythonTypeConverter {

public PythonType convert(ConversionContext ctx, VariableDescriptor from) {
var typeWrapper = Optional.ofNullable(from.annotatedType())
return Optional.ofNullable(from.annotatedType())
.map(fqn -> ctx.lazyTypesContext().getOrCreateLazyTypeWrapper(fqn))
.orElse(TypeWrapper.UNKNOWN_TYPE_WRAPPER);
return new ObjectType(typeWrapper);
.map(t -> (PythonType) new ObjectType(t))

Choose a reason for hiding this comment

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

minor: this chain lambdas could be replaced with method references:

return Optional.ofNullable(from.annotatedType())
      .map(ctx.lazyTypesContext()::getOrCreateLazyTypeWrapper)
      .map(ObjectType::new)
      .map(PythonType.class::cast)
      .orElse(PythonType.UNKNOWN);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I hesitated between the 2 approaches and somehow preferred the one in the code (neither of them really please me though tbh...), so I'll keep it that way.

.orElse(PythonType.UNKNOWN);
}

@Override
Original file line number Diff line number Diff line change
@@ -66,7 +66,7 @@ void imported_unknown2() {
var etreeType = ((ModuleType) ((ExpressionStatement) fileInput.statements().statements().get(1)).expressions().get(0).typeV2());
assertThat(etreeType.name()).isEqualTo("xml");
assertThat(etreeType.resolveSubmodule("etree")).isEmpty();
assertThat(etreeType.resolveMember("etree").get().unwrappedType()).isInstanceOf(UnknownType.UnknownTypeImpl.class);
assertThat(etreeType.resolveMember("etree").get()).isInstanceOf(UnknownType.UnknownTypeImpl.class);
}

@Test