Skip to content

Commit 9a87bab

Browse files
Fix type params tuple error (#4684)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parent b085473 commit 9a87bab

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- Fix crash on `await ...` (where `...` is a literal `Ellipsis`) (#4676)
2222
- Remove support for pre-python 3.7 `await/async` as soft keywords/variable names
2323
(#4676)
24+
- Fix crash on parenthesized expression inside a type parameter bound (#4684)
2425

2526
### Preview style
2627

src/black/linegen.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,12 +787,26 @@ def left_hand_split(
787787
head_leaves: list[Leaf] = []
788788
current_leaves = head_leaves
789789
matching_bracket: Optional[Leaf] = None
790-
for leaf in line.leaves:
790+
depth = 0
791+
for index, leaf in enumerate(line.leaves):
792+
if index == 2 and leaf.type == token.LSQB:
793+
# A [ at index 2 means this is a type param, so start
794+
# tracking the depth
795+
depth += 1
796+
elif depth > 0:
797+
if leaf.type == token.LSQB:
798+
depth += 1
799+
elif leaf.type == token.RSQB:
800+
depth -= 1
791801
if (
792802
current_leaves is body_leaves
793803
and leaf.type in CLOSING_BRACKETS
794804
and leaf.opening_bracket is matching_bracket
795805
and isinstance(matching_bracket, Leaf)
806+
# If the code is still on LPAR and we are inside a type
807+
# param, ignore the match since this is searching
808+
# for the function arguments
809+
and not (leaf_type == token.LPAR and depth > 0)
796810
):
797811
ensure_visible(leaf)
798812
ensure_visible(matching_bracket)

tests/data/cases/type_params.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ def magic[Trailing, Comma,](): pass
1515

1616
def weird_syntax[T: lambda: 42, U: a or b](): pass
1717

18+
def name_3[name_0: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa if aaaaaaaaaaa else name_3](): pass
19+
1820
# output
1921

2022

@@ -62,3 +64,9 @@ def magic[
6264

6365
def weird_syntax[T: lambda: 42, U: a or b]():
6466
pass
67+
68+
69+
def name_3[
70+
name_0: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa if aaaaaaaaaaa else name_3
71+
]():
72+
pass

0 commit comments

Comments
 (0)