diff --git a/src/black/lines.py b/src/black/lines.py index 8d02267a85b..4d4f47a44e8 100644 --- a/src/black/lines.py +++ b/src/black/lines.py @@ -745,7 +745,10 @@ def _maybe_empty_lines_for_class_or_def( # noqa: C901 if self.previous_line.depth < current_line.depth and ( self.previous_line.is_class or self.previous_line.is_def ): - return 0, 0 + if self.mode.is_pyi or not Preview.allow_empty_first_line_in_block: + return 0, 0 + else: + return 1 if user_had_newline else 0, 0 comment_to_add_newlines: Optional[LinesBlock] = None if ( diff --git a/tests/data/cases/class_blank_parentheses.py b/tests/data/cases/class_blank_parentheses.py index 1a5721a2889..3c460d9bd79 100644 --- a/tests/data/cases/class_blank_parentheses.py +++ b/tests/data/cases/class_blank_parentheses.py @@ -39,6 +39,7 @@ def test_func(self): class ClassWithEmptyFunc(object): + def func_with_blank_parentheses(): return 5 diff --git a/tests/data/cases/preview_allow_empty_first_line.py b/tests/data/cases/preview_allow_empty_first_line.py index 3e14fa15250..5ece2700bdb 100644 --- a/tests/data/cases/preview_allow_empty_first_line.py +++ b/tests/data/cases/preview_allow_empty_first_line.py @@ -22,7 +22,7 @@ def foo(): Long comment here """ a = 123 - + if z: for _ in range(100): @@ -62,6 +62,15 @@ def method(self): pass + +def top_level( + a: int, + b: str, +) -> Whatever[Generic, Something]: + + def nested(x: int) -> int: + pass + # output def foo(): @@ -123,6 +132,16 @@ def quux(): class Cls: + def method(self): pass + + +def top_level( + a: int, + b: str, +) -> Whatever[Generic, Something]: + + def nested(x: int) -> int: + pass diff --git a/tests/data/cases/preview_form_feeds.py b/tests/data/cases/preview_form_feeds.py index c236f177a95..08ba21fc22d 100644 --- a/tests/data/cases/preview_form_feeds.py +++ b/tests/data/cases/preview_form_feeds.py @@ -37,7 +37,7 @@ # # - + # \ @@ -72,7 +72,7 @@ pass pass - + pass @@ -95,7 +95,7 @@ class Baz: def __init__(self): pass - + def something(self): pass @@ -203,6 +203,7 @@ def bar(a=1, b: bool = False): class Baz: + def __init__(self): pass