From a2bacfa2ada21160bfa9d474d531748d2694f6bb Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Wed, 7 May 2025 12:21:06 +0300 Subject: [PATCH] PY-80936 Don't hide generic type arguments if they all are Any GitOrigin-RevId: d3354c68617a8176f9ef2f78b0c37afed56add51 --- .../python/documentation/PyTypeRenderer.java | 14 +------------ .../AsyncForIterable.py | 2 +- .../PyTypeCheckerInspection/BoundedGeneric.py | 2 +- .../ComparisonOperators.py | 2 +- .../FunctionReturnTypePy3.py | 2 +- .../TypingCallableCall.py | 2 +- .../UnionReturnTypes.py | 2 +- .../com/jetbrains/python/PyTypeTest.java | 20 +++++++++---------- .../com/jetbrains/python/PyTypingTest.java | 10 +++++----- .../PyTypeCheckerInspectionTest.java | 10 +++++----- .../com/jetbrains/python/pyi/PyiTypeTest.java | 4 ++-- 11 files changed, 29 insertions(+), 41 deletions(-) diff --git a/python/python-psi-impl/src/com/jetbrains/python/documentation/PyTypeRenderer.java b/python/python-psi-impl/src/com/jetbrains/python/documentation/PyTypeRenderer.java index 45e238c6a1dc..2ca7222696b9 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/documentation/PyTypeRenderer.java +++ b/python/python-psi-impl/src/com/jetbrains/python/documentation/PyTypeRenderer.java @@ -114,11 +114,6 @@ public abstract class PyTypeRenderer extends PyTypeVisitorExt<@NotNull HtmlChunk return false; } - @Override - protected boolean hideAllAnyTypeArguments() { - return false; - } - @Override public HtmlChunk visitPyCallableType(@NotNull PyCallableType callableType) { HtmlBuilder result = new HtmlBuilder(); @@ -191,10 +186,6 @@ public abstract class PyTypeRenderer extends PyTypeVisitorExt<@NotNull HtmlChunk return origin == null || PythonLanguageLevelPusher.getLanguageLevelForFile(origin).isAtLeast(LanguageLevel.PYTHON39); } - protected boolean hideAllAnyTypeArguments() { - return true; - } - @Override public HtmlChunk visitPyGenericType(@NotNull PyCollectionType collectionOf) { HtmlChunk genericTypeRender = renderGenericType(collectionOf); @@ -203,10 +194,7 @@ public abstract class PyTypeRenderer extends PyTypeVisitorExt<@NotNull HtmlChunk private @NotNull HtmlChunk renderGenericType(@NotNull PyCollectionType genericType) { HtmlBuilder result = new HtmlBuilder(); - // TODO get rid of that behavior - boolean allTypeParamsAreAny = ContainerUtil.and(genericType.getElementTypes(), t -> t == null); - boolean renderTypeArgumentList = !genericType.getElementTypes().isEmpty() && - !(hideAllAnyTypeArguments() && allTypeParamsAreAny); + boolean renderTypeArgumentList = !genericType.getElementTypes().isEmpty(); String className = genericType.getPyClass().getName(); if (renderTypeArgumentList && !isGenericBuiltinsAvailable() && PyTypingTypeProvider.TYPING_COLLECTION_CLASSES.containsKey(className)) { result.append(className(PyTypingTypeProvider.TYPING_COLLECTION_CLASSES.get(className))); // NON-NLS diff --git a/python/testData/inspections/PyTypeCheckerInspection/AsyncForIterable.py b/python/testData/inspections/PyTypeCheckerInspection/AsyncForIterable.py index 986fc4fec7aa..561c35b68368 100644 --- a/python/testData/inspections/PyTypeCheckerInspection/AsyncForIterable.py +++ b/python/testData/inspections/PyTypeCheckerInspection/AsyncForIterable.py @@ -30,7 +30,7 @@ async def coro(): else: print('end') - async for i in []: + async for i in []: pass diff --git a/python/testData/inspections/PyTypeCheckerInspection/BoundedGeneric.py b/python/testData/inspections/PyTypeCheckerInspection/BoundedGeneric.py index 588ecaa36704..dc828835f6f5 100644 --- a/python/testData/inspections/PyTypeCheckerInspection/BoundedGeneric.py +++ b/python/testData/inspections/PyTypeCheckerInspection/BoundedGeneric.py @@ -8,5 +8,5 @@ def test(): x = f(10) y = f('foo') - z = f([]) + z = f([]) return x + y diff --git a/python/testData/inspections/PyTypeCheckerInspection/ComparisonOperators.py b/python/testData/inspections/PyTypeCheckerInspection/ComparisonOperators.py index 2d64733b86cb..3b745411b446 100644 --- a/python/testData/inspections/PyTypeCheckerInspection/ComparisonOperators.py +++ b/python/testData/inspections/PyTypeCheckerInspection/ComparisonOperators.py @@ -14,6 +14,6 @@ def test(): f(o >= o) f('foo' > 'bar') f(c < 1) - f(c > 1) + f(c > 1) f(c == 1) f(c in [1, 2, 3]) diff --git a/python/testData/inspections/PyTypeCheckerInspection/FunctionReturnTypePy3.py b/python/testData/inspections/PyTypeCheckerInspection/FunctionReturnTypePy3.py index b63445847ef1..ab6ff4ffeea3 100644 --- a/python/testData/inspections/PyTypeCheckerInspection/FunctionReturnTypePy3.py +++ b/python/testData/inspections/PyTypeCheckerInspection/FunctionReturnTypePy3.py @@ -28,7 +28,7 @@ def g(x) -> int: if x: return 'abc' else: - return {} + return {} def h(x) -> int: return diff --git a/python/testData/inspections/PyTypeCheckerInspection/TypingCallableCall.py b/python/testData/inspections/PyTypeCheckerInspection/TypingCallableCall.py index f054ba1515c0..71758d757794 100644 --- a/python/testData/inspections/PyTypeCheckerInspection/TypingCallableCall.py +++ b/python/testData/inspections/PyTypeCheckerInspection/TypingCallableCall.py @@ -29,4 +29,4 @@ cllbl_c = baz() cllbl_c(1, "2") cllbl_c(1, 2) cllbl_c("1", "2") -cllbl_c([], []) +cllbl_c([], []) diff --git a/python/testData/inspections/PyTypeCheckerInspection/UnionReturnTypes.py b/python/testData/inspections/PyTypeCheckerInspection/UnionReturnTypes.py index 30e2ab7ddda1..0a9a0263507b 100644 --- a/python/testData/inspections/PyTypeCheckerInspection/UnionReturnTypes.py +++ b/python/testData/inspections/PyTypeCheckerInspection/UnionReturnTypes.py @@ -17,7 +17,7 @@ def test(c): """ x1 = f1(c) f2(x1) # Weaker union types - f3(x1) + f3(x1) f2(x1.count('')) f3(x1.count('')) diff --git a/python/testSrc/com/jetbrains/python/PyTypeTest.java b/python/testSrc/com/jetbrains/python/PyTypeTest.java index 0ced222ed113..703acf093410 100644 --- a/python/testSrc/com/jetbrains/python/PyTypeTest.java +++ b/python/testSrc/com/jetbrains/python/PyTypeTest.java @@ -686,7 +686,7 @@ public class PyTypeTest extends PyTestCase { // EA-40207 public void testRecursion() { - doTest("list", + doTest("List[Any]", """ def f(): return [f()] @@ -1244,7 +1244,7 @@ public class PyTypeTest extends PyTestCase { } public void testUnionTypeAttributeOfDifferentTypes() { - doTest("Union[list, int]", + doTest("Union[List[Any], int]", """ class Foo: x = [] @@ -1640,7 +1640,7 @@ public class PyTypeTest extends PyTestCase { } public void testListLiteral() { - doTest("list", "expr = []"); + doTest("List[Any]", "expr = []"); doTest("List[int]", "expr = [1, 2, 3]"); @@ -1658,7 +1658,7 @@ public class PyTypeTest extends PyTestCase { } public void testDictLiteral() { - doTest("dict", "expr = {}"); + doTest("Dict[Any, Any]", "expr = {}"); doTest("Dict[int, bool]", "expr = {1: False}"); @@ -1712,7 +1712,7 @@ public class PyTypeTest extends PyTestCase { // PY-20797 public void testValueOfEmptyDefaultDict() { - doTest("list", + doTest("List[Any]", """ from collections import defaultdict expr = defaultdict(lambda: [])['x'] @@ -1894,7 +1894,7 @@ public class PyTypeTest extends PyTestCase { // PY-21474 public void testReassigningOptionalListWithDefaultValue() { - doTest("Union[List[str], list]", + doTest("Union[List[str], List[Any]]", """ def x(things): ""\" @@ -2034,7 +2034,7 @@ public class PyTypeTest extends PyTestCase { // PY-37755 public void testGlobalType() { - doTest("list", + doTest("List[Any]", """ expr = [] @@ -2042,7 +2042,7 @@ public class PyTypeTest extends PyTestCase { global expr expr"""); - doTest("list", + doTest("List[Any]", """ expr = [] @@ -2051,7 +2051,7 @@ public class PyTypeTest extends PyTestCase { global expr expr"""); - doTest("list", + doTest("List[Any]", """ expr = [] @@ -2928,7 +2928,7 @@ public class PyTypeTest extends PyTestCase { // PY-26061 public void testUnknownDictValues() { - doTest("list", + doTest("List[Any]", "expr = dict().values()"); } diff --git a/python/testSrc/com/jetbrains/python/PyTypingTest.java b/python/testSrc/com/jetbrains/python/PyTypingTest.java index e7cbf22b7cdb..0453154ce53b 100644 --- a/python/testSrc/com/jetbrains/python/PyTypingTest.java +++ b/python/testSrc/com/jetbrains/python/PyTypingTest.java @@ -1699,7 +1699,7 @@ public class PyTypingTest extends PyTestCase { // PY-31004 public void testRecursiveTypeAliasInAnotherFile() { - doMultiFileStubAwareTest("list | int", + doMultiFileStubAwareTest("list[Any] | int", """ from other import MyType @@ -2512,7 +2512,7 @@ public class PyTypingTest extends PyTestCase { // PY-53105 public void testGenericVariadicStarArgsPrefixSuffix() { - doTest("tuple[str, list, dict, bool, int]", + doTest("tuple[str, list[Any], dict[Any, Any], bool, int]", """ from typing import TypeVarTuple, Tuple @@ -4233,7 +4233,7 @@ public class PyTypingTest extends PyTestCase { // PY-61883 public void testRecursiveTypeAliasInAnotherFilePEP695Syntax() { - doMultiFileStubAwareTest("list | int", + doMultiFileStubAwareTest("list[Any] | int", """ from a import MyType @@ -4721,7 +4721,7 @@ public class PyTypingTest extends PyTestCase { // PY-71002 public void testTypeVarDefaultsClassWithInitMethodReference() { - doTest("type[Bar[Any, list]]", """ + doTest("type[Bar[Any, list[Any]]]", """ from typing import TypeVar, Generic Z1 = TypeVar("Z1") ListDefaultT = TypeVar("ListDefaultT", default=list[Z1]) @@ -4733,7 +4733,7 @@ public class PyTypingTest extends PyTestCase { // PY-71002 public void testTypeVarDefaultsClassWithInitMethodReferenceNewSyntax() { - doTest("type[Bar[Any, list]]", """ + doTest("type[Bar[Any, list[Any]]]", """ from typing import TypeVar, Generic class Bar[Z1, ListDefaultT = list[Z1]]: def __init__(self, x: Z1, y: ListDefaultT): ... diff --git a/python/testSrc/com/jetbrains/python/inspections/PyTypeCheckerInspectionTest.java b/python/testSrc/com/jetbrains/python/inspections/PyTypeCheckerInspectionTest.java index 3c701d9ce7a1..aca164cbe5c6 100644 --- a/python/testSrc/com/jetbrains/python/inspections/PyTypeCheckerInspectionTest.java +++ b/python/testSrc/com/jetbrains/python/inspections/PyTypeCheckerInspectionTest.java @@ -1435,13 +1435,13 @@ public class PyTypeCheckerInspectionTest extends PyInspectionTestCase { y = {} z = {'foo': 'bar'} n = {"foo": "", "quux": 3} - f(y) + f(y) f(n) f(z) - f(x=y) + f(x=y) f(x=n) f(x=z) - z2: C = y + z2: C = y z2: C = n z2: C = z """) @@ -1505,8 +1505,8 @@ public class PyTypeCheckerInspectionTest extends PyInspectionTestCase { 'd': {} } } - s2: HardDict = {'a': 'xx', 'd': t1} - s3: HardDict = t1 + s2: HardDict = {'a': 'xx', 'd': t1} + s3: HardDict = t1 s4: HardDict = { 'd': { 'a': 'a', diff --git a/python/testSrc/com/jetbrains/python/pyi/PyiTypeTest.java b/python/testSrc/com/jetbrains/python/pyi/PyiTypeTest.java index 62276da09fa5..90e8cffaade0 100644 --- a/python/testSrc/com/jetbrains/python/pyi/PyiTypeTest.java +++ b/python/testSrc/com/jetbrains/python/pyi/PyiTypeTest.java @@ -111,12 +111,12 @@ public class PyiTypeTest extends PyTestCase { // PY-22808 public void testOverloadedNotMatchedType() { - doTest("list | Any"); + doTest("list[Any] | Any"); } // PY-22808 public void testOverloadedNotMatchedGenericType() { - doTest("dict[str, Any] | list"); + doTest("dict[str, Any] | list[Any]"); } public void testGenericClassDefinitionInOtherFile() {