Skip to content

Commit

Permalink
SONARPY-21194 Prevent the creation of ObjectType[UnknownType] when co…
Browse files Browse the repository at this point in the history
…nverting VariableDescriptor
  • Loading branch information
guillaume-dequenne-sonarsource committed Oct 23, 2024
1 parent 1e93b5c commit 29e98b3
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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))
.orElse(PythonType.UNKNOWN);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 29e98b3

Please sign in to comment.