PY-22919 Fixed: max() function transmits argument type to return type

PY-23634 Fixed: min() from comprehension with unknown items type return list

Primarily these issues were fixed by PY-22808-related commits.
In these change inferred call type is more precise. PyiTypeProvider doesn't take into account overloads that have unmapped args or params.
This commit is contained in:
Semyon Proshev
2017-04-18 15:45:44 +03:00
parent 844082c455
commit 7a23d509b8
3 changed files with 29 additions and 5 deletions
@@ -29,8 +29,6 @@ import org.jetbrains.annotations.Nullable;
import java.util.*;
import static com.jetbrains.python.psi.PyUtil.as;
/**
* @author vlan
*/
@@ -212,10 +210,10 @@ public class PyiTypeProvider extends PyTypeProviderBase {
final PyCallExpressionHelper.ArgumentMappingResults mapping =
PyCallExpressionHelper.mapArguments(callSite, function, parameters, context);
final PyCallExpression callExpr = as(callSite, PyCallExpression.class);
if (callExpr != null && callExpr.getArguments().length != mapping.getMappedParameters().size()) {
if (!mapping.getUnmappedArguments().isEmpty() || !mapping.getUnmappedParameters().isEmpty()) {
return null;
}
return mapping;
}
}
@@ -22,4 +22,4 @@ def test(c):
f2(<warning descr="Expected type 'str', got 'int' instead">x1.count('')</warning>)
f3(x1.count(''))
f2(x1.strip())
f3(<warning descr="Expected type 'int', got 'Union[str, unicode]' instead">x1.strip()</warning>)
f3(<warning descr="Expected type 'int', got 'str' instead">x1.strip()</warning>)
@@ -1677,6 +1677,32 @@ public class PyTypeTest extends PyTestCase {
" return get_class()");
}
// PY-22919
public void testMaxListKnownElements() {
doTest("int",
"expr = max([1, 2, 3])");
}
// PY-22919
public void testMaxListUnknownElements() {
doTest("Any",
"l = []\n" +
"expr = max(l)");
}
// PY-23634
public void testMinListKnownElements() {
doTest("int",
"expr = min([1, 2, 3])");
}
// PY-23634
public void testMinListUnknownElements() {
doTest("Any",
"l = []\n" +
"expr = min(l)");
}
private static List<TypeEvalContext> getTypeEvalContexts(@NotNull PyExpression element) {
return ImmutableList.of(TypeEvalContext.codeAnalysis(element.getProject(), element.getContainingFile()).withTracing(),
TypeEvalContext.userInitiated(element.getProject(), element.getContainingFile()).withTracing());