|
31 | 31 | import org.junit.jupiter.api.Disabled;
|
32 | 32 | import org.junit.jupiter.api.Test;
|
33 | 33 | import org.mockito.Mockito;
|
| 34 | +import org.sonar.plugins.python.api.LocationInFile; |
34 | 35 | import org.sonar.plugins.python.api.PythonFile;
|
35 | 36 | import org.sonar.plugins.python.api.symbols.ClassSymbol;
|
36 | 37 | import org.sonar.plugins.python.api.symbols.Symbol;
|
|
54 | 55 | import org.sonar.python.PythonTestUtils;
|
55 | 56 | import org.sonar.python.semantic.ClassSymbolImpl;
|
56 | 57 | import org.sonar.python.semantic.ProjectLevelSymbolTable;
|
| 58 | +import org.sonar.python.semantic.SymbolUtils; |
57 | 59 | import org.sonar.python.tree.ExpressionStatementImpl;
|
58 | 60 | import org.sonar.python.tree.TreeUtils;
|
59 | 61 | import org.sonar.python.types.v2.ClassType;
|
@@ -694,6 +696,38 @@ def foo(param: my_alias): ...
|
694 | 696 | assertThat(((FunctionType) functionDef.name().typeV2()).parameters().get(0).declaredType().type().unwrappedType()).isEqualTo(PythonType.UNKNOWN);
|
695 | 697 | }
|
696 | 698 |
|
| 699 | + @Test |
| 700 | + void inferFunctionParameterTypesMultiFile() { |
| 701 | + FileInput tree = parseWithoutSymbols( |
| 702 | + "def foo(param1: int): ...", |
| 703 | + "class A: ...", |
| 704 | + "def foo2(p1: dict, p2: A): ..." |
| 705 | + ); |
| 706 | + ProjectLevelSymbolTable projectLevelSymbolTable = new ProjectLevelSymbolTable(); |
| 707 | + var modFile = pythonFile("mod.py"); |
| 708 | + projectLevelSymbolTable.addModule(tree, "", modFile); |
| 709 | + ProjectLevelTypeTable projectLevelTypeTable = new ProjectLevelTypeTable(projectLevelSymbolTable); |
| 710 | + var modFileId = SymbolUtils.pathOf(modFile).toString(); |
| 711 | + |
| 712 | + var intType = projectLevelTypeTable.lazyTypesContext().getOrCreateLazyType("int").resolve(); |
| 713 | + var dictType = projectLevelTypeTable.lazyTypesContext().getOrCreateLazyType("dict").resolve(); |
| 714 | + var aType = projectLevelTypeTable.lazyTypesContext().getOrCreateLazyType("mod.A").resolve(); |
| 715 | + var lines = """ |
| 716 | + from mod import foo, foo2 |
| 717 | + foo |
| 718 | + foo2 |
| 719 | + """; |
| 720 | + FileInput fileInput = inferTypes(lines, projectLevelTypeTable); |
| 721 | + FunctionType fooType = (FunctionType) ((ExpressionStatement) fileInput.statements().statements().get(1)).expressions().get(0).typeV2(); |
| 722 | + assertThat(fooType.parameters().get(0).declaredType().type().unwrappedType()).isEqualTo(intType); |
| 723 | + |
| 724 | + FunctionType foo2Type = (FunctionType) ((ExpressionStatement) fileInput.statements().statements().get(2)).expressions().get(0).typeV2(); |
| 725 | + assertThat(foo2Type.parameters()).extracting(ParameterV2::declaredType).extracting(TypeWrapper::type).containsExactly(dictType, aType); |
| 726 | + assertThat(foo2Type.parameters()).extracting(ParameterV2::location).containsExactly( |
| 727 | + new LocationInFile(modFileId, 3, 9, 3, 17), |
| 728 | + new LocationInFile(modFileId, 3, 19, 3, 24)); |
| 729 | + } |
| 730 | + |
697 | 731 | @Test
|
698 | 732 | void inferFunctionReturnTypeType() {
|
699 | 733 | FileInput root = inferTypes("""
|
|
0 commit comments