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");
}
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();
}
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();
}
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");
}
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"));
}
Aggregations