Fixed bug in type evaluation for Python references inside isinstance checks

This commit is contained in:
Andrey Vlasovskikh
2011-09-02 13:11:07 +04:00
parent 6c3bfd36bc
commit dedcf13073
3 changed files with 20 additions and 2 deletions
@@ -273,7 +273,7 @@ public class PyReferenceExpressionImpl extends PyElementImpl implements PyRefere
@Nullable
public static PyType getTypeFromTarget(@NotNull final PsiElement target,
final TypeEvalContext context,
@Nullable PyReferenceExpression anchor) {
PyReferenceExpression anchor) {
final PyType pyType = getReferenceTypeFromProviders(target, context, anchor);
if (pyType != null) {
return pyType;
@@ -155,7 +155,7 @@ public class PyTargetExpressionImpl extends PyPresentableElementImpl<PyTargetExp
if (target == this || target == null) {
return null; // fix SOE on "a = a"
}
final PyType typeFromTarget = PyReferenceExpressionImpl.getTypeFromTarget(target, context, null);
final PyType typeFromTarget = PyReferenceExpressionImpl.getTypeFromTarget(target, context, refex);
if (target instanceof PyTargetExpression && typeFromTarget instanceof PyNoneType) {
// this usually means that the variable is initialized to a non-None value somewhere else where we haven't looked
return null;
@@ -237,11 +237,29 @@ public class PyTypeTest extends PyLightFixtureTestCase {
assertInstanceOf(t, PyTypeReference.class);
}
public void testIsInstance() {
doTest("str",
"def f(c):\n" +
" def g():\n" +
" '''\n" +
" :rtype: int or str\n" +
" '''\n" +
" x = g()\n" +
" if isinstance(x, str):\n" +
" expr = x");
}
private PyExpression parseExpr(String text) {
myFixture.configureByText(PythonFileType.INSTANCE, text);
return myFixture.findElementByText("expr", PyExpression.class);
}
private static String msg(PyType expected, PyType actual, TypeEvalContext context) {
return String.format("Expected: %s, actual: %s",
PythonDocumentationProvider.getTypeName(expected, context),
PythonDocumentationProvider.getTypeName(actual, context));
}
private void doTest(final String expectedType, final String text) {
PyExpression expr = parseExpr(text);
TypeEvalContext context = TypeEvalContext.slow().withTracing();