Skip to content

Commit

Permalink
SonarQube fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Seppli11 committed Nov 27, 2024
1 parent 566023b commit ebba41c
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,14 @@ void removeUsages() {

@Test
void from_protobuf() throws TextFormat.ParseException {
String protobuf =
"name: \"A\"\n" +
"fully_qualified_name: \"mod.A\"\n" +
"super_classes: \"builtins.object\"\n" +
"has_decorators: true\n" +
"has_metaclass: true\n" +
"metaclass_name: \"abc.ABCMeta\"";
String protobuf = """
name: "A"
fully_qualified_name: "mod.A"
super_classes: "builtins.object"
has_decorators: true
has_metaclass: true
metaclass_name: "abc.ABCMeta"
""";
ClassSymbolImpl classSymbol = new ClassSymbolImpl(classSymbol(protobuf), "mod");
assertThat(classSymbol.name()).isEqualTo("A");
assertThat(classSymbol.fullyQualifiedName()).isEqualTo("mod.A");
Expand All @@ -251,91 +252,96 @@ void from_protobuf() throws TextFormat.ParseException {

@Test
void from_protobuf_instance_method() throws TextFormat.ParseException {
String protobuf =
"name: \"A\"\n" +
"fully_qualified_name: \"mod.A\"\n" +
"super_classes: \"builtins.object\"\n" +
"methods {\n" +
" name: \"foo\"\n" +
" fully_qualified_name: \"mod.A.foo\"\n" +
" parameters {\n" +
" name: \"self\"\n" +
" kind: POSITIONAL_OR_KEYWORD\n" +
" }\n" +
" has_decorators: true\n" +
"}";
String protobuf = """
name: "A"
fully_qualified_name: "mod.A"
super_classes: "builtins.object"
methods {
name: "foo"
fully_qualified_name: "mod.A.foo"
parameters {
name: "self"
kind: POSITIONAL_OR_KEYWORD
}
has_decorators: true
}
""";
ClassSymbolImpl classSymbol = new ClassSymbolImpl(classSymbol(protobuf), "mod");
FunctionSymbol foo = (FunctionSymbol) classSymbol.declaredMembers().iterator().next();
assertThat(foo.isInstanceMethod()).isTrue();
}

@Test
void from_protobuf_class_method() throws TextFormat.ParseException {
String protobuf =
"name: \"A\"\n" +
"fully_qualified_name: \"mod.A\"\n" +
"super_classes: \"builtins.object\"\n" +
"methods {\n" +
" name: \"foo\"\n" +
" fully_qualified_name: \"mod.A.foo\"\n" +
" parameters {\n" +
" name: \"cls\"\n" +
" kind: POSITIONAL_OR_KEYWORD\n" +
" }\n" +
" has_decorators: true\n" +
" is_class_method: true\n" +
"}";
String protobuf = """
name: "A"
fully_qualified_name: "mod.A"
super_classes: "builtins.object"
methods {
name: "foo"
fully_qualified_name: "mod.A.foo"
parameters {
name: "cls"
kind: POSITIONAL_OR_KEYWORD
}
has_decorators: true
is_class_method: true
}
""";
ClassSymbolImpl classSymbol = new ClassSymbolImpl(classSymbol(protobuf), "mod");
FunctionSymbol foo = (FunctionSymbol) classSymbol.declaredMembers().iterator().next();
assertThat(foo.isInstanceMethod()).isFalse();
}

@Test
void from_protobuf_static_method() throws TextFormat.ParseException {
String protobuf =
"name: \"A\"\n" +
"fully_qualified_name: \"mod.A\"\n" +
"super_classes: \"builtins.object\"\n" +
"methods {\n" +
" name: \"foo\"\n" +
" fully_qualified_name: \"mod.A.foo\"\n" +
" parameters {\n" +
" name: \"x\"\n" +
" kind: POSITIONAL_OR_KEYWORD\n" +
" }\n" +
" has_decorators: true\n" +
" is_static: true\n" +
"}";
String protobuf = """
name: "A"
fully_qualified_name: "mod.A"
super_classes: "builtins.object"
methods {
name: "foo"
fully_qualified_name: "mod.A.foo"
parameters {
name: "x"
kind: POSITIONAL_OR_KEYWORD
}
has_decorators: true
is_static: true
}
""";
ClassSymbolImpl classSymbol = new ClassSymbolImpl(classSymbol(protobuf), "mod");
FunctionSymbol foo = (FunctionSymbol) classSymbol.declaredMembers().iterator().next();
assertThat(foo.isInstanceMethod()).isFalse();
}

@Test
void overloaded_methods() throws TextFormat.ParseException {
String protobuf =
"name: \"A\"\n" +
"fully_qualified_name: \"mod.A\"\n" +
"super_classes: \"builtins.object\"\n" +
"methods {\n" +
" name: \"foo\"\n" +
" fully_qualified_name: \"mod.A.foo\"\n" +
" valid_for: \"39\"\n" +
"}\n" +
"overloaded_methods {\n" +
" name: \"foo\"\n" +
" fullname: \"mod.A.foo\"\n" +
" valid_for: \"310\"\n" +
" definitions {\n" +
" name: \"foo\"\n" +
" fully_qualified_name: \"mod.A.foo\"\n" +
" has_decorators: true\n" +
" }\n" +
" definitions {\n" +
" name: \"foo\"\n" +
" fully_qualified_name: \"mod.A.foo\"\n" +
" }\n" +
"}\n";
String protobuf = """
name: "A"
fully_qualified_name: "mod.A"
super_classes: "builtins.object"
methods {
name: "foo"
fully_qualified_name: "mod.A.foo"
valid_for: "39"
}
overloaded_methods {
name: "foo"
fullname: "mod.A.foo"
valid_for: "310"
definitions {
name: "foo"
fully_qualified_name: "mod.A.foo"
has_decorators: true
}
definitions {
name: "foo"
fully_qualified_name: "mod.A.foo"
}
}
""";
ClassSymbolImpl classSymbol = new ClassSymbolImpl(classSymbol(protobuf), "mod");
Symbol foo = classSymbol.resolveMember("foo").get();
assertThat(foo.is(Symbol.Kind.AMBIGUOUS)).isTrue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ void importing_stdlib() {
FunctionDef functionDef = functionTreesByName.get("importing_stdlib");
Map<String, Symbol> symbolByName = getSymbolByName(functionDef);

assertThat(symbolByName.keySet()).containsOnly("os");
assertThat(symbolByName).containsOnlyKeys("os");
assertThat(symbolByName.get("os").usages()).extracting(Usage::kind).containsExactly(Usage.Kind.IMPORT, Usage.Kind.OTHER);

CallExpression callExpression = (CallExpression) ((ExpressionStatement) functionDef.body().statements().get(1)).expressions().get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2650,7 +2650,7 @@ void imported_ambiguous_symbol() {
realpath
""");
UnionType realpathType = (UnionType) ((ExpressionStatement) fileInput.statements().statements().get(1)).expressions().get(0).typeV2();
assertThat(realpathType.candidates()).allMatch(p -> p instanceof FunctionType);
assertThat(realpathType.candidates()).allMatch(FunctionType.class::isInstance);
assertThat(realpathType.candidates()).extracting(PythonType::name).containsExactly("realpath", "realpath", "realpath", "realpath");
assertThat(realpathType.candidates())
.map(FunctionType.class::cast)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* SonarQube Python Plugin
* Copyright (C) 2011-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
package org.sonar.python.types;

import java.util.List;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

class TypeShedInitializationTest {
@Test
void is_valid_for_project_python_version_should_throw_when_not_initialized() {
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> TypeShed.isValidForProjectPythonVersion(List.of("3.9")));
}
}

0 comments on commit ebba41c

Please sign in to comment.