mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
PY-20057 Consider incompatible types one of which is Type[] in the type checker
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user