Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 15 additions & 1 deletion src/black/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 param, 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
# param, 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)
Expand Down
8 changes: 8 additions & 0 deletions tests/data/cases/type_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Loading