Use upper-cased versions of built-in types in type descriptions (PY-16303)

This commit is contained in:
Andrey Vlasovskikh
2015-10-22 18:50:28 +03:00
parent a1d2464aa0
commit 5edd67644b
8 changed files with 29 additions and 18 deletions
@@ -42,6 +42,7 @@ import java.util.regex.Pattern;
*/
public class PyTypingTypeProvider extends PyTypeProviderBase {
public static final Pattern TYPE_COMMENT_PATTERN = Pattern.compile("# *type: *(.*)");
private static ImmutableMap<String, String> COLLECTION_CLASSES = ImmutableMap.<String, String>builder()
.put("typing.List", "list")
.put("typing.Dict", "dict")
@@ -59,6 +60,13 @@ public class PyTypingTypeProvider extends PyTypeProviderBase {
.put("typing.MutableSet", PyNames.COLLECTIONS + "." + "MutableSet")
.build();
public static ImmutableMap<String, String> TYPING_COLLECTION_CLASSES = ImmutableMap.<String, String>builder()
.put("list", "List")
.put("dict", "Dict")
.put("set", "Set")
.put("frozenset", "FrozenSet")
.build();
private static ImmutableSet<String> GENERIC_CLASSES = ImmutableSet.<String>builder()
.add("typing.Generic")
.add("typing.AbstractGeneric")
@@ -20,6 +20,7 @@ import com.google.common.collect.Collections2;
import com.google.common.collect.Maps;
import com.intellij.psi.PsiElement;
import com.jetbrains.python.PyNames;
import com.jetbrains.python.codeInsight.PyTypingTypeProvider;
import com.jetbrains.python.psi.types.*;
import com.jetbrains.python.toolbox.ChainIterable;
import org.jetbrains.annotations.NotNull;
@@ -405,7 +406,9 @@ public class PyTypeModelBuilder {
add("...");
return;
}
addType(collectionOf.collectionName);
final String name = collectionOf.collectionName;
final String typingName = PyTypingTypeProvider.TYPING_COLLECTION_CLASSES.get(name);
addType(typingName != null ? typingName : name);
add("[");
processList(collectionOf.elementTypes, ", ");
add("]");
@@ -79,7 +79,7 @@ def test():
return xs
return [
''.join(gen(10)),
f_1(<warning descr="Expected type 'list[int]', got '__generator[str, Any, None]' instead">gen(11)</warning>),
f_1(<warning descr="Expected type 'List[int]', got '__generator[str, Any, None]' instead">gen(11)</warning>),
f_2(<warning descr="Expected type 'Sequence[int]', got '__generator[str, Any, None]' instead">gen(11)</warning>),
f_3(<warning descr="Expected type 'Container[int]', got '__generator[str, Any, None]' instead">gen(11)</warning>),
f_4(<warning descr="Expected type 'Iterator[int]', got '__generator[str, Any, None]' instead">gen(11)</warning>),
@@ -89,7 +89,7 @@ def test():
f_8(<warning descr="Expected type 'Container', got '__generator[str, Any, None]' instead">gen(11)</warning>),
f_9(gen(11)),
f_10(gen(11)),
f_11(<warning descr="Expected type 'list[Union[str, unicode]]', got '__generator[str, Any, None]' instead">gen(11)</warning>),
f_11(<warning descr="Expected type 'List[Union[str, unicode]]', got '__generator[str, Any, None]' instead">gen(11)</warning>),
f_12(<warning descr="Expected type 'Sequence[Union[str, unicode]]', got '__generator[str, Any, None]' instead">gen(11)</warning>),
f_13(<warning descr="Expected type 'Container[Union[str, unicode]]', got '__generator[str, Any, None]' instead">gen(11)</warning>),
f_14(gen(11)),
@@ -40,7 +40,7 @@ def test():
print(result)
print(result + <warning descr="Expected type 'Number', got 'str' instead">'foo'</warning>)
f2(1, <weak_warning descr="Expected type 'list[int]' (matched generic type 'list[TypeVar('T')]'), got 'list[str]' instead">['foo']</weak_warning>, 'bar')
f2(1, <weak_warning descr="Expected type 'List[int]' (matched generic type 'List[TypeVar('T')]'), got 'List[str]' instead">['foo']</weak_warning>, 'bar')
result = f3(1, 'foo', True)
f4(<warning descr="Expected type 'Tuple[bool, int, str]', got 'Tuple[int, str, bool]' instead">result</warning>)
@@ -7,5 +7,5 @@ def f(spam, eggs):
def test():
f(<warning descr="Expected type 'list[Union[str, unicode]]', got 'list[int]' instead">[1, 2, 3]</warning>,
f(<warning descr="Expected type 'List[Union[str, unicode]]', got 'List[int]' instead">[1, 2, 3]</warning>,
<warning descr="Expected type 'Tuple[bool, int, unicode]', got 'Tuple[bool, int, str]' instead">(False, 2, '')</warning>)
@@ -4,7 +4,7 @@ from m1 import f, g, C, stub_only, Gen
def test_overloaded_function(x):
g(<warning descr="Expected type 'dict', got 'int' instead">f(10)</warning>)
g(<warning descr="Expected type 'dict', got 'str' instead">f('foo')</warning>)
g(<warning descr="Expected type 'dict', got 'Union[int, str]' instead">f(<warning descr="Expected type 'int', got 'dict[int, int]' instead">{1: 2}</warning>)</warning>)
g(<warning descr="Expected type 'dict', got 'Union[int, str]' instead">f(<warning descr="Expected type 'int', got 'Dict[int, int]' instead">{1: 2}</warning>)</warning>)
g(<warning descr="Expected type 'dict', got 'Union[int, str]' instead">f(x)</warning>)
@@ -12,21 +12,21 @@ def test_overloaded_subscription_operator_parameters():
c = C()
print(c[10])
print(c['foo'])
print(c[<warning descr="Expected type 'int', got 'dict[int, int]' instead">{1: 2}</warning>])
print(c[<warning descr="Expected type 'int', got 'Dict[int, int]' instead">{1: 2}</warning>])
def test_overloaded_binary_operator_parameters():
c = C()
print(c + 10)
print(c + 'foo')
print(c + <warning descr="Expected type 'int', got 'dict[int, int]' instead">{1: 2}</warning>)
print(c + <warning descr="Expected type 'int', got 'Dict[int, int]' instead">{1: 2}</warning>)
def test_stub_only_function(x):
g(<warning descr="Expected type 'dict', got 'int' instead">stub_only(10)</warning>)
g(<warning descr="Expected type 'dict', got 'str' instead">stub_only('foo')</warning>)
g(<warning descr="Expected type 'dict', got 'Union[int, str]' instead">stub_only(x)</warning>)
g(<warning descr="Expected type 'dict', got 'Union[int, str]' instead">stub_only(<warning descr="Expected type 'int', got 'dict[int, int]' instead">{1: 2}</warning>)</warning>)
g(<warning descr="Expected type 'dict', got 'Union[int, str]' instead">stub_only(<warning descr="Expected type 'int', got 'Dict[int, int]' instead">{1: 2}</warning>)</warning>)
def tset_overloaded_generics(x):
@@ -48,7 +48,7 @@ public class PyTypeTest extends PyTestCase {
"expr = '1' + '2'");
doTest("Union[str, unicode]",
"expr = '%s' % ('a')");
doTest("list[int]",
doTest("List[int]",
"expr = [1] + [2]");
}
@@ -97,7 +97,7 @@ public class PyTypeTest extends PyTestCase {
}
public void testSet() {
doTest("set[int]",
doTest("Set[int]",
"expr = {1, 2, 3}");
}
@@ -154,7 +154,7 @@ public class PyTypeTest extends PyTestCase {
}
public void testSliceType() {
doTest("list[int]",
doTest("List[int]",
"l = [1, 2, 3]; expr = l[0:1]");
}
@@ -439,7 +439,7 @@ public class PyTypeTest extends PyTestCase {
// PY-7215
public void testFunctionWithNestedGenerator() {
doTest("list[int]",
doTest("List[int]",
"def f():\n" +
" def g():\n" +
" yield 10\n" +
@@ -619,7 +619,7 @@ public class PyTypeTest extends PyTestCase {
}
public void testFunctionTypeAsUnificationArgument() {
doTest("Union[list[int], str, unicode]",
doTest("Union[List[int], str, unicode]",
"def map2(f, xs):\n" +
" '''\n" +
" :type f: (T) -> V | None\n" +
@@ -111,7 +111,7 @@ public class PyTypingTest extends PyTestCase {
}
public void testBuiltinListWithParameter() {
doTest("list[int]",
doTest("List[int]",
"from typing import List\n" +
"\n" +
"def f(expr: List[int]):\n" +
@@ -119,7 +119,7 @@ public class PyTypingTest extends PyTestCase {
}
public void testBuiltinDictWithParameters() {
doTest("dict[str, int]",
doTest("Dict[str, int]",
"from typing import Dict\n" +
"\n" +
"def f(expr: Dict[str, int]):\n" +
@@ -379,7 +379,7 @@ public class PyTypingTest extends PyTestCase {
// PY-16303
public void testAssignedTypeInDocstring() {
doTest("list[int]",
doTest("List[int]",
"from typing import List\n" +
"\n" +
"IntList = List[int]\n" +
@@ -393,7 +393,7 @@ public class PyTypingTest extends PyTestCase {
// PY-16303
public void testParameterAssignedTypeInDocstring() {
doTest("Union[int, list[int]]",
doTest("Union[int, List[int]]",
"from typing import List, Union\n" +
"\n" +
"IntList = List[int]\n" +