Search in sources :

Example 16 with ClassSymbol

use of org.sonar.plugins.python.api.symbols.ClassSymbol in project sonar-python by SonarSource.

the class TypeShedTest method classes.

@Test
public void classes() {
    ClassSymbol intClass = TypeShed.typeShedClass("int");
    assertThat(intClass.superClasses()).extracting(Symbol::name).containsExactly("object");
    assertThat(intClass.hasUnresolvedTypeHierarchy()).isFalse();
    assertThat(intClass.usages()).isEmpty();
    assertThat(intClass.declaredMembers()).allMatch(member -> member.usages().isEmpty());
    assertThat(TypeShed.typeShedClass("bool").superClasses()).extracting(Symbol::fullyQualifiedName).containsExactly("int");
}
Also used : ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol) Test(org.junit.Test)

Example 17 with ClassSymbol

use of org.sonar.plugins.python.api.symbols.ClassSymbol in project sonar-python by SonarSource.

the class TypeShedTest method return_type_hints.

@Test
public void return_type_hints() {
    Map<String, Symbol> symbols = symbolsForModule("typing");
    assertThat(((FunctionSymbolImpl) symbols.get("get_args")).annotatedReturnTypeName()).isEqualTo("tuple");
    symbols = symbolsForModule("flask_mail");
    ClassSymbol mail = (ClassSymbol) symbols.get("Mail");
    assertThat(((FunctionSymbol) mail.declaredMembers().stream().iterator().next()).annotatedReturnTypeName()).isNull();
}
Also used : FunctionSymbol(org.sonar.plugins.python.api.symbols.FunctionSymbol) AmbiguousSymbol(org.sonar.plugins.python.api.symbols.AmbiguousSymbol) ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol) FunctionSymbol(org.sonar.plugins.python.api.symbols.FunctionSymbol) Symbol(org.sonar.plugins.python.api.symbols.Symbol) ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol) FunctionSymbolImpl(org.sonar.python.semantic.FunctionSymbolImpl) Test(org.junit.Test)

Example 18 with ClassSymbol

use of org.sonar.plugins.python.api.symbols.ClassSymbol in project sonar-python by SonarSource.

the class TypeShedTest method deserialize_annoy_protobuf.

@Test
public void deserialize_annoy_protobuf() {
    Map<String, Symbol> deserializedAnnoySymbols = symbolsForModule("annoy").values().stream().collect(Collectors.toMap(Symbol::fullyQualifiedName, s -> s));
    assertThat(deserializedAnnoySymbols.values()).extracting(Symbol::kind, Symbol::fullyQualifiedName).containsExactlyInAnyOrder(tuple(Kind.CLASS, "annoy._Vector"), tuple(Kind.CLASS, "annoy.AnnoyIndex"));
    ClassSymbol vector = (ClassSymbol) deserializedAnnoySymbols.get("annoy._Vector");
    assertThat(vector.superClasses()).extracting(Symbol::kind, Symbol::fullyQualifiedName).containsExactlyInAnyOrder(tuple(Kind.CLASS, "typing.Sized"));
    assertThat(vector.declaredMembers()).extracting(Symbol::name).containsExactlyInAnyOrder("__getitem__");
    assertThat(vector.hasDecorators()).isFalse();
    assertThat(vector.definitionLocation()).isNull();
    ClassSymbol annoyIndex = (ClassSymbol) deserializedAnnoySymbols.get("annoy.AnnoyIndex");
    assertThat(annoyIndex.superClasses()).extracting(Symbol::kind, Symbol::fullyQualifiedName).containsExactlyInAnyOrder(tuple(Kind.CLASS, "object"));
    assertThat(annoyIndex.declaredMembers()).extracting(Symbol::kind, Symbol::name).containsExactlyInAnyOrder(tuple(Kind.FUNCTION, "__init__"), tuple(Kind.FUNCTION, "load"), tuple(Kind.FUNCTION, "save"), tuple(Kind.AMBIGUOUS, "get_nns_by_item"), tuple(Kind.AMBIGUOUS, "get_nns_by_vector"), tuple(Kind.FUNCTION, "get_item_vector"), tuple(Kind.FUNCTION, "add_item"), tuple(Kind.FUNCTION, "on_disk_build"), tuple(Kind.FUNCTION, "build"), tuple(Kind.FUNCTION, "unbuild"), tuple(Kind.FUNCTION, "unload"), tuple(Kind.FUNCTION, "get_distance"), tuple(Kind.FUNCTION, "get_n_items"), tuple(Kind.FUNCTION, "get_n_trees"), tuple(Kind.FUNCTION, "verbose"), tuple(Kind.FUNCTION, "set_seed"), tuple(Kind.OTHER, "f"));
    assertThat(annoyIndex.hasDecorators()).isFalse();
    assertThat(annoyIndex.definitionLocation()).isNull();
}
Also used : TypeShed.symbolsForModule(org.sonar.python.types.TypeShed.symbolsForModule) SymbolsProtos(org.sonar.python.types.protobuf.SymbolsProtos) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SymbolImpl(org.sonar.python.semantic.SymbolImpl) HashMap(java.util.HashMap) ProjectPythonVersion(org.sonar.plugins.python.api.ProjectPythonVersion) AmbiguousSymbol(org.sonar.plugins.python.api.symbols.AmbiguousSymbol) ByteArrayInputStream(java.io.ByteArrayInputStream) PythonVersionUtils(org.sonar.plugins.python.api.PythonVersionUtils) Map(java.util.Map) TextFormat(com.google.protobuf.TextFormat) Before(org.junit.Before) Assertions.tuple(org.assertj.core.api.Assertions.tuple) Collection(java.util.Collection) Set(java.util.Set) Kind(org.sonar.plugins.python.api.symbols.Symbol.Kind) Test(org.junit.Test) ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol) Collectors(java.util.stream.Collectors) FunctionSymbolImpl(org.sonar.python.semantic.FunctionSymbolImpl) LogTester(org.sonar.api.utils.log.LogTester) AmbiguousSymbolImpl(org.sonar.python.semantic.AmbiguousSymbolImpl) ClassSymbolImpl(org.sonar.python.semantic.ClassSymbolImpl) Collections(java.util.Collections) FunctionSymbol(org.sonar.plugins.python.api.symbols.FunctionSymbol) Symbol(org.sonar.plugins.python.api.symbols.Symbol) InputStream(java.io.InputStream) LoggerLevel(org.sonar.api.utils.log.LoggerLevel) AmbiguousSymbol(org.sonar.plugins.python.api.symbols.AmbiguousSymbol) ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol) FunctionSymbol(org.sonar.plugins.python.api.symbols.FunctionSymbol) Symbol(org.sonar.plugins.python.api.symbols.Symbol) ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol) Test(org.junit.Test)

Example 19 with ClassSymbol

use of org.sonar.plugins.python.api.symbols.ClassSymbol in project sonar-python by SonarSource.

the class TypeShedTest method typeshed_private_modules_should_not_affect_fqn.

@Test
public void typeshed_private_modules_should_not_affect_fqn() {
    Map<String, Symbol> socketModule = symbolsForModule("socket");
    ClassSymbol socket = (ClassSymbol) TypeShed.disambiguateWithLatestPythonSymbol(((AmbiguousSymbol) socketModule.get("socket")).alternatives());
    assertThat(socket.declaredMembers()).extracting(Symbol::name, Symbol::fullyQualifiedName).contains(tuple("connect", "socket.socket.connect"));
    assertThat(socket.superClasses()).extracting(Symbol::fullyQualifiedName).containsExactly("object");
}
Also used : AmbiguousSymbol(org.sonar.plugins.python.api.symbols.AmbiguousSymbol) ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol) FunctionSymbol(org.sonar.plugins.python.api.symbols.FunctionSymbol) Symbol(org.sonar.plugins.python.api.symbols.Symbol) ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol) AmbiguousSymbol(org.sonar.plugins.python.api.symbols.AmbiguousSymbol) Test(org.junit.Test)

Example 20 with ClassSymbol

use of org.sonar.plugins.python.api.symbols.ClassSymbol in project sonar-python by SonarSource.

the class TypeShedTest method class_symbols_from_protobuf.

@Test
public void class_symbols_from_protobuf() throws TextFormat.ParseException {
    SymbolsProtos.ModuleSymbol moduleSymbol = moduleSymbol("fully_qualified_name: \"mod\"\n" + "classes {\n" + "  name: \"Base\"\n" + "  fully_qualified_name: \"mod.Base\"\n" + "  super_classes: \"builtins.object\"\n" + "}\n" + "classes {\n" + "  name: \"C\"\n" + "  fully_qualified_name: \"mod.C\"\n" + "  super_classes: \"builtins.str\"\n" + "}\n" + "classes {\n" + "  name: \"D\"\n" + "  fully_qualified_name: \"mod.D\"\n" + "  super_classes: \"NOT_EXISTENT\"\n" + "}");
    Map<String, Symbol> symbols = TypeShed.getSymbolsFromProtobufModule(moduleSymbol);
    assertThat(symbols.values()).extracting(Symbol::kind, Symbol::fullyQualifiedName).containsExactlyInAnyOrder(tuple(Kind.CLASS, "mod.Base"), tuple(Kind.CLASS, "mod.C"), tuple(Kind.CLASS, "mod.D"));
    ClassSymbol C = (ClassSymbol) symbols.get("C");
    assertThat(C.superClasses()).extracting(Symbol::fullyQualifiedName).containsExactly("str");
    ClassSymbol D = (ClassSymbol) symbols.get("D");
    assertThat(D.superClasses()).extracting(Symbol::kind, Symbol::fullyQualifiedName).containsExactly(tuple(Kind.OTHER, "NOT_EXISTENT"));
}
Also used : SymbolsProtos(org.sonar.python.types.protobuf.SymbolsProtos) AmbiguousSymbol(org.sonar.plugins.python.api.symbols.AmbiguousSymbol) ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol) FunctionSymbol(org.sonar.plugins.python.api.symbols.FunctionSymbol) Symbol(org.sonar.plugins.python.api.symbols.Symbol) ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol) Test(org.junit.Test)

Aggregations

ClassSymbol (org.sonar.plugins.python.api.symbols.ClassSymbol)87 Test (org.junit.Test)64 Symbol (org.sonar.plugins.python.api.symbols.Symbol)55 AmbiguousSymbol (org.sonar.plugins.python.api.symbols.AmbiguousSymbol)48 FileInput (org.sonar.plugins.python.api.tree.FileInput)36 FunctionSymbol (org.sonar.plugins.python.api.symbols.FunctionSymbol)29 ClassDef (org.sonar.plugins.python.api.tree.ClassDef)29 ClassSymbolImpl (org.sonar.python.semantic.ClassSymbolImpl)13 HashMap (java.util.HashMap)11 HashSet (java.util.HashSet)11 Set (java.util.Set)11 List (java.util.List)10 Map (java.util.Map)10 Collectors (java.util.stream.Collectors)10 SymbolImpl (org.sonar.python.semantic.SymbolImpl)9 Collections (java.util.Collections)7 HasSymbol (org.sonar.plugins.python.api.tree.HasSymbol)7 InferredType (org.sonar.plugins.python.api.types.InferredType)7 Arrays (java.util.Arrays)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6