diff --git a/python/src/com/jetbrains/python/psi/impl/PySubscriptionExpressionImpl.java b/python/src/com/jetbrains/python/psi/impl/PySubscriptionExpressionImpl.java index 5fb1a47209e1..7ace1a928f4b 100644 --- a/python/src/com/jetbrains/python/psi/impl/PySubscriptionExpressionImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PySubscriptionExpressionImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2016 JetBrains s.r.o. + * Copyright 2000-2017 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,6 +42,7 @@ public class PySubscriptionExpressionImpl extends PyElementImpl implements PySub super(astNode); } + @Override @NotNull public PyExpression getOperand() { return childToPsiNotNull(PythonDialectsTokenSetProvider.INSTANCE.getExpressionTokens(), 0); @@ -57,6 +58,7 @@ public class PySubscriptionExpressionImpl extends PyElementImpl implements PySub return operand; } + @Override @Nullable public PyExpression getIndexExpression() { return childToPsi(PythonDialectsTokenSetProvider.INSTANCE.getExpressionTokens(), 1); @@ -98,6 +100,11 @@ public class PySubscriptionExpressionImpl extends PyElementImpl implements PySub } members.add(res); } + + if (type instanceof PyUnionType && ((PyUnionType)type).isWeak()) { + return PyUnionType.createWeakType(PyUnionType.union(members)); + } + return PyUnionType.union(members); } diff --git a/python/src/com/jetbrains/python/psi/types/PyTypeChecker.java b/python/src/com/jetbrains/python/psi/types/PyTypeChecker.java index 93ab6cfc6365..ee20747700bd 100644 --- a/python/src/com/jetbrains/python/psi/types/PyTypeChecker.java +++ b/python/src/com/jetbrains/python/psi/types/PyTypeChecker.java @@ -561,7 +561,7 @@ public class PyTypeChecker { for (PyTypeProvider provider : Extensions.getExtensions(PyTypeProvider.EP_NAME)) { final PyType genericType = provider.getGenericType(type.getPyClass(), context); if (genericType != null) { - match(genericType, qualifierType, context, substitutions); + match(genericType, type, context, substitutions); } for (Map.Entry entry : provider.getGenericSubstitutions(type.getPyClass(), context).entrySet()) { final PyGenericType genericKey = as(entry.getKey(), PyGenericType.class); diff --git a/python/testSrc/com/jetbrains/python/PyTypeTest.java b/python/testSrc/com/jetbrains/python/PyTypeTest.java index ddfea706620b..ebea739ab568 100644 --- a/python/testSrc/com/jetbrains/python/PyTypeTest.java +++ b/python/testSrc/com/jetbrains/python/PyTypeTest.java @@ -1897,6 +1897,13 @@ public class PyTypeTest extends PyTestCase { ); } + // PY-24383 + public void testSubscriptionOnWeakType() { + doTest("Union[int, Any]", + "foo = bar() if 42 != 42 else [1, 2, 3, 4]\n" + + "expr = foo[0]"); + } + private static List getTypeEvalContexts(@NotNull PyExpression element) { return ImmutableList.of(TypeEvalContext.codeAnalysis(element.getProject(), element.getContainingFile()).withTracing(), TypeEvalContext.userInitiated(element.getProject(), element.getContainingFile()).withTracing());