From 0c6b684c676f7f3d0e6f2edc29726668ddb169f0 Mon Sep 17 00:00:00 2001 From: GiGaGon <107241144+MeGaGiGaGon@users.noreply.github.com> Date: Wed, 4 Jun 2025 16:54:54 -0700 Subject: [PATCH 1/5] Update linegen.py --- src/black/linegen.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/black/linegen.py b/src/black/linegen.py index fa574ca215e..df5dfb8092a 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -787,12 +787,26 @@ def left_hand_split( head_leaves: list[Leaf] = [] current_leaves = head_leaves matching_bracket: Optional[Leaf] = None - for leaf in line.leaves: + depth = 0 + for index, leaf in enumerate(line.leaves): + if index == 2 and leaf.type == token.LSQB: + # A [ at index 2 means this is a type annotation, so start + # tracking the depth + depth += 1 + elif depth > 0: + if leaf.type == token.LSQB: + depth += 1 + elif leaf.type == token.RSQB: + depth -= 1 if ( current_leaves is body_leaves and leaf.type in CLOSING_BRACKETS and leaf.opening_bracket is matching_bracket and isinstance(matching_bracket, Leaf) + # If the code is still on LPAR and we are inside a type + # annotation, ignore the match since this is searching + # for the function arguments + and not (leaf_type == token.LPAR and depth > 0) ): ensure_visible(leaf) ensure_visible(matching_bracket) From f9a31dc85253cdd3f567de3e9624e21d6b3eca4f Mon Sep 17 00:00:00 2001 From: GiGaGon <107241144+MeGaGiGaGon@users.noreply.github.com> Date: Wed, 4 Jun 2025 16:57:02 -0700 Subject: [PATCH 2/5] Update CHANGES.md --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index cf415f15fc3..398f77acbbf 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -16,6 +16,7 @@ - Handle `# fmt: skip` followed by a comment at the end of file (#4635) - Fix crash when a tuple appears in the `as` clause of a `with` statement (#4634) - Fix crash when tuple is used as a context manager inside a `with` statement (#4646) +- Fix crash when parenthesis are inside a type annotation (#4684) ### Preview style From a7d8f2ec900e0e800fa0f81b639c5759eeb5247e Mon Sep 17 00:00:00 2001 From: GiGaGon <107241144+MeGaGiGaGon@users.noreply.github.com> Date: Wed, 4 Jun 2025 17:02:12 -0700 Subject: [PATCH 3/5] Update type_params.py --- tests/data/cases/type_params.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/data/cases/type_params.py b/tests/data/cases/type_params.py index f8fc3855741..124292d6d54 100644 --- a/tests/data/cases/type_params.py +++ b/tests/data/cases/type_params.py @@ -15,6 +15,8 @@ def magic[Trailing, Comma,](): pass def weird_syntax[T: lambda: 42, U: a or b](): pass +def name_3[name_0: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa if aaaaaaaaaaa else name_3](): pass + # output @@ -62,3 +64,9 @@ def magic[ def weird_syntax[T: lambda: 42, U: a or b](): pass + + +def name_3[ + name_0: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa if aaaaaaaaaaa else name_3 +](): + pass From 57a059894133a52ce607745d7efef417aac9d2bc Mon Sep 17 00:00:00 2001 From: GiGaGon <107241144+MeGaGiGaGon@users.noreply.github.com> Date: Wed, 4 Jun 2025 17:04:37 -0700 Subject: [PATCH 4/5] Update linegen.py --- src/black/linegen.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/black/linegen.py b/src/black/linegen.py index df5dfb8092a..032ab687835 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -790,7 +790,7 @@ def left_hand_split( depth = 0 for index, leaf in enumerate(line.leaves): if index == 2 and leaf.type == token.LSQB: - # A [ at index 2 means this is a type annotation, so start + # A [ at index 2 means this is a type param, so start # tracking the depth depth += 1 elif depth > 0: @@ -804,7 +804,7 @@ def left_hand_split( and leaf.opening_bracket is matching_bracket and isinstance(matching_bracket, Leaf) # If the code is still on LPAR and we are inside a type - # annotation, ignore the match since this is searching + # param, ignore the match since this is searching # for the function arguments and not (leaf_type == token.LPAR and depth > 0) ): From f37919de4659e5d3442da5fa4a61e5f0f8e98cf0 Mon Sep 17 00:00:00 2001 From: GiGaGon <107241144+MeGaGiGaGon@users.noreply.github.com> Date: Thu, 10 Jul 2025 20:25:10 -0700 Subject: [PATCH 5/5] Update CHANGES.md Co-authored-by: Jelle Zijlstra --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 398f77acbbf..795f1af8a67 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -16,7 +16,7 @@ - Handle `# fmt: skip` followed by a comment at the end of file (#4635) - Fix crash when a tuple appears in the `as` clause of a `with` statement (#4634) - Fix crash when tuple is used as a context manager inside a `with` statement (#4646) -- Fix crash when parenthesis are inside a type annotation (#4684) +- Fix crash on parenthesized expression inside a type parameter bound (#4684) ### Preview style