Skip to content

Commit

Permalink
SONARPY-2332 review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-serre-sonarsource committed Nov 22, 2024
1 parent 399db7e commit 93f19e6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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();
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 93f19e6

Please sign in to comment.