From 6d5be90de727c79297737ce16774de3873ca8fdc Mon Sep 17 00:00:00 2001 From: Semyon Proshev Date: Tue, 21 Nov 2017 16:38:31 +0300 Subject: [PATCH] Also drop `cls`-parameter for qualified reference that resolves to method (PY-26616) --- .../python/psi/impl/PyCallExpressionHelper.java | 6 +++--- .../psi/impl/PyReferenceExpressionImpl.java | 17 ++++++----------- .../com/jetbrains/python/PyTypeTest.java | 10 ++++++++++ 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/python/src/com/jetbrains/python/psi/impl/PyCallExpressionHelper.java b/python/src/com/jetbrains/python/psi/impl/PyCallExpressionHelper.java index f94b10977f69..d507b54f48a5 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyCallExpressionHelper.java +++ b/python/src/com/jetbrains/python/psi/impl/PyCallExpressionHelper.java @@ -369,9 +369,9 @@ public class PyCallExpressionHelper { return false; } - public static boolean isQualifiedByInstance(@Nullable PyCallable resolved, - @NotNull PyExpression qualifier, - @NotNull TypeEvalContext context) { + private static boolean isQualifiedByInstance(@Nullable PyCallable resolved, + @NotNull PyExpression qualifier, + @NotNull TypeEvalContext context) { if (isQualifiedByClass(resolved, qualifier, context)) { return false; } diff --git a/python/src/com/jetbrains/python/psi/impl/PyReferenceExpressionImpl.java b/python/src/com/jetbrains/python/psi/impl/PyReferenceExpressionImpl.java index 6857e543657d..05b1795551f7 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyReferenceExpressionImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyReferenceExpressionImpl.java @@ -410,7 +410,7 @@ public class PyReferenceExpressionImpl extends PyElementImpl implements PyRefere private static PyType getTypeFromTarget(@NotNull PsiElement target, @NotNull TypeEvalContext context, @NotNull PyReferenceExpression anchor) { - final PyType type = dropSelfForInstanceMethod(getGenericTypeFromTarget(target, context, anchor), context, anchor); + final PyType type = dropSelfForQualifiedMethod(getGenericTypeFromTarget(target, context, anchor), context, anchor); if (context.maySwitchToAST(anchor)) { final PyExpression qualifier = anchor.getQualifier(); @@ -501,16 +501,11 @@ public class PyReferenceExpressionImpl extends PyElementImpl implements PyRefere } @Nullable - private static PyType dropSelfForInstanceMethod(@Nullable PyType type, - @NotNull TypeEvalContext context, - @NotNull PyReferenceExpression anchor) { - if (type instanceof PyFunctionType && context.maySwitchToAST(anchor)) { - final PyExpression qualifier = anchor.getQualifier(); - final PyFunctionType functionType = (PyFunctionType)type; - - if (qualifier != null && PyCallExpressionHelper.isQualifiedByInstance(functionType.getCallable(), qualifier, context)) { - return functionType.dropSelf(context); - } + private static PyType dropSelfForQualifiedMethod(@Nullable PyType type, + @NotNull TypeEvalContext context, + @NotNull PyReferenceExpression anchor) { + if (type instanceof PyFunctionType && context.maySwitchToAST(anchor) && anchor.getQualifier() != null) { + return ((PyFunctionType)type).dropSelf(context); } return type; diff --git a/python/testSrc/com/jetbrains/python/PyTypeTest.java b/python/testSrc/com/jetbrains/python/PyTypeTest.java index fc80f17c8c6f..4edb351c5c67 100644 --- a/python/testSrc/com/jetbrains/python/PyTypeTest.java +++ b/python/testSrc/com/jetbrains/python/PyTypeTest.java @@ -1947,6 +1947,16 @@ public class PyTypeTest extends PyTestCase { "expr = my_list.count"); } + // PY-26616 + public void testClassMethodQualifiedWithDefinition() { + doTest("(x: str) -> Foo", + "class Foo:\n" + + " @classmethod\n" + + " def make_foo(cls, x: str) -> 'Foo':\n" + + " pass\n" + + "expr = Foo.make_foo"); + } + public void testConstructingGenericClassWithNotFilledGenericValue() { doTest("MyIterator", "from typing import Iterator\n" +