Skip to content

Commit

Permalink
Consistently add trailing comma on typed parameters (#4164)
Browse files Browse the repository at this point in the history
Signed-off-by: RedGuy12 <61329810+RedGuy12@users.noreply.github.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
  • Loading branch information
RedGuy12 and JelleZijlstra committed Jan 27, 2024
1 parent 1607e9a commit 8bf0454
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

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

- Consistently add trailing comma on typed parameters (#4164)

### Configuration

<!-- Changes to how Black can be configured -->
Expand Down
2 changes: 2 additions & 0 deletions docs/the_black_code_style/future_style.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Currently, the following features are included in the preview style:
brackets ([see below](labels/hug-parens))
- `no_normalize_fmt_skip_whitespace`: whitespace before `# fmt: skip` comments is no
longer normalized
- `typed_params_trailing_comma`: consistently add trailing commas to typed function
parameters

(labels/unstable-features)=

Expand Down
2 changes: 1 addition & 1 deletion src/black/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def parse_pyproject_toml(path_config: str) -> Dict[str, Any]:


def infer_target_version(
pyproject_toml: Dict[str, Any]
pyproject_toml: Dict[str, Any],
) -> Optional[List[TargetVersion]]:
"""Infer Black's target version from the project metadata in pyproject.toml.
Expand Down
10 changes: 9 additions & 1 deletion src/black/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
is_one_sequence_between,
is_one_tuple,
is_parent_function_or_class,
is_part_of_annotation,
is_rpar_token,
is_stub_body,
is_stub_suite,
Expand Down Expand Up @@ -1041,7 +1042,14 @@ def bracket_split_build_line(
no_commas = (
original.is_def
and opening_bracket.value == "("
and not any(leaf.type == token.COMMA for leaf in leaves)
and not any(
leaf.type == token.COMMA
and (
Preview.typed_params_trailing_comma not in original.mode
or not is_part_of_annotation(leaf)
)
for leaf in leaves
)
# In particular, don't add one within a parenthesized return annotation.
# Unfortunately the indicator we're in a return annotation (RARROW) may
# be defined directly in the parent node, the parent of the parent ...
Expand Down
1 change: 1 addition & 0 deletions src/black/mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ class Preview(Enum):
no_normalize_fmt_skip_whitespace = auto()
wrap_long_dict_values_in_parens = auto()
multiline_string_handling = auto()
typed_params_trailing_comma = auto()


UNSTABLE_FEATURES: Set[Preview] = {
Expand Down
2 changes: 1 addition & 1 deletion src/blib2to3/pgen2/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def lam_sub(grammar: Grammar, node: RawNode) -> NL:


def stack_copy(
stack: List[Tuple[DFAS, int, RawNode]]
stack: List[Tuple[DFAS, int, RawNode]],
) -> List[Tuple[DFAS, int, RawNode]]:
"""Nodeless stack copy."""
return [(dfa, label, DUMMY_NODE) for dfa, label, _ in stack]
Expand Down
24 changes: 24 additions & 0 deletions tests/data/cases/typed_params_trailing_comma.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# flags: --preview
def long_function_name_goes_here(
x: Callable[List[int]]
) -> Union[List[int], float, str, bytes, Tuple[int]]:
pass


def long_function_name_goes_here(
x: Callable[[str, Any], int]
) -> Union[List[int], float, str, bytes, Tuple[int]]:
pass


# output
def long_function_name_goes_here(
x: Callable[List[int]],
) -> Union[List[int], float, str, bytes, Tuple[int]]:
pass


def long_function_name_goes_here(
x: Callable[[str, Any], int],
) -> Union[List[int], float, str, bytes, Tuple[int]]:
pass

0 comments on commit 8bf0454

Please sign in to comment.