PY-31689 Do not wrap tuples and list literals inside subscription expressions

GitOrigin-RevId: 9878660c10bab6bda36aa5f2bcd4c96f416dddb8
This commit is contained in:
Daniil Kalinin
2025-02-13 10:45:38 +00:00
committed by intellij-monorepo-bot
parent 5b0b94a8dd
commit fddb487bf8
4 changed files with 17 additions and 2 deletions
@@ -1570,4 +1570,11 @@ public abstract class PythonCommonFormatterTest extends PythonCommonTestCase {
getCodeStyleSettings().CALL_PARAMETERS_WRAP = CommonCodeStyleSettings.WRAP_ALWAYS;
doTest();
}
// PY-31689
public void testDoNotWrapListsAndTuplesInsideSubscriptionExpressions() {
getPythonCodeStyleSettings().TUPLE_WRAPPING = CommonCodeStyleSettings.WRAP_ALWAYS;
getPythonCodeStyleSettings().LIST_WRAPPING = CommonCodeStyleSettings.WRAP_ALWAYS;
doTest();
}
}
@@ -513,13 +513,13 @@ public class PyBlock implements ASTBlock {
if (childType != PyTokenTypes.COMMA && childType != PyTokenTypes.END_OF_LINE_COMMENT) {
// (...)
if (childType != PyTokenTypes.LPAR && childType != PyTokenTypes.RPAR) {
if (parentType == PyElementTypes.TUPLE_EXPRESSION) return myTupleWrapping;
if (parentType == PyElementTypes.PARAMETER_LIST) return myParameterListWrapping;
if (parentType == PyElementTypes.ARGUMENT_LIST) return myArgumentListWrapping;
if (parentType == PyElementTypes.TUPLE_EXPRESSION && !isInsideSubscriptionExpression(child)) return myTupleWrapping;
}
// [...]
if (childType != PyTokenTypes.LBRACKET && childType != PyTokenTypes.RBRACKET) {
if (parentType == PyElementTypes.LIST_LITERAL_EXPRESSION) return myListWrapping;
if (parentType == PyElementTypes.LIST_LITERAL_EXPRESSION && !isInsideSubscriptionExpression(child)) return myListWrapping;
}
// {...}
if (childType != PyTokenTypes.LBRACE && childType != PyTokenTypes.RBRACE) {
@@ -532,6 +532,10 @@ public class PyBlock implements ASTBlock {
return childWrap;
}
private static boolean isInsideSubscriptionExpression(ASTNode child) {
return PsiTreeUtil.getParentOfType(child.getPsi(), PyAstSubscriptionExpression.class) != null;
}
private static boolean isInsideWithStatementParentheses(@NotNull ASTNode withStatement, @NotNull ASTNode node) {
ASTNode openingParenthesis = withStatement.findChildByType(PyTokenTypes.LPAR);
if (openingParenthesis == null) {
@@ -0,0 +1,2 @@
Callable[[str, int, Iterator[str]], bool]
ParamSpec[(str, int, bool, Iterator[str])]
@@ -0,0 +1,2 @@
Callable[[str, int, Iterator[str]], bool]
ParamSpec[(str, int, bool, Iterator[str])]