mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Fixed bug in type evaluation for Python references inside isinstance checks
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user