diff --git a/python-frontend/src/main/java/org/sonar/python/types/RuntimeType.java b/python-frontend/src/main/java/org/sonar/python/types/RuntimeType.java index cb9a910aa9..6563d218ec 100644 --- a/python-frontend/src/main/java/org/sonar/python/types/RuntimeType.java +++ b/python-frontend/src/main/java/org/sonar/python/types/RuntimeType.java @@ -29,10 +29,9 @@ import org.sonar.plugins.python.api.types.InferredType; import org.sonar.python.semantic.ClassSymbolImpl; -public class RuntimeType implements InferredType { +import static org.sonar.python.types.InferredTypes.TYPE; - // Name of the type returned by the type() function - private static final String TYPE_CLASS_NAME = "type"; +public class RuntimeType implements InferredType { private ClassSymbol typeClass; private String builtinFullyQualifiedName; @@ -55,18 +54,15 @@ public boolean isIdentityComparableWith(InferredType other) { if (other instanceof UnionType) { return other.isIdentityComparableWith(this); } - if (isComparingTypeWithMetaclass(other)) { - return true; - } - return this.equals(other); + return isComparingTypeWithMetaclass(other) || this.equals(other); } private boolean isComparingTypeWithMetaclass(InferredType other) { if (other instanceof RuntimeType otherRuntimeType) { boolean hasOtherMetaClass = hasMetaclassInHierarchy(otherRuntimeType); boolean hasThisMetaClass = hasMetaclassInHierarchy(this); - return (TYPE_CLASS_NAME.equals(getTypeClass().name()) && hasOtherMetaClass) - || (hasThisMetaClass && TYPE_CLASS_NAME.equals(otherRuntimeType.getTypeClass().name())); + return (TYPE.getClass().getName().equals(getTypeClass().name()) && hasOtherMetaClass) + || (hasThisMetaClass && TYPE.getClass().getName().equals(otherRuntimeType.getTypeClass().name())); } return false; } @@ -81,7 +77,7 @@ public boolean canHaveMember(String memberName) { if (MOCK_FQNS.stream().anyMatch(this::mustBeOrExtend)){ return true; } - if (this.equals(InferredTypes.TYPE)) { + if (this.equals(TYPE)) { // SONARPY-1666: need to know the actual type to know its members return true; } diff --git a/python-frontend/src/test/java/org/sonar/python/types/RuntimeTypeTest.java b/python-frontend/src/test/java/org/sonar/python/types/RuntimeTypeTest.java index 1d352512a7..0502e9b023 100644 --- a/python-frontend/src/test/java/org/sonar/python/types/RuntimeTypeTest.java +++ b/python-frontend/src/test/java/org/sonar/python/types/RuntimeTypeTest.java @@ -36,6 +36,7 @@ import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.python.types.InferredTypes.TYPE; import static org.sonar.python.types.InferredTypes.or; import static org.sonar.python.types.InferredTypes.runtimeBuiltinType; @@ -66,8 +67,6 @@ void isIdentityComparableWith() { @Test void isIdentityComparableWithMetaclass() { - RuntimeType typeType = new RuntimeType(new ClassSymbolImpl("type", "type")); - ClassSymbolImpl metaclassSymbol = new ClassSymbolImpl("Meta", "Meta"); metaclassSymbol.setHasMetaClass(); RuntimeType metaClassType = new RuntimeType(metaclassSymbol); @@ -78,16 +77,16 @@ void isIdentityComparableWithMetaclass() { UnknownClassType unknownClassType = new UnknownClassType(metaclassSymbol); - assertThat(typeType.isIdentityComparableWith(typeType)).isTrue(); - assertThat(typeType.isIdentityComparableWith(metaClassType)).isTrue(); - assertThat(typeType.isIdentityComparableWith(superMetaClassType)).isTrue(); - assertThat(typeType.isIdentityComparableWith(unknownClassType)).isFalse(); + assertThat(TYPE.isIdentityComparableWith(TYPE)).isTrue(); + assertThat(TYPE.isIdentityComparableWith(metaClassType)).isTrue(); + assertThat(TYPE.isIdentityComparableWith(superMetaClassType)).isTrue(); + assertThat(TYPE.isIdentityComparableWith(unknownClassType)).isFalse(); - assertThat(metaClassType.isIdentityComparableWith(typeType)).isTrue(); + assertThat(metaClassType.isIdentityComparableWith(TYPE)).isTrue(); assertThat(metaClassType.isIdentityComparableWith(metaClassType)).isTrue(); assertThat(metaClassType.isIdentityComparableWith(superMetaClassType)).isFalse(); - assertThat(superMetaClassType.isIdentityComparableWith(typeType)).isTrue(); + assertThat(superMetaClassType.isIdentityComparableWith(TYPE)).isTrue(); assertThat(superMetaClassType.isIdentityComparableWith(metaClassType)).isFalse(); assertThat(superMetaClassType.isIdentityComparableWith(superMetaClassType)).isTrue(); } @@ -152,12 +151,12 @@ void mocks_should_have_and_declare_any_members() { @Test void type_types_can_have_any_member() { - assertThat(InferredTypes.TYPE.canHaveMember("foo")).isTrue(); - assertThat(InferredTypes.TYPE.declaresMember("foo")).isTrue(); - assertThat(InferredTypes.TYPE.resolveMember("foo")).isEmpty(); - assertThat(InferredTypes.TYPE.canHaveMember("bar")).isTrue(); - assertThat(InferredTypes.TYPE.declaresMember("bar")).isTrue(); - assertThat(InferredTypes.TYPE.resolveMember("bar")).isEmpty(); + assertThat(TYPE.canHaveMember("foo")).isTrue(); + assertThat(TYPE.declaresMember("foo")).isTrue(); + assertThat(TYPE.resolveMember("foo")).isEmpty(); + assertThat(TYPE.canHaveMember("bar")).isTrue(); + assertThat(TYPE.declaresMember("bar")).isTrue(); + assertThat(TYPE.resolveMember("bar")).isEmpty(); } @Test