Skip to content

Commit

Permalink
SONARPY-2424 Document limitation on AliasDescriptor to PythonType con…
Browse files Browse the repository at this point in the history
…version (#2226)
  • Loading branch information
guillaume-dequenne-sonarsource authored Dec 9, 2024
1 parent bf7232a commit 514ded9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public class AliasDescriptorToPythonTypeConverter implements DescriptorToPythonTypeConverter {
@Override
public PythonType convert(ConversionContext ctx, Descriptor from) {
// We should try to retrieve the original type if possible, instead of recreating it
// SONARPY-2423: We should try to retrieve the original type if possible, instead of recreating it
return ctx.convert(((AliasDescriptor) from).originalDescriptor());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,33 @@ class B: pass
assertThat(localTypeChecker.typeCheckBuilder().isTypeOrInstanceWithName("my_package.unknown.A").check(aType)).isEqualTo(TriBool.UNKNOWN);
}

@Test
void typesFromAliasDescriptor() {
ProjectLevelSymbolTable projectLevelSymbolTable = ProjectLevelSymbolTable.empty();
ProjectLevelTypeTable projectLevelTypeTable = new ProjectLevelTypeTable(projectLevelSymbolTable);
PythonFile pythonFile = PythonTestUtils.pythonFile("mod.py");
TypeChecker localTypeChecker = new TypeChecker(projectLevelTypeTable);
var fileInput = parseAndInferTypes(projectLevelTypeTable, pythonFile, """
from fastapi.responses import Response as A
from starlette.responses import Response as B
A
B
"""
);
var aType = ((ExpressionStatement) fileInput.statements().statements().get(2)).expressions().get(0).typeV2();
var bType = ((ExpressionStatement) fileInput.statements().statements().get(3)).expressions().get(0).typeV2();

// SONARPY-2423, the types for A and B should be identical, since fastapi.responses.Response is an alias to starlette.responses.Response
assertThat(localTypeChecker.typeCheckBuilder().isTypeOrInstanceWithName("fastapi.responses.Response").check(aType)).isEqualTo(TriBool.TRUE);
assertThat(localTypeChecker.typeCheckBuilder().isTypeOrInstanceWithName("fastapi.responses.Response").check(bType)).isEqualTo(TriBool.FALSE);

assertThat(localTypeChecker.typeCheckBuilder().isTypeOrInstanceWithName("starlette.responses.Response").check(aType)).isEqualTo(TriBool.FALSE);
assertThat(localTypeChecker.typeCheckBuilder().isTypeOrInstanceWithName("starlette.responses.Response").check(bType)).isEqualTo(TriBool.TRUE);

assertThat(aType).isNotEqualTo(bType);
}



@Test
void testIsInstance() {
Expand Down

0 comments on commit 514ded9

Please sign in to comment.