From 5dc70ed88daa1b06eaa34970741a673782d59221 Mon Sep 17 00:00:00 2001 From: jpy-git Date: Tue, 5 Apr 2022 22:31:59 +0100 Subject: [PATCH 1/6] Update test --- tests/data/string_quotes.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/data/string_quotes.py b/tests/data/string_quotes.py index 3384241f4ad..ef1c0b626de 100644 --- a/tests/data/string_quotes.py +++ b/tests/data/string_quotes.py @@ -59,6 +59,7 @@ # output """""" + "'" '"' "'" From 423a66ddc96c453dd915a3a9ffdac5c1c2fff591 Mon Sep 17 00:00:00 2001 From: jpy-git Date: Tue, 5 Apr 2022 22:38:11 +0100 Subject: [PATCH 2/6] Function docstrings --- src/black/lines.py | 11 +++++++++++ tests/data/function_docstring.py | 30 ++++++++++++++++++++++++++++++ tests/test_format.py | 1 + 3 files changed, 42 insertions(+) create mode 100644 tests/data/function_docstring.py diff --git a/src/black/lines.py b/src/black/lines.py index f43b8281000..2f8f313ad97 100644 --- a/src/black/lines.py +++ b/src/black/lines.py @@ -522,8 +522,19 @@ def _maybe_empty_lines(self, current_line: Line) -> int: and self.previous_lines[-1].is_triple_quoted_string and current_line.depth == self.previous_lines[-1].depth ): + # Single newline after class docstring. return 1 + if ( + self.preview + and len(self.previous_lines) > 1 + and self.previous_lines[-2].is_def + and self.previous_lines[-1].is_triple_quoted_string + and current_line.depth == self.previous_lines[-1].depth + ): + # No newline after function docstring. + return 0 + return before def _maybe_empty_lines_for_class_or_def( diff --git a/tests/data/function_docstring.py b/tests/data/function_docstring.py new file mode 100644 index 00000000000..c3ddfb5f2f7 --- /dev/null +++ b/tests/data/function_docstring.py @@ -0,0 +1,30 @@ +def foo(): + """hello world.""" + + + + print("hello world!") + + +def bar(x: int, y: int) -> int: + """hello world. + + blah blah blah... + """ + + + + return x + y + +# output +def foo(): + """hello world.""" + print("hello world!") + + +def bar(x: int, y: int) -> int: + """hello world. + + blah blah blah... + """ + return x + y diff --git a/tests/test_format.py b/tests/test_format.py index 422e94aa62d..6595c816f10 100644 --- a/tests/test_format.py +++ b/tests/test_format.py @@ -85,6 +85,7 @@ "one_element_subscript", "module_docstring_1", "module_docstring_2", + "function_docstring", ] SOURCES: List[str] = [ From bc17dec681cb91586632b9c1dfa3e0e750cc0529 Mon Sep 17 00:00:00 2001 From: jpy-git Date: Tue, 5 Apr 2022 23:51:43 +0100 Subject: [PATCH 3/6] Format files --- fuzz.py | 1 - scripts/check_pre_commit_rev_in_example.py | 1 - scripts/check_version_in_basics_example.py | 1 - scripts/diff_shades_gha_helper.py | 1 - 4 files changed, 4 deletions(-) diff --git a/fuzz.py b/fuzz.py index f5f655ea279..2fcde30cd94 100644 --- a/fuzz.py +++ b/fuzz.py @@ -4,7 +4,6 @@ generation. You can run this file with `python`, `pytest`, or (soon) a coverage-guided fuzzer I'm working on. """ - import re import hypothesmith diff --git a/scripts/check_pre_commit_rev_in_example.py b/scripts/check_pre_commit_rev_in_example.py index 9560b3b8401..132384c84b6 100644 --- a/scripts/check_pre_commit_rev_in_example.py +++ b/scripts/check_pre_commit_rev_in_example.py @@ -8,7 +8,6 @@ technical and some pragmatic). Encouraging bad practice is also just not ideal. xref: https://github.com/psf/black/issues/420 """ - import os import sys diff --git a/scripts/check_version_in_basics_example.py b/scripts/check_version_in_basics_example.py index c62780d97ab..733bfcce9a4 100644 --- a/scripts/check_version_in_basics_example.py +++ b/scripts/check_version_in_basics_example.py @@ -3,7 +3,6 @@ the latest version of Black. This saves us from forgetting to update that during the release process. """ - import os import sys diff --git a/scripts/diff_shades_gha_helper.py b/scripts/diff_shades_gha_helper.py index b5fea5a817d..4b8ef27418e 100644 --- a/scripts/diff_shades_gha_helper.py +++ b/scripts/diff_shades_gha_helper.py @@ -13,7 +13,6 @@ https://black.readthedocs.io/en/latest/contributing/gauging_changes.html#diff-shades """ - import json import os import platform From 8992d86ad73d615dd2837394364237dd75e1fbed Mon Sep 17 00:00:00 2001 From: jpy-git Date: Wed, 6 Apr 2022 00:39:24 +0100 Subject: [PATCH 4/6] Add change log entry --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 244c0182b88..541cdb14bd0 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -16,6 +16,7 @@ - Remove unnecessary parentheses from `with` statements (#2926) - Standardise newlines after module-level docstrings (#2996) +- Remove newlines after function docstrings (#9999) ### _Blackd_ From ad0ddba13589693792a2e8934cdb8d6a9f14c4f8 Mon Sep 17 00:00:00 2001 From: jpy-git Date: Wed, 6 Apr 2022 00:42:30 +0100 Subject: [PATCH 5/6] Format files --- src/black/linegen.py | 2 -- src/black/parsing.py | 1 - src/black/trans.py | 1 - 3 files changed, 4 deletions(-) diff --git a/src/black/linegen.py b/src/black/linegen.py index 2cf9cf3130a..0b927537b0a 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -999,7 +999,6 @@ def maybe_make_parens_invisible_in_atom( def should_split_line(line: Line, opening_bracket: Leaf) -> bool: """Should `line` be immediately split with `delimiter_split()` after RHS?""" - if not (opening_bracket.parent and opening_bracket.value in "[{("): return False @@ -1034,7 +1033,6 @@ def generate_trailers_to_omit(line: Line, line_length: int) -> Iterator[Set[Leaf set is empty, unless the line should explode, in which case bracket pairs until the one that needs to explode are omitted. """ - omit: Set[LeafID] = set() if not line.magic_trailing_comma: yield omit diff --git a/src/black/parsing.py b/src/black/parsing.py index 12726567948..20b82a8c589 100644 --- a/src/black/parsing.py +++ b/src/black/parsing.py @@ -193,7 +193,6 @@ def _normalize(lineend: str, value: str) -> str: def stringify_ast(node: Union[ast.AST, ast3.AST], depth: int = 0) -> Iterator[str]: """Simple visitor generating strings to compare ASTs by content.""" - node = fixup_ast_constants(node) yield f"{' ' * depth}{node.__class__.__name__}(" diff --git a/src/black/trans.py b/src/black/trans.py index 01aa80eaaf8..7966216250e 100644 --- a/src/black/trans.py +++ b/src/black/trans.py @@ -73,7 +73,6 @@ def TErr(err_msg: str) -> Err[CannotTransform]: def hug_power_op(line: Line, features: Collection[Feature]) -> Iterator[Line]: """A transformer which normalizes spacing around power operators.""" - # Performance optimization to avoid unnecessary Leaf clones and other ops. for leaf in line.leaves: if leaf.type == token.DOUBLESTAR: From faf6500b3aab0c887ef57d4f1ff8825836d57e15 Mon Sep 17 00:00:00 2001 From: jpy-git Date: Wed, 6 Apr 2022 00:49:08 +0100 Subject: [PATCH 6/6] Use valid pr number in changelog --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 541cdb14bd0..cfe02a477d3 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -16,7 +16,7 @@ - Remove unnecessary parentheses from `with` statements (#2926) - Standardise newlines after module-level docstrings (#2996) -- Remove newlines after function docstrings (#9999) +- Remove newlines after function docstrings (#1) ### _Blackd_