RuntimeType implements InferredType { @Override public boolean isCompatibleWith(InferredType other) { return InferredTypes.isTypeClassCompatibleWith(typeClass, other); } RuntimeType(ClassSymbol typeClass); @Override boolean isIdentityComparableWith(InferredType other); @Override boolean canHaveMember(String memberName); @Override boolean declaresMember(String memberName); @Override Optional<Symbol> resolveMember(String memberName); @Override Optional<Symbol> resolveDeclaredMember(String memberName); @Override boolean canOnlyBe(String typeName); @Override boolean canBeOrExtend(String typeName); @Override boolean isCompatibleWith(InferredType other); boolean mustBeOrExtend(String fullyQualifiedName); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); ClassSymbol getTypeClass();  }
@Test public void test_isCompatibleWith() { ClassSymbolImpl x1 = new ClassSymbolImpl("x1", "x1"); ClassSymbolImpl x2 = new ClassSymbolImpl("x2", "x2"); x2.addSuperClass(x1); assertThat(new RuntimeType(x2).isCompatibleWith(new RuntimeType(x1))).isTrue(); assertThat(new RuntimeType(x1).isCompatibleWith(new RuntimeType(x1))).isTrue(); assertThat(new RuntimeType(x1).isCompatibleWith(new RuntimeType(x2))).isTrue(); assertThat(new RuntimeType(x2).isCompatibleWith(new DeclaredType(x1))).isTrue(); assertThat(new RuntimeType(x1).isCompatibleWith(new DeclaredType(x1))).isTrue(); assertThat(new RuntimeType(x1).isCompatibleWith(new DeclaredType(x2))).isTrue(); assertThat(new RuntimeType(x1).isCompatibleWith(new DeclaredType(new SymbolImpl("foo", "foo")))).isTrue(); ClassSymbolImpl a = new ClassSymbolImpl("a", null); ClassSymbolImpl b = new ClassSymbolImpl("b", "b"); assertThat(new RuntimeType(a).isCompatibleWith(new RuntimeType(b))).isTrue(); assertThat(new RuntimeType(b).isCompatibleWith(new RuntimeType(a))).isTrue(); ClassSymbolImpl y = new ClassSymbolImpl("y", "y"); ClassSymbolImpl z = new ClassSymbolImpl("z", "z"); y.addSuperClass(new SymbolImpl("unknown", null)); assertThat(new RuntimeType(y).isCompatibleWith(new RuntimeType(z))).isTrue(); FileInput fileInput = PythonTestUtils.parse( new SymbolTableBuilder("animals", PythonTestUtils.pythonFile("foo")), "class duck:", " def swim(): ...", " def quack(): ...", "class goose:", " def swim(): ..."); ClassDef duckClass = PythonTestUtils.getFirstDescendant(fileInput, tree -> tree.is(Tree.Kind.CLASSDEF)); ClassDef gooseClass = PythonTestUtils.getLastDescendant(fileInput, tree -> tree.is(Tree.Kind.CLASSDEF)); ClassSymbol duck = TreeUtils.getClassSymbolFromDef(duckClass); ClassSymbol goose = TreeUtils.getClassSymbolFromDef(gooseClass); assertThat(new RuntimeType(duck).isCompatibleWith(new RuntimeType(goose))).isTrue(); assertThat(new RuntimeType(goose).isCompatibleWith(new RuntimeType(duck))).isFalse(); }
@Test public void test_isCompatibleWith_numbers() { assertThat(InferredTypes.INT.isCompatibleWith(InferredTypes.INT)).isTrue(); assertThat(InferredTypes.INT.isCompatibleWith(InferredTypes.FLOAT)).isTrue(); assertThat(InferredTypes.FLOAT.isCompatibleWith(InferredTypes.INT)).isFalse(); assertThat(InferredTypes.FLOAT.isCompatibleWith(InferredTypes.COMPLEX)).isTrue(); assertThat(InferredTypes.FLOAT.isCompatibleWith(InferredTypes.FLOAT)).isTrue(); assertThat(InferredTypes.INT.isCompatibleWith(InferredTypes.COMPLEX)).isTrue(); assertThat(InferredTypes.COMPLEX.isCompatibleWith(InferredTypes.COMPLEX)).isTrue(); assertThat(InferredTypes.COMPLEX.isCompatibleWith(InferredTypes.FLOAT)).isFalse(); assertThat(InferredTypes.COMPLEX.isCompatibleWith(InferredTypes.INT)).isFalse(); }
@Test public void test_isCompatibleWith_str() { InferredType unicode = runtimeBuiltinType("unicode"); assertThat(InferredTypes.STR.isCompatibleWith(unicode)).isTrue(); assertThat(InferredTypes.STR.isCompatibleWith(InferredTypes.STR)).isTrue(); assertThat(unicode.isCompatibleWith(InferredTypes.STR)).isFalse(); InferredType memoryView = runtimeBuiltinType("memoryview"); assertThat(memoryView.isCompatibleWith(InferredTypes.STR)).isTrue(); assertThat(InferredTypes.STR.isCompatibleWith(memoryView)).isFalse(); }
@Test public void test_isCompatibleWith_NoneType() { ClassSymbolImpl x1 = new ClassSymbolImpl("x1", "x1"); x1.addMembers(Collections.singletonList(new SymbolImpl("foo", null))); ClassSymbolImpl none = new ClassSymbolImpl("NoneType", "NoneType"); assertThat(new RuntimeType(x1).isCompatibleWith(new RuntimeType(none))).isFalse(); assertThat(new RuntimeType(none).isCompatibleWith(new RuntimeType(x1))).isFalse(); assertThat(new RuntimeType(none).isCompatibleWith(new RuntimeType(none))).isTrue(); }
@Test public void test_isCompatibleWith_declared_union_with_missing_symbols() { ClassSymbolImpl x = new ClassSymbolImpl("x", "x"); ClassSymbolImpl union = new ClassSymbolImpl("union", "typing.Union"); assertThat(new RuntimeType(x).isCompatibleWith(new DeclaredType(union))).isTrue(); }