@@ -565,14 +565,9 @@ def maybe_empty_lines(self, current_line: Line) -> LinesBlock:
565565 )
566566 before , after = self ._maybe_empty_lines (current_line )
567567 previous_after = self .previous_block .after if self .previous_block else 0
568- before = (
569- # Black should not insert empty lines at the beginning
570- # of the file
571- 0
572- if self .previous_line is None
573- else before - previous_after
574- )
568+ before = max (0 , before - previous_after )
575569 if (
570+ # Always have one empty line after a module docstring
576571 self .previous_block
577572 and self .previous_block .previous_block is None
578573 and len (self .previous_block .original_line .leaves ) == 1
@@ -607,10 +602,11 @@ def maybe_empty_lines(self, current_line: Line) -> LinesBlock:
607602 self .previous_block = block
608603 return block
609604
610- def _maybe_empty_lines (self , current_line : Line ) -> Tuple [int , int ]:
605+ def _maybe_empty_lines (self , current_line : Line ) -> Tuple [int , int ]: # noqa: C901
611606 max_allowed = 1
612607 if current_line .depth == 0 :
613608 max_allowed = 1 if self .mode .is_pyi else 2
609+
614610 if current_line .leaves :
615611 # Consume the first leaf's extra newlines.
616612 first_leaf = current_line .leaves [0 ]
@@ -623,9 +619,22 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
623619 user_had_newline = bool (before )
624620 depth = current_line .depth
625621
622+ # Mutate self.previous_defs, remainder of this function should be pure
626623 previous_def = None
627624 while self .previous_defs and self .previous_defs [- 1 ].depth >= depth :
628625 previous_def = self .previous_defs .pop ()
626+ if current_line .is_def or current_line .is_class :
627+ self .previous_defs .append (current_line )
628+
629+ if self .previous_line is None :
630+ # Don't insert empty lines before the first line in the file.
631+ return 0 , 0
632+
633+ if current_line .is_docstring :
634+ if self .previous_line .is_class :
635+ return 0 , 1
636+ if self .previous_line .opens_block and self .previous_line .is_def :
637+ return 0 , 0
629638
630639 if previous_def is not None :
631640 assert self .previous_line is not None
@@ -668,58 +677,32 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
668677 )
669678
670679 if (
671- self .previous_line
672- and self .previous_line .is_import
680+ self .previous_line .is_import
673681 and not current_line .is_import
674682 and not current_line .is_fmt_pass_converted (first_leaf_matches = is_import )
675683 and depth == self .previous_line .depth
676684 ):
677685 return (before or 1 ), 0
678686
679- if (
680- self .previous_line
681- and self .previous_line .is_class
682- and current_line .is_docstring
683- ):
684- return 0 , 1
685-
686- # In preview mode, always allow blank lines, except right before a function
687- # docstring
688- is_empty_first_line_ok = not current_line .is_docstring or (
689- self .previous_line and not self .previous_line .is_def
690- )
691-
692- if (
693- self .previous_line
694- and self .previous_line .opens_block
695- and not is_empty_first_line_ok
696- ):
697- return 0 , 0
698687 return before , 0
699688
700689 def _maybe_empty_lines_for_class_or_def ( # noqa: C901
701690 self , current_line : Line , before : int , user_had_newline : bool
702691 ) -> Tuple [int , int ]:
703- if not current_line .is_decorator :
704- self .previous_defs .append (current_line )
705- if self .previous_line is None :
706- # Don't insert empty lines before the first line in the file.
707- return 0 , 0
692+ assert self .previous_line is not None
708693
709694 if self .previous_line .is_decorator :
710695 if self .mode .is_pyi and current_line .is_stub_class :
711696 # Insert an empty line after a decorated stub class
712697 return 0 , 1
713-
714698 return 0 , 0
715699
716700 if self .previous_line .depth < current_line .depth and (
717701 self .previous_line .is_class or self .previous_line .is_def
718702 ):
719703 if self .mode .is_pyi :
720704 return 0 , 0
721- else :
722- return 1 if user_had_newline else 0 , 0
705+ return 1 if user_had_newline else 0 , 0
723706
724707 comment_to_add_newlines : Optional [LinesBlock ] = None
725708 if (
@@ -750,9 +733,6 @@ def _maybe_empty_lines_for_class_or_def( # noqa: C901
750733 newlines = 0
751734 else :
752735 newlines = 1
753- # Remove case `self.previous_line.depth > current_line.depth` below when
754- # this becomes stable.
755- #
756736 # Don't inspect the previous line if it's part of the body of the previous
757737 # statement in the same level, we always want a blank line if there's
758738 # something with a body preceding.
@@ -769,8 +749,6 @@ def _maybe_empty_lines_for_class_or_def( # noqa: C901
769749 # Blank line between a block of functions (maybe with preceding
770750 # decorators) and a block of non-functions
771751 newlines = 1
772- elif self .previous_line .depth > current_line .depth :
773- newlines = 1
774752 else :
775753 newlines = 0
776754 else :
0 commit comments