Don't warn about using values with generic types as normal Python values (PY-7244)

Generics are not a part of Python, they are used in PyCharm as a convenient tool for more powerful code completion and weak type checking. Python programmers are not expected to annotate their classes with generics instantiation annotations.
This commit is contained in:
Andrey Vlasovskikh
2012-08-20 19:24:21 +04:00
parent d7b891fc49
commit 55ed25d706
5 changed files with 50 additions and 4 deletions
@@ -50,9 +50,6 @@ public class PyTypeChecker {
if (actual instanceof PyTypeReference) {
return match(expected, ((PyTypeReference)actual).resolve(null, context), context, substitutions, false);
}
if (isUnknown(actual)) {
return true;
}
if (expected instanceof PyGenericType && substitutions != null) {
final PyGenericType generic = (PyGenericType)expected;
final PyType subst = substitutions.get(generic);
@@ -72,6 +69,9 @@ public class PyTypeChecker {
return true;
}
}
if (isUnknown(actual)) {
return true;
}
if (actual instanceof PyUnionType) {
for (PyType m : ((PyUnionType)actual).getMembers()) {
if (!match(expected, m, context, substitutions, recursive)) {
@@ -149,7 +149,7 @@ public class PyTypeChecker {
}
public static boolean isUnknown(@Nullable PyType type) {
if (type == null || type instanceof PyTypeReference) {
if (type == null || type instanceof PyTypeReference || type instanceof PyGenericType) {
return true;
}
if (type instanceof PyUnionType) {
@@ -0,0 +1,18 @@
class B(object):
def __init__(self, x):
"""
:type x: T
:rtype: B of T
"""
self._x = x
def foo(self):
"""
:rtype: T
"""
return self._x
class C(B):
def bar(self):
expr = self.foo()
return 'foo' + expr #pass
@@ -0,0 +1,18 @@
class B(object):
def __init__(self, x):
"""
:type x: T
:rtype: B of T
"""
self._x = x
def foo(self):
"""
:rtype: T
"""
return self._x
class C(B):
def bar(self):
expr = self.foo()
return expr.upper() #pass
@@ -140,4 +140,9 @@ public class PyTypeCheckerInspectionTest extends PyTestCase {
public void testAssignedOperator() {
doTest();
}
// PY-7244
public void testGenericArguments() {
doTest();
}
}
@@ -175,6 +175,11 @@ public class PyUnresolvedReferencesInspectionTest extends PyTestCase {
doTest();
}
// PY-7244
public void testAttributesOfGenerics() {
doTest();
}
private void doTest() {
myFixture.configureByFile(TEST_DIRECTORY + getTestName(true) + ".py");
myFixture.enableInspections(PyUnresolvedReferencesInspection.class);