Skip to content

Commit d8f44e6

Browse files
Respect the magic trailing comma in starred type parameter lists
A magic trailing comma exploded a PEP 695 type parameter list of plain type vars, but was silently ignored once the list contained a *TypeVarTuple or **ParamSpec, collapsing the params back onto one line. is_split_before_delimiter/is_split_after_delimiter skip a star that is_vararg() recognises, so it is not mistaken for a MATH_OPERATORS delimiter. is_vararg() checks the star's parent against VARARGS_PARENTS, which listed the argument and lambda nodes but not the PEP 695 typevartuple and paramspec ones. The stars in a type parameter list were therefore scored as multiplication and power operators, and that delimiter beat the comma when the line was split. Add both nodes to VARARGS_PARENTS. This affects def, async def, class and type aliases alike. Closes #5243
1 parent 51abf53 commit d8f44e6

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

src/black/nodes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373
syms.trailer, # single argument to call
7474
syms.typedargslist,
7575
syms.varargslist, # lambdas
76+
syms.typevartuple, # star in a PEP 695 type parameter list
77+
syms.paramspec, # double star in a PEP 695 type parameter list
7678
}
7779
UNPACKING_PARENTS: Final = {
7880
syms.atom, # single element of a list or set literal

tests/data/cases/type_params.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ def it_gets_worse[WhatIsTheLongestTypeVarNameYouCanThinkOfEnoughToMakeBlackSplit
1313

1414
def magic[Trailing, Comma,](): pass
1515

16+
def magic_starred[T, *Ts, **P,](): pass
17+
18+
class MagicStarred[T, *Ts, **P,]: pass
19+
20+
type MagicStarredAlias[T, *Ts, **P,] = int
21+
1622
def weird_syntax[T: lambda: 42, U: a or b](): pass
1723

1824
def name_3[name_0: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa if aaaaaaaaaaa else name_3](): pass
@@ -62,6 +68,29 @@ def magic[
6268
pass
6369

6470

71+
def magic_starred[
72+
T,
73+
*Ts,
74+
**P,
75+
]():
76+
pass
77+
78+
79+
class MagicStarred[
80+
T,
81+
*Ts,
82+
**P,
83+
]:
84+
pass
85+
86+
87+
type MagicStarredAlias[
88+
T,
89+
*Ts,
90+
**P,
91+
] = int
92+
93+
6594
def weird_syntax[T: lambda: 42, U: a or b]():
6695
pass
6796

0 commit comments

Comments
 (0)