Skip to content
Merged
4 changes: 3 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@

<!-- Changes that affect Black's stable style -->

- Fix bug where python 3.12 generics syntax split line happens weirdly (#4740)

### Preview style

<!-- Changes that affect Black's preview style -->

- Fix bug where module docstrings would be treated as normal strings if preceeded by
comments (#4764)
comments (#4777)

### Configuration

Comment thread
Exgene marked this conversation as resolved.
Expand Down
1 change: 1 addition & 0 deletions docs/the_black_code_style/future_style.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Currently, the following features are included in the preview style:
normalize file newlines both from and to.
- `fix_module_docstring_detection`: Fix module docstrings being treated as normal
strings if preceeded by comments.
- `fix_type_expansion_split`: Fix type expansions split in generic functions.

(labels/unstable-features)=

Expand Down
5 changes: 4 additions & 1 deletion src/black/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,10 @@ def left_hand_split(
current_leaves = tail_leaves if body_leaves else head_leaves
current_leaves.append(leaf)
if current_leaves is head_leaves:
if leaf.type == leaf_type:
if leaf.type == leaf_type and (
Preview.fix_type_expansion_split
and not (leaf_type == token.LPAR and depth > 0)
Comment thread
Exgene marked this conversation as resolved.
Outdated
):
matching_bracket = leaf
current_leaves = body_leaves
if matching_bracket and tail_leaves:
Expand Down
1 change: 1 addition & 0 deletions src/black/mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ class Preview(Enum):
remove_parens_around_except_types = auto()
normalize_cr_newlines = auto()
fix_module_docstring_detection = auto()
fix_type_expansion_split = auto()


UNSTABLE_FEATURES: set[Preview] = {
Expand Down
3 changes: 2 additions & 1 deletion src/black/resources/black.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@
"wrap_comprehension_in",
"remove_parens_around_except_types",
"normalize_cr_newlines",
"fix_module_docstring_detection"
"fix_module_docstring_detection",
"fix_type_expansion_split"
]
},
"description": "Enable specific features included in the `--unstable` style. Requires `--preview`. No compatibility guarantees are provided on the behavior or existence of any unstable features."
Expand Down
60 changes: 60 additions & 0 deletions tests/data/cases/type_expansion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# flags: --preview

def f1[T: (int, str)](a,): pass

def f2[T: (int, str)](a: int, b,): pass

def g1[T: (int,)](a,): pass

def g2[T: (int, str, bytes)](a,): pass

def g3[T: ((int, str), (bytes,))](a,): pass

def g4[T: (int, (str, bytes))](a,): pass

def g5[T: ((int,),)](a: int, b,): pass

# output

def f1[T: (int, str)](
a,
):
pass


def f2[T: (int, str)](
a: int,
b,
):
pass


def g1[T: (int,)](
a,
):
pass


def g2[T: (int, str, bytes)](
a,
):
pass


def g3[T: ((int, str), (bytes,))](
a,
):
pass


def g4[T: (int, (str, bytes))](
a,
):
pass


def g5[T: ((int,),)](
a: int,
b,
):
pass
1 change: 0 additions & 1 deletion tests/data/cases/type_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ 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