Consistently add trailing comma on typed parameters (#4164)

Signed-off-by: RedGuy12 <61329810+RedGuy12@users.noreply.github.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
cobalt 2024-01-27 15:55:22 -06:00 committed by GitHub
parent 1607e9ab20
commit 8bf04549ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 40 additions and 3 deletions

View File

@ -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 -->

View File

@ -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)=

View File

@ -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.

View File

@ -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,
@ -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 ...

View File

@ -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] = {

View File

@ -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]

View File

@ -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