From 29e97d1d4a7717f1bd0ca35cacf2f2ce6d815b0c Mon Sep 17 00:00:00 2001 From: Vishwas B Sharma Date: Tue, 24 Apr 2018 09:38:12 -0700 Subject: [PATCH] [#154] Handle comments between decorators properly (#166) --- black.py | 4 ++++ tests/comments6.py | 8 ++++++++ tests/test_black.py | 8 ++++++++ 3 files changed, 20 insertions(+) create mode 100644 tests/comments6.py diff --git a/black.py b/black.py index 15a7547..a03b9aa 100644 --- a/black.py +++ b/black.py @@ -1044,6 +1044,10 @@ class EmptyLineTracker: # Don't insert empty lines between decorators. return 0, 0 + if is_decorator and self.previous_line and self.previous_line.is_comment: + # Don't insert empty lines between decorator comments. + return 0, 0 + newlines = 2 if current_line.depth: newlines -= 1 diff --git a/tests/comments6.py b/tests/comments6.py new file mode 100644 index 0000000..0565015 --- /dev/null +++ b/tests/comments6.py @@ -0,0 +1,8 @@ +@property +# TODO: X +@property +# TODO: Y +# TODO: Z +@property +def foo(): + pass diff --git a/tests/test_black.py b/tests/test_black.py index dd3beed..9c029df 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -626,6 +626,14 @@ class BlackTestCase(unittest.TestCase): ) self.assertEqual(result.exit_code, 1) + @patch("black.dump_to_file", dump_to_stderr) + def test_comment_in_decorator(self) -> None: + source, expected = read_data("comments6") + actual = fs(source) + self.assertFormatEqual(expected, actual) + black.assert_equivalent(source, actual) + black.assert_stable(source, actual, line_length=ll) + if __name__ == "__main__": unittest.main() -- 2.39.5