From f23816813e71e4999b930b50b684daccbf72733a Mon Sep 17 00:00:00 2001 From: jsh9 <25124332+jsh9@users.noreply.github.com> Date: Sun, 21 May 2023 02:43:01 -0700 Subject: [PATCH] Squashed commit of the following: commit eedfc3832290b3a32825b3c0f2dfa3f3d7ee9d1c Author: Jason R. Coombs Date: Fri May 19 13:00:29 2023 -0400 Avoid EncodingWarning in blib2to3 (#3696) commit 2fd9d8b339e1e2e1b93956c6d68b2b358b3fc29d Author: Jonathan Berthias Date: Fri May 19 01:57:17 2023 +0200 Remove blank lines before class docstring (#3692) commit db3668a381e82866269ff25cb4cd11d2a53d009a Author: Ray Bell Date: Tue May 16 22:47:45 2023 -0400 Sort DEFAULT_EXCLUDES and add .vscode, .pytest_cache and .ruff_cache (#3691) Co-authored-by: Ray Bell --- CHANGES.md | 3 + src/blib2to3/pgen2/pgen.py | 3 +- src/cercis/lines.py | 2 + src/cercis/mode.py | 1 + .../preview/no_blank_line_before_docstring.py | 58 +++++++++++++++++++ 5 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 tests/data/preview/no_blank_line_before_docstring.py diff --git a/CHANGES.md b/CHANGES.md index f9bec185ff5..6a9923f8d8d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -16,11 +16,14 @@ - Implicitly concatenated strings used as function args are no longer wrapped inside parentheses (#3640) +- Remove blank lines between a class definition and its docstring (#3692) ### Configuration +- `.pytest_cache`, `.ruff_cache` and `.vscode` are now excluded by default (#3691) + ### Packaging diff --git a/src/blib2to3/pgen2/pgen.py b/src/blib2to3/pgen2/pgen.py index 631682a77c9..b5ebc7b3e42 100644 --- a/src/blib2to3/pgen2/pgen.py +++ b/src/blib2to3/pgen2/pgen.py @@ -30,7 +30,6 @@ class PgenGrammar(grammar.Grammar): class ParserGenerator(object): - filename: Path stream: IO[Text] generator: Iterator[GoodTokenInfo] @@ -39,7 +38,7 @@ class ParserGenerator(object): def __init__(self, filename: Path, stream: Optional[IO[Text]] = None) -> None: close_stream = None if stream is None: - stream = open(filename) + stream = open(filename, encoding="utf-8") close_stream = stream.close self.filename = filename self.stream = stream diff --git a/src/cercis/lines.py b/src/cercis/lines.py index 082bbd2b5d8..cbb6bd1bbe2 100644 --- a/src/cercis/lines.py +++ b/src/cercis/lines.py @@ -696,6 +696,8 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]: and self.previous_line.is_class and current_line.is_triple_quoted_string ): + if Preview.no_blank_line_before_class_docstring in current_line.mode: + return 0, 1 return before, 1 if self.previous_line and self.previous_line.opens_block: diff --git a/src/cercis/mode.py b/src/cercis/mode.py index 2cb55bc6582..913c2dc9d07 100644 --- a/src/cercis/mode.py +++ b/src/cercis/mode.py @@ -170,6 +170,7 @@ class Preview(Enum): hex_codes_in_unicode_sequences = auto() improved_async_statements_handling = auto() multiline_string_handling = auto() + no_blank_line_before_class_docstring = auto() prefer_splitting_right_hand_side_of_assignments = auto() # NOTE: string_processing requires wrap_long_dict_values_in_parens # for https://github.com/psf/black/issues/3117 to be fixed. diff --git a/tests/data/preview/no_blank_line_before_docstring.py b/tests/data/preview/no_blank_line_before_docstring.py new file mode 100644 index 00000000000..a37362de100 --- /dev/null +++ b/tests/data/preview/no_blank_line_before_docstring.py @@ -0,0 +1,58 @@ +def line_before_docstring(): + + """Please move me up""" + + +class LineBeforeDocstring: + + """Please move me up""" + + +class EvenIfThereIsAMethodAfter: + + """I'm the docstring""" + def method(self): + pass + + +class TwoLinesBeforeDocstring: + + + """I want to be treated the same as if I were closer""" + + +class MultilineDocstringsAsWell: + + """I'm so far + + and on so many lines... + """ + + +# output + + +def line_before_docstring(): + """Please move me up""" + + +class LineBeforeDocstring: + """Please move me up""" + + +class EvenIfThereIsAMethodAfter: + """I'm the docstring""" + + def method(self): + pass + + +class TwoLinesBeforeDocstring: + """I want to be treated the same as if I were closer""" + + +class MultilineDocstringsAsWell: + """I'm so far + + and on so many lines... + """