From ebba41c2a18c2187022b5e82d898d5092cc71001 Mon Sep 17 00:00:00 2001 From: Sebastian Zumbrunn Date: Wed, 27 Nov 2024 17:55:16 +0100 Subject: [PATCH] SonarQube fixes --- .../python/semantic/ClassSymbolImplTest.java | 148 +++++++++--------- .../semantic/SymbolTableBuilderTest.java | 2 +- .../semantic/v2/TypeInferenceV2Test.java | 2 +- .../types/TypeShedInitializationTest.java | 29 ++++ 4 files changed, 108 insertions(+), 73 deletions(-) create mode 100644 python-frontend/src/test/java/org/sonar/python/types/TypeShedInitializationTest.java diff --git a/python-frontend/src/test/java/org/sonar/python/semantic/ClassSymbolImplTest.java b/python-frontend/src/test/java/org/sonar/python/semantic/ClassSymbolImplTest.java index 6f384e90c1..c0dbc40e0a 100644 --- a/python-frontend/src/test/java/org/sonar/python/semantic/ClassSymbolImplTest.java +++ b/python-frontend/src/test/java/org/sonar/python/semantic/ClassSymbolImplTest.java @@ -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"); @@ -251,19 +252,20 @@ 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(); @@ -271,20 +273,21 @@ void from_protobuf_instance_method() throws TextFormat.ParseException { @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(); @@ -292,20 +295,21 @@ void from_protobuf_class_method() throws TextFormat.ParseException { @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(); @@ -313,29 +317,31 @@ void from_protobuf_static_method() throws TextFormat.ParseException { @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(); diff --git a/python-frontend/src/test/java/org/sonar/python/semantic/SymbolTableBuilderTest.java b/python-frontend/src/test/java/org/sonar/python/semantic/SymbolTableBuilderTest.java index 799df71478..7563fbaf47 100644 --- a/python-frontend/src/test/java/org/sonar/python/semantic/SymbolTableBuilderTest.java +++ b/python-frontend/src/test/java/org/sonar/python/semantic/SymbolTableBuilderTest.java @@ -273,7 +273,7 @@ void importing_stdlib() { FunctionDef functionDef = functionTreesByName.get("importing_stdlib"); Map 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); diff --git a/python-frontend/src/test/java/org/sonar/python/semantic/v2/TypeInferenceV2Test.java b/python-frontend/src/test/java/org/sonar/python/semantic/v2/TypeInferenceV2Test.java index 06ce2f8e31..d652c45db4 100644 --- a/python-frontend/src/test/java/org/sonar/python/semantic/v2/TypeInferenceV2Test.java +++ b/python-frontend/src/test/java/org/sonar/python/semantic/v2/TypeInferenceV2Test.java @@ -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) diff --git a/python-frontend/src/test/java/org/sonar/python/types/TypeShedInitializationTest.java b/python-frontend/src/test/java/org/sonar/python/types/TypeShedInitializationTest.java new file mode 100644 index 0000000000..b937fe0b97 --- /dev/null +++ b/python-frontend/src/test/java/org/sonar/python/types/TypeShedInitializationTest.java @@ -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"))); + } +}