From 5edd67644b5f71f17a4b7927d9dedfa5558c329a Mon Sep 17 00:00:00 2001 From: Andrey Vlasovskikh Date: Thu, 22 Oct 2015 18:50:28 +0300 Subject: [PATCH] Use upper-cased versions of built-in types in type descriptions (PY-16303) --- .../python/codeInsight/PyTypingTypeProvider.java | 8 ++++++++ .../python/documentation/PyTypeModelBuilder.java | 5 ++++- .../inspections/PyTypeCheckerInspection/Generator.py | 4 ++-- .../PyTypeCheckerInspection/GenericUserFunctions.py | 2 +- .../inspections/PyTypeCheckerInspection/ListTuple.py | 2 +- python/testData/pyi/inspections/overloads/Overloads.py | 8 ++++---- python/testSrc/com/jetbrains/python/PyTypeTest.java | 10 +++++----- python/testSrc/com/jetbrains/python/PyTypingTest.java | 8 ++++---- 8 files changed, 29 insertions(+), 18 deletions(-) diff --git a/python/src/com/jetbrains/python/codeInsight/PyTypingTypeProvider.java b/python/src/com/jetbrains/python/codeInsight/PyTypingTypeProvider.java index 271930390272..4c44890d7836 100644 --- a/python/src/com/jetbrains/python/codeInsight/PyTypingTypeProvider.java +++ b/python/src/com/jetbrains/python/codeInsight/PyTypingTypeProvider.java @@ -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 COLLECTION_CLASSES = ImmutableMap.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 TYPING_COLLECTION_CLASSES = ImmutableMap.builder() + .put("list", "List") + .put("dict", "Dict") + .put("set", "Set") + .put("frozenset", "FrozenSet") + .build(); + private static ImmutableSet GENERIC_CLASSES = ImmutableSet.builder() .add("typing.Generic") .add("typing.AbstractGeneric") diff --git a/python/src/com/jetbrains/python/documentation/PyTypeModelBuilder.java b/python/src/com/jetbrains/python/documentation/PyTypeModelBuilder.java index 8fdd9b0460c5..c7d42130cf33 100644 --- a/python/src/com/jetbrains/python/documentation/PyTypeModelBuilder.java +++ b/python/src/com/jetbrains/python/documentation/PyTypeModelBuilder.java @@ -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("]"); diff --git a/python/testData/inspections/PyTypeCheckerInspection/Generator.py b/python/testData/inspections/PyTypeCheckerInspection/Generator.py index ac43b303784e..5b367dd3485c 100644 --- a/python/testData/inspections/PyTypeCheckerInspection/Generator.py +++ b/python/testData/inspections/PyTypeCheckerInspection/Generator.py @@ -79,7 +79,7 @@ def test(): return xs return [ ''.join(gen(10)), - f_1(gen(11)), + f_1(gen(11)), f_2(gen(11)), f_3(gen(11)), f_4(gen(11)), @@ -89,7 +89,7 @@ def test(): f_8(gen(11)), f_9(gen(11)), f_10(gen(11)), - f_11(gen(11)), + f_11(gen(11)), f_12(gen(11)), f_13(gen(11)), f_14(gen(11)), diff --git a/python/testData/inspections/PyTypeCheckerInspection/GenericUserFunctions.py b/python/testData/inspections/PyTypeCheckerInspection/GenericUserFunctions.py index 2fd5ffb4c22f..33a56eca93f9 100644 --- a/python/testData/inspections/PyTypeCheckerInspection/GenericUserFunctions.py +++ b/python/testData/inspections/PyTypeCheckerInspection/GenericUserFunctions.py @@ -40,7 +40,7 @@ def test(): print(result) print(result + 'foo') - f2(1, ['foo'], 'bar') + f2(1, ['foo'], 'bar') result = f3(1, 'foo', True) f4(result) diff --git a/python/testData/inspections/PyTypeCheckerInspection/ListTuple.py b/python/testData/inspections/PyTypeCheckerInspection/ListTuple.py index 2d6e638ce3b9..1c763e03bf1e 100644 --- a/python/testData/inspections/PyTypeCheckerInspection/ListTuple.py +++ b/python/testData/inspections/PyTypeCheckerInspection/ListTuple.py @@ -7,5 +7,5 @@ def f(spam, eggs): def test(): - f([1, 2, 3], + f([1, 2, 3], (False, 2, '')) diff --git a/python/testData/pyi/inspections/overloads/Overloads.py b/python/testData/pyi/inspections/overloads/Overloads.py index 954390000ab9..86d06b23c14f 100644 --- a/python/testData/pyi/inspections/overloads/Overloads.py +++ b/python/testData/pyi/inspections/overloads/Overloads.py @@ -4,7 +4,7 @@ from m1 import f, g, C, stub_only, Gen def test_overloaded_function(x): g(f(10)) g(f('foo')) - g(f({1: 2})) + g(f({1: 2})) g(f(x)) @@ -12,21 +12,21 @@ def test_overloaded_subscription_operator_parameters(): c = C() print(c[10]) print(c['foo']) - print(c[{1: 2}]) + print(c[{1: 2}]) def test_overloaded_binary_operator_parameters(): c = C() print(c + 10) print(c + 'foo') - print(c + {1: 2}) + print(c + {1: 2}) def test_stub_only_function(x): g(stub_only(10)) g(stub_only('foo')) g(stub_only(x)) - g(stub_only({1: 2})) + g(stub_only({1: 2})) def tset_overloaded_generics(x): diff --git a/python/testSrc/com/jetbrains/python/PyTypeTest.java b/python/testSrc/com/jetbrains/python/PyTypeTest.java index 32a318c62c9d..2eac92ca130e 100644 --- a/python/testSrc/com/jetbrains/python/PyTypeTest.java +++ b/python/testSrc/com/jetbrains/python/PyTypeTest.java @@ -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" + diff --git a/python/testSrc/com/jetbrains/python/PyTypingTest.java b/python/testSrc/com/jetbrains/python/PyTypingTest.java index 2411e2ef0478..6f840f5923ae 100644 --- a/python/testSrc/com/jetbrains/python/PyTypingTest.java +++ b/python/testSrc/com/jetbrains/python/PyTypingTest.java @@ -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" +