Also drop cls-parameter for qualified reference that resolves to method (PY-26616)

This commit is contained in:
Semyon Proshev
2017-12-11 15:38:56 +03:00
parent a13b4625c3
commit 6d5be90de7
3 changed files with 19 additions and 14 deletions
@@ -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;
}
@@ -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;
@@ -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" +