Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Consistently add trailing comma on typed parameters #4164

Merged
merged 7 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ release:
- Fix crash when using a walrus in a dictionary (#4155)
- Fix unnecessary parentheses when wrapping long dicts (#4135)
- Stop normalizing spaces before `# fmt: skip` comments (#4146)
- Consistently add trailing comma on typed parameters (#4164)
JelleZijlstra marked this conversation as resolved.
Show resolved Hide resolved

### Configuration

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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use is_part_of_annotation to simplify some of the logic in the next check too? Lines 1092-1099?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried, but it ended up adding a lot of unrelated changes to this PR since is_part_of_annotation currently checks for both return and parameter annotations. I can do that refactor in a future PR.

)
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