PY-20057 Consider incompatible types one of which is Type[] in the type checker

This commit is contained in:
Mikhail Golubev
2017-03-06 15:20:44 +03:00
parent b6dbc5b989
commit 2c79c98d22
3 changed files with 42 additions and 1 deletions
@@ -73,10 +73,18 @@ public class PyTypeChecker {
return match(expected, builtinCache.getStrOrUnicodeType(), context, substitutions, recursive);
}
}
if (expected instanceof PyInstantiableType && actual instanceof PyInstantiableType
&& ((PyInstantiableType)expected).isDefinition() ^ ((PyInstantiableType)actual).isDefinition()) {
return false;
}
if (expected instanceof PyGenericType && substitutions != null) {
final PyGenericType generic = (PyGenericType)expected;
final PyType subst = substitutions.get(generic);
final PyType bound = generic.getBound();
PyType bound = generic.getBound();
// Promote int in Type[TypeVar('T', int)] to Type[int] before checking that bounds match
if (generic.isDefinition() && bound instanceof PyInstantiableType) {
bound = ((PyInstantiableType)bound).toClass();
}
if (!match(bound, actual, context, substitutions, recursive)) {
return false;
}
@@ -0,0 +1,28 @@
from typing import Type, TypeVar
class MyClass:
pass
def expects_myclass(x: Type[MyClass]):
pass
expects_myclass(<warning descr="Expected type 'Type[MyClass]', got 'MyClass' instead">MyClass()</warning>)
expects_class(MyClass)
T1 = TypeVar('T1')
def expects_any_class(x: Type[T1]):
pass
# expects_any_class(MyClass)
expects_any_class(<warning descr="Expected type 'Type[TypeVar('T1')]', got 'MyClass' instead">MyClass()</warning>)
# expects_any_class(object)
expects_any_class(<warning descr="Expected type 'Type[TypeVar('T1')]', got 'object' instead">object()</warning>)
T2 = TypeVar('T2', MyClass)
def expects_myclass_descendant(x: Type[T2]):
pass
expects_myclass_descendant(MyClass)
expects_myclass_descendant(<warning descr="Expected type 'Type[TypeVar('T2', MyClass)]', got 'MyClass' instead">MyClass()</warning>)
expects_myclass_descendant(<warning descr="Expected type 'Type[TypeVar('T2', MyClass)]', got 'Type[object]' instead">object</warning>)
expects_myclass_descendant(<warning descr="Expected type 'Type[TypeVar('T2', MyClass)]', got 'object' instead">object()</warning>)
@@ -198,4 +198,9 @@ public class Py3TypeCheckerInspectionTest extends PyTestCase {
public void testBuiltinInputPy3() {
doTest();
}
// PY-200057
public void testClassObjectType() {
doTest();
}
}