Skip to content

Commit 8203030

Browse files
ExgeneMeGaGiGaGon
andauthored
fix(3.12_generics_syntax): Fixed formatting of 3.12 generics syntax (#4777)
* fix(3.12_generics_syntax): Fixed formatting of 3.12 generics syntax * chore: add test cases and update CHANGELOG * revert: Formatted test code files, Im dumb * chore: update with multiple tests * fix: Use PR number instead of issue * chore: move fix to preview with proper documentation for stability * chore: update linegen to check against mode Co-authored-by: GiGaGon <[email protected]> * chore: update changes.md to move it to preview * fix: minimum python version to 3.12 for tests * Fix changelog entry * Update type_params.py to remove unnecessary edit --------- Co-authored-by: GiGaGon <[email protected]>
1 parent 0b8f222 commit 8203030

File tree

6 files changed

+69
-2
lines changed

6 files changed

+69
-2
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
- Fix bug where module docstrings would be treated as normal strings if preceeded by
1818
comments (#4764)
19+
- Fix bug where python 3.12 generics syntax split line happens weirdly (#4777)
1920

2021
### Configuration
2122

docs/the_black_code_style/future_style.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Currently, the following features are included in the preview style:
3737
normalize file newlines both from and to.
3838
- `fix_module_docstring_detection`: Fix module docstrings being treated as normal
3939
strings if preceeded by comments.
40+
- `fix_type_expansion_split`: Fix type expansions split in generic functions.
4041

4142
(labels/unstable-features)=
4243

src/black/linegen.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,10 @@ def left_hand_split(
834834
current_leaves = tail_leaves if body_leaves else head_leaves
835835
current_leaves.append(leaf)
836836
if current_leaves is head_leaves:
837-
if leaf.type == leaf_type:
837+
if leaf.type == leaf_type and (
838+
Preview.fix_type_expansion_split not in mode
839+
or not (leaf_type == token.LPAR and depth > 0)
840+
):
838841
matching_bracket = leaf
839842
current_leaves = body_leaves
840843
if matching_bracket and tail_leaves:

src/black/mode.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ class Preview(Enum):
237237
remove_parens_around_except_types = auto()
238238
normalize_cr_newlines = auto()
239239
fix_module_docstring_detection = auto()
240+
fix_type_expansion_split = auto()
240241

241242

242243
UNSTABLE_FEATURES: set[Preview] = {

src/black/resources/black.schema.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@
8989
"wrap_comprehension_in",
9090
"remove_parens_around_except_types",
9191
"normalize_cr_newlines",
92-
"fix_module_docstring_detection"
92+
"fix_module_docstring_detection",
93+
"fix_type_expansion_split"
9394
]
9495
},
9596
"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."

tests/data/cases/type_expansion.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# flags: --preview --minimum-version=3.12
2+
3+
def f1[T: (int, str)](a,): pass
4+
5+
def f2[T: (int, str)](a: int, b,): pass
6+
7+
def g1[T: (int,)](a,): pass
8+
9+
def g2[T: (int, str, bytes)](a,): pass
10+
11+
def g3[T: ((int, str), (bytes,))](a,): pass
12+
13+
def g4[T: (int, (str, bytes))](a,): pass
14+
15+
def g5[T: ((int,),)](a: int, b,): pass
16+
17+
# output
18+
19+
def f1[T: (int, str)](
20+
a,
21+
):
22+
pass
23+
24+
25+
def f2[T: (int, str)](
26+
a: int,
27+
b,
28+
):
29+
pass
30+
31+
32+
def g1[T: (int,)](
33+
a,
34+
):
35+
pass
36+
37+
38+
def g2[T: (int, str, bytes)](
39+
a,
40+
):
41+
pass
42+
43+
44+
def g3[T: ((int, str), (bytes,))](
45+
a,
46+
):
47+
pass
48+
49+
50+
def g4[T: (int, (str, bytes))](
51+
a,
52+
):
53+
pass
54+
55+
56+
def g5[T: ((int,),)](
57+
a: int,
58+
b,
59+
):
60+
pass

0 commit comments

Comments
 (0)