From 58d00407db93105d81f9e33c5ef3cb4fd8923b15 Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Tue, 29 Oct 2024 12:02:54 +0200 Subject: [PATCH] PY-77060 Remove spaces after * and ** in new-style PEP 695 type parameters GitOrigin-RevId: cd38f49d0f28894e4f33dbd2fa331eaf895fd70d --- .../jetbrains/python/PythonCommonFormatterTest.java | 5 +++++ .../formatter/PythonFormattingModelBuilder.java | 1 + .../formatter/spaceAfterStarInTypeParameterList.py | 12 ++++++++++++ .../spaceAfterStarInTypeParameterList_after.py | 12 ++++++++++++ 4 files changed, 30 insertions(+) create mode 100644 python/testData/formatter/spaceAfterStarInTypeParameterList.py create mode 100644 python/testData/formatter/spaceAfterStarInTypeParameterList_after.py diff --git a/python/python-common-tests/com/jetbrains/python/PythonCommonFormatterTest.java b/python/python-common-tests/com/jetbrains/python/PythonCommonFormatterTest.java index 3488b2800771..7a4b25683506 100644 --- a/python/python-common-tests/com/jetbrains/python/PythonCommonFormatterTest.java +++ b/python/python-common-tests/com/jetbrains/python/PythonCommonFormatterTest.java @@ -1361,4 +1361,9 @@ public abstract class PythonCommonFormatterTest extends PythonCommonTestCase { public void testAlignmentInMultilineTypeParameterListInClassDefinition() { doTest(); } + + // PY-77060 + public void testSpaceAfterStarInTypeParameterList() { + doTest(); + } } diff --git a/python/python-syntax-core/src/com/jetbrains/python/formatter/PythonFormattingModelBuilder.java b/python/python-syntax-core/src/com/jetbrains/python/formatter/PythonFormattingModelBuilder.java index aa0bfd865ddf..1d5a1b1b2824 100644 --- a/python/python-syntax-core/src/com/jetbrains/python/formatter/PythonFormattingModelBuilder.java +++ b/python/python-syntax-core/src/com/jetbrains/python/formatter/PythonFormattingModelBuilder.java @@ -137,6 +137,7 @@ public class PythonFormattingModelBuilder implements FormattingModelBuilder, Cus .aroundInside(ADDITIVE_OPERATIONS, BINARY_EXPRESSION).spaceIf(commonSettings.SPACE_AROUND_ADDITIVE_OPERATORS) .aroundInside(STAR_OPERATORS, STAR_PARAMETERS).none() .aroundInside(STAR_OPERATORS, STAR_PATTERNS).none() + .aroundInside(STAR_OPERATORS, TYPE_PARAMETER).none() .between(EXCEPT_KEYWORD, MULT).none() .between(PERC, TokenSet.create(PERC, IDENTIFIER, EXPRESSION_STATEMENT)).none() .around(MULTIPLICATIVE_OPERATIONS).spaceIf(commonSettings.SPACE_AROUND_MULTIPLICATIVE_OPERATORS) diff --git a/python/testData/formatter/spaceAfterStarInTypeParameterList.py b/python/testData/formatter/spaceAfterStarInTypeParameterList.py new file mode 100644 index 000000000000..051d1d7dd1e8 --- /dev/null +++ b/python/testData/formatter/spaceAfterStarInTypeParameterList.py @@ -0,0 +1,12 @@ +from typing import Callable + +type A[T, * Ts, ** P] = Callable[P, tuple[*Ts, T]] + + +def f[T, * Ts, ** P]() -> Callable[P, tuple[*Ts, T]]: + ... + + +class C[T, * Ts, ** P]: + def m(self) -> Callable[P, tuple[*Ts, T]]: + ... \ No newline at end of file diff --git a/python/testData/formatter/spaceAfterStarInTypeParameterList_after.py b/python/testData/formatter/spaceAfterStarInTypeParameterList_after.py new file mode 100644 index 000000000000..f0fa89280566 --- /dev/null +++ b/python/testData/formatter/spaceAfterStarInTypeParameterList_after.py @@ -0,0 +1,12 @@ +from typing import Callable + +type A[T, *Ts, **P] = Callable[P, tuple[*Ts, T]] + + +def f[T, *Ts, **P]() -> Callable[P, tuple[*Ts, T]]: + ... + + +class C[T, *Ts, **P]: + def m(self) -> Callable[P, tuple[*Ts, T]]: + ...