From ea66d40dd7f1eaa20256e6fccaf6d7b853ccc541 Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Sun, 25 Feb 2024 16:36:52 -0800 Subject: [PATCH 1/3] Update empty line documentation (#4239) Reflects status quo following #4043 Fixes #4238 --- docs/the_black_code_style/current_style.md | 39 +++++++++------------- 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/docs/the_black_code_style/current_style.md b/docs/the_black_code_style/current_style.md index 586c79074af..68cd6175e3e 100644 --- a/docs/the_black_code_style/current_style.md +++ b/docs/the_black_code_style/current_style.md @@ -166,44 +166,35 @@ that in-function vertical whitespace should only be used sparingly. _Black_ will allow single empty lines inside functions, and single and double empty lines on module level left by the original editors, except when they're within parenthesized expressions. Since such expressions are always reformatted to fit minimal -space, this whitespace is lost. The other exception is that it will remove any empty -lines immediately following a statement that introduces a new indentation level. +space, this whitespace is lost. ```python # in: -def foo(): +def function( + some_argument: int, - print("All the newlines above me should be deleted!") + other_argument: int = 5, +) -> EmptyLineInParenWillBeDeleted: -if condition: - print("No newline above me!") - - print("There is a newline above me, and that's OK!") - - -class Point: - - x: int - y: int + print("One empty line above me will be kept!") +def this_is_okay_too(): + print("No empty line here") # out: -def foo(): - print("All the newlines above me should be deleted!") - - -if condition: - print("No newline above me!") +def function( + some_argument: int, + other_argument: int = 5, +) -> EmptyLineInParenWillBeDeleted: - print("There is a newline above me, and that's OK!") + print("One empty line above me will be kept!") -class Point: - x: int - y: int +def this_is_okay_too(): + print("No empty line here") ``` It will also insert proper spacing before and after function definitions. It's one line From 899002399a26348198612503ce6ca2fc298551a6 Mon Sep 17 00:00:00 2001 From: Victorien <65306057+Viicos@users.noreply.github.com> Date: Mon, 26 Feb 2024 16:49:39 +0100 Subject: [PATCH 2/3] Remove usage of `pkg_resources` in `docs/conf.py` (#4251) --- docs/conf.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 52a849d06a4..c352f98da82 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -15,10 +15,9 @@ import os import string +from importlib.metadata import version from pathlib import Path -from pkg_resources import get_distribution - CURRENT_DIR = Path(__file__).parent @@ -43,7 +42,7 @@ def make_pypi_svg(version: str) -> None: # Autopopulate version # The version, including alpha/beta/rc tags, but not commit hash and datestamps -release = get_distribution("black").version.split("+")[0] +release = version("black").split("+")[0] # The short X.Y version. version = release for sp in "abcfr": From 0f18001abfe50b078317395e77508b151e271624 Mon Sep 17 00:00:00 2001 From: cobalt <61329810+RedGuy12@users.noreply.github.com> Date: Wed, 28 Feb 2024 10:27:57 -0600 Subject: [PATCH 3/3] chore: Refactor `delimiter_split()` (#4257) Signed-off-by: RedGuy12 <61329810+RedGuy12@users.noreply.github.com> --- src/black/linegen.py | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/black/linegen.py b/src/black/linegen.py index cc8e41dfb20..dea36d5a5d8 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -1135,6 +1135,14 @@ def _get_last_non_comment_leaf(line: Line) -> Optional[int]: return None +def _can_add_trailing_comma(leaf: Leaf, features: Collection[Feature]) -> bool: + if is_vararg(leaf, within={syms.typedargslist}): + return Feature.TRAILING_COMMA_IN_DEF in features + if is_vararg(leaf, within={syms.arglist, syms.argument}): + return Feature.TRAILING_COMMA_IN_CALL in features + return True + + def _safe_add_trailing_comma(safe: bool, delimiter_priority: int, line: Line) -> Line: if ( safe @@ -1156,10 +1164,9 @@ def delimiter_split( If the appropriate Features are given, the split will add trailing commas also in function signatures and calls that contain `*` and `**`. """ - try: - last_leaf = line.leaves[-1] - except IndexError: + if len(line.leaves) == 0: raise CannotSplit("Line empty") from None + last_leaf = line.leaves[-1] bt = line.bracket_tracker try: @@ -1167,9 +1174,11 @@ def delimiter_split( except ValueError: raise CannotSplit("No delimiters found") from None - if delimiter_priority == DOT_PRIORITY: - if bt.delimiter_count_with_priority(delimiter_priority) == 1: - raise CannotSplit("Splitting a single attribute from its owner looks wrong") + if ( + delimiter_priority == DOT_PRIORITY + and bt.delimiter_count_with_priority(delimiter_priority) == 1 + ): + raise CannotSplit("Splitting a single attribute from its owner looks wrong") current_line = Line( mode=line.mode, depth=line.depth, inside_brackets=line.inside_brackets @@ -1198,15 +1207,8 @@ def append_to_line(leaf: Leaf) -> Iterator[Line]: yield from append_to_line(comment_after) lowest_depth = min(lowest_depth, leaf.bracket_depth) - if leaf.bracket_depth == lowest_depth: - if is_vararg(leaf, within={syms.typedargslist}): - trailing_comma_safe = ( - trailing_comma_safe and Feature.TRAILING_COMMA_IN_DEF in features - ) - elif is_vararg(leaf, within={syms.arglist, syms.argument}): - trailing_comma_safe = ( - trailing_comma_safe and Feature.TRAILING_COMMA_IN_CALL in features - ) + if trailing_comma_safe and leaf.bracket_depth == lowest_depth: + trailing_comma_safe = _can_add_trailing_comma(leaf, features) if last_leaf.type == STANDALONE_COMMENT and leaf_idx == last_non_comment_leaf: current_line = _safe_add_trailing_comma( @@ -1220,6 +1222,7 @@ def append_to_line(leaf: Leaf) -> Iterator[Line]: current_line = Line( mode=line.mode, depth=line.depth, inside_brackets=line.inside_brackets ) + if current_line: current_line = _safe_add_trailing_comma( trailing_comma_safe, delimiter_priority, current_line