diff --git a/CHANGES.md b/CHANGES.md index 98e4f6506ad..50bf323f1a3 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -16,6 +16,7 @@ - Fix bug where module docstrings would be treated as normal strings if preceeded by comments (#4764) +- Fix bug where python 3.12 generics syntax split line happens weirdly (#4777) ### Configuration diff --git a/docs/the_black_code_style/future_style.md b/docs/the_black_code_style/future_style.md index c1e88c1cba5..f5651c20d4a 100644 --- a/docs/the_black_code_style/future_style.md +++ b/docs/the_black_code_style/future_style.md @@ -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)= diff --git a/src/black/linegen.py b/src/black/linegen.py index 27c2c92d9b2..09197e674e7 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -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 not in mode + or not (leaf_type == token.LPAR and depth > 0) + ): matching_bracket = leaf current_leaves = body_leaves if matching_bracket and tail_leaves: diff --git a/src/black/mode.py b/src/black/mode.py index 9927f73c4a7..79dfed41047 100644 --- a/src/black/mode.py +++ b/src/black/mode.py @@ -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] = { diff --git a/src/black/resources/black.schema.json b/src/black/resources/black.schema.json index 8ea19908570..9e60db33ebf 100644 --- a/src/black/resources/black.schema.json +++ b/src/black/resources/black.schema.json @@ -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." diff --git a/tests/data/cases/type_expansion.py b/tests/data/cases/type_expansion.py new file mode 100644 index 00000000000..6cd7a0b7736 --- /dev/null +++ b/tests/data/cases/type_expansion.py @@ -0,0 +1,60 @@ +# flags: --preview --minimum-version=3.12 + +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