diff --git a/src/black/lines.py b/src/black/lines.py index 2f6b4138cf6..0b46daf3ae8 100644 --- a/src/black/lines.py +++ b/src/black/lines.py @@ -725,14 +725,15 @@ def _maybe_empty_lines_for_class_or_def( # noqa: C901 else: newlines = 0 else: + newlines = 1 if current_line.depth else 2 + # If a user has left no space after a dummy implementation, don't insert + # new lines. This is useful for instance for @overload or Protocols. if ( Preview.dummy_implementations in self.mode and self.previous_line.is_stub_def - and (current_line.is_stub_def or current_line.is_decorator) + and not bool(user_hint_before) ): - newlines = user_hint_before - else: - newlines = 1 if current_line.depth else 2 + newlines = 0 if comment_to_add_newlines is not None: previous_block = comment_to_add_newlines.previous_block if previous_block is not None: diff --git a/tests/data/preview/dummy_implementations.py b/tests/data/preview/dummy_implementations.py index 1b9c6ba2649..e07c25ed129 100644 --- a/tests/data/preview/dummy_implementations.py +++ b/tests/data/preview/dummy_implementations.py @@ -34,6 +34,19 @@ def dummy_three(): def dummy_four(): ... +@overload +def b(arg: int) -> int: ... + +@overload +def b(arg: str) -> str: ... +@overload +def b(arg: object) -> NoReturn: ... + +def b(arg: Union[int, str, object]) -> Union[int, str]: + if not isinstance(arg, (int, str)): + raise TypeError + return arg + # output from typing import NoReturn, Protocol, Union, overload @@ -49,8 +62,6 @@ def a(arg: int) -> int: ... def a(arg: str) -> str: ... @overload def a(arg: object) -> NoReturn: ... - - def a(arg: Union[int, str, object]) -> Union[int, str]: if not isinstance(arg, (int, str)): raise TypeError @@ -68,4 +79,21 @@ def dummy_two(): ... @dummy def dummy_three(): ... + def dummy_four(): ... + + +@overload +def b(arg: int) -> int: ... + + +@overload +def b(arg: str) -> str: ... +@overload +def b(arg: object) -> NoReturn: ... + + +def b(arg: Union[int, str, object]) -> Union[int, str]: + if not isinstance(arg, (int, str)): + raise TypeError + return arg