Skip to content

Commit 8bf0454

Browse files
Consistently add trailing comma on typed parameters (psf#4164)
Signed-off-by: RedGuy12 <[email protected]> Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 1607e9a commit 8bf0454

File tree

7 files changed

+40
-3
lines changed

7 files changed

+40
-3
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

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

17+
- Consistently add trailing comma on typed parameters (#4164)
18+
1719
### Configuration
1820

1921
<!-- Changes to how Black can be configured -->

docs/the_black_code_style/future_style.md

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Currently, the following features are included in the preview style:
2626
brackets ([see below](labels/hug-parens))
2727
- `no_normalize_fmt_skip_whitespace`: whitespace before `# fmt: skip` comments is no
2828
longer normalized
29+
- `typed_params_trailing_comma`: consistently add trailing commas to typed function
30+
parameters
2931

3032
(labels/unstable-features)=
3133

src/black/files.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def parse_pyproject_toml(path_config: str) -> Dict[str, Any]:
131131

132132

133133
def infer_target_version(
134-
pyproject_toml: Dict[str, Any]
134+
pyproject_toml: Dict[str, Any],
135135
) -> Optional[List[TargetVersion]]:
136136
"""Infer Black's target version from the project metadata in pyproject.toml.
137137

src/black/linegen.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
is_one_sequence_between,
4949
is_one_tuple,
5050
is_parent_function_or_class,
51+
is_part_of_annotation,
5152
is_rpar_token,
5253
is_stub_body,
5354
is_stub_suite,
@@ -1041,7 +1042,14 @@ def bracket_split_build_line(
10411042
no_commas = (
10421043
original.is_def
10431044
and opening_bracket.value == "("
1044-
and not any(leaf.type == token.COMMA for leaf in leaves)
1045+
and not any(
1046+
leaf.type == token.COMMA
1047+
and (
1048+
Preview.typed_params_trailing_comma not in original.mode
1049+
or not is_part_of_annotation(leaf)
1050+
)
1051+
for leaf in leaves
1052+
)
10451053
# In particular, don't add one within a parenthesized return annotation.
10461054
# Unfortunately the indicator we're in a return annotation (RARROW) may
10471055
# be defined directly in the parent node, the parent of the parent ...

src/black/mode.py

+1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ class Preview(Enum):
176176
no_normalize_fmt_skip_whitespace = auto()
177177
wrap_long_dict_values_in_parens = auto()
178178
multiline_string_handling = auto()
179+
typed_params_trailing_comma = auto()
179180

180181

181182
UNSTABLE_FEATURES: Set[Preview] = {

src/blib2to3/pgen2/parse.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def lam_sub(grammar: Grammar, node: RawNode) -> NL:
5050

5151

5252
def stack_copy(
53-
stack: List[Tuple[DFAS, int, RawNode]]
53+
stack: List[Tuple[DFAS, int, RawNode]],
5454
) -> List[Tuple[DFAS, int, RawNode]]:
5555
"""Nodeless stack copy."""
5656
return [(dfa, label, DUMMY_NODE) for dfa, label, _ in stack]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# flags: --preview
2+
def long_function_name_goes_here(
3+
x: Callable[List[int]]
4+
) -> Union[List[int], float, str, bytes, Tuple[int]]:
5+
pass
6+
7+
8+
def long_function_name_goes_here(
9+
x: Callable[[str, Any], int]
10+
) -> Union[List[int], float, str, bytes, Tuple[int]]:
11+
pass
12+
13+
14+
# output
15+
def long_function_name_goes_here(
16+
x: Callable[List[int]],
17+
) -> Union[List[int], float, str, bytes, Tuple[int]]:
18+
pass
19+
20+
21+
def long_function_name_goes_here(
22+
x: Callable[[str, Any], int],
23+
) -> Union[List[int], float, str, bytes, Tuple[int]]:
24+
pass

0 commit comments

Comments
 (0)