diff --git a/crates/ruff_linter/resources/test/fixtures/pycodestyle/E30.py b/crates/ruff_linter/resources/test/fixtures/pycodestyle/E30.py index cb2545982b23e..3439d9982389c 100644 --- a/crates/ruff_linter/resources/test/fixtures/pycodestyle/E30.py +++ b/crates/ruff_linter/resources/test/fixtures/pycodestyle/E30.py @@ -515,6 +515,26 @@ def a(): # end +# E303 +class Class: + def a(self): + pass + + + def b(self): + pass +# end + + +# E303 +if True: + a = 1 + + + a = 2 +# end + + # E304 @decorator diff --git a/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/blank_lines.rs b/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/blank_lines.rs index d9c794a42d7f6..c9db7a8d4961c 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/blank_lines.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/blank_lines.rs @@ -167,8 +167,8 @@ impl AlwaysFixableViolation for BlankLinesTopLevel { /// /// ## Why is this bad? /// PEP 8 recommends using blank lines as follows: -/// - Surround top-level function and class definitions with two blank lines. -/// - Surround method definitions inside a class by a single blank line. +/// - No more than two blank lines between top-level statements. +/// - No more than one blank line between non-top-level statements. /// /// ## Example /// ```python @@ -471,8 +471,7 @@ pub(crate) fn blank_lines( } if line.line.blank_lines > BlankLinesConfig::TOP_LEVEL - || ((tracked_vars.is_in_class || tracked_vars.is_in_fn) - && line.line.blank_lines > BlankLinesConfig::METHOD) + || (indent_level > 0 && line.line.blank_lines > BlankLinesConfig::METHOD) { // E303 let mut diagnostic = diff --git a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E303_E30.py.snap b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E303_E30.py.snap index 2f75dfe18fbb2..29c3ce985c118 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E303_E30.py.snap +++ b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E303_E30.py.snap @@ -125,4 +125,39 @@ E30.py:512:1: E303 [*] Too many blank lines (3) 513 512 | It gives error E303: too many blank lines (3) 514 513 | """ +E30.py:524:5: E303 [*] Too many blank lines (2) + | +524 | def b(self): + | ^^^ E303 +525 | pass +526 | # end + | + = help: Remove extraneous blank line(s) + +ℹ Safe fix +520 520 | def a(self): +521 521 | pass +522 522 | +523 |- +524 523 | def b(self): +525 524 | pass +526 525 | # end + +E30.py:534:5: E303 [*] Too many blank lines (2) + | +534 | a = 2 + | ^ E303 +535 | # end + | + = help: Remove extraneous blank line(s) + +ℹ Safe fix +530 530 | if True: +531 531 | a = 1 +532 532 | +533 |- +534 533 | a = 2 +535 534 | # end +536 535 | + diff --git a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E304_E30.py.snap b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E304_E30.py.snap index 54b8fe7c92869..b65d50e537bcb 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E304_E30.py.snap +++ b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E304_E30.py.snap @@ -1,24 +1,24 @@ --- source: crates/ruff_linter/src/rules/pycodestyle/mod.rs --- -E30.py:521:1: E304 [*] blank lines found after function decorator +E30.py:541:1: E304 [*] blank lines found after function decorator | -519 | @decorator -520 | -521 | def function(): +539 | @decorator +540 | +541 | def function(): | ^^^ E304 -522 | pass -523 | # end +542 | pass +543 | # end | = help: Remove extraneous blank line(s) ℹ Safe fix -517 517 | -518 518 | # E304 -519 519 | @decorator -520 |- -521 520 | def function(): -522 521 | pass -523 522 | # end +537 537 | +538 538 | # E304 +539 539 | @decorator +540 |- +541 540 | def function(): +542 541 | pass +543 542 | # end diff --git a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E305_E30.py.snap b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E305_E30.py.snap index dc30b8f8b471a..87e987f761575 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E305_E30.py.snap +++ b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E305_E30.py.snap @@ -1,102 +1,102 @@ --- source: crates/ruff_linter/src/rules/pycodestyle/mod.rs --- -E30.py:533:1: E305 [*] expected 2 blank lines after class or function definition, found (0) +E30.py:553:1: E305 [*] expected 2 blank lines after class or function definition, found (0) | -532 | # another comment -533 | fn() +552 | # another comment +553 | fn() | ^^ E305 -534 | # end +554 | # end | = help: Add missing blank line(s) ℹ Safe fix -530 530 | # comment -531 531 | -532 532 | # another comment - 533 |+ - 534 |+ -533 535 | fn() -534 536 | # end -535 537 | +550 550 | # comment +551 551 | +552 552 | # another comment + 553 |+ + 554 |+ +553 555 | fn() +554 556 | # end +555 557 | -E30.py:544:1: E305 [*] expected 2 blank lines after class or function definition, found (0) +E30.py:564:1: E305 [*] expected 2 blank lines after class or function definition, found (0) | -543 | # another comment -544 | a = 1 +563 | # another comment +564 | a = 1 | ^ E305 -545 | # end +565 | # end | = help: Add missing blank line(s) ℹ Safe fix -541 541 | # comment -542 542 | -543 543 | # another comment - 544 |+ - 545 |+ -544 546 | a = 1 -545 547 | # end -546 548 | +561 561 | # comment +562 562 | +563 563 | # another comment + 564 |+ + 565 |+ +564 566 | a = 1 +565 567 | # end +566 568 | -E30.py:556:1: E305 [*] expected 2 blank lines after class or function definition, found (1) +E30.py:576:1: E305 [*] expected 2 blank lines after class or function definition, found (1) | -554 | # another comment -555 | -556 | try: +574 | # another comment +575 | +576 | try: | ^^^ E305 -557 | fn() -558 | except Exception: +577 | fn() +578 | except Exception: | = help: Add missing blank line(s) ℹ Safe fix -553 553 | -554 554 | # another comment -555 555 | - 556 |+ -556 557 | try: -557 558 | fn() -558 559 | except Exception: +573 573 | +574 574 | # another comment +575 575 | + 576 |+ +576 577 | try: +577 578 | fn() +578 579 | except Exception: -E30.py:568:1: E305 [*] expected 2 blank lines after class or function definition, found (0) +E30.py:588:1: E305 [*] expected 2 blank lines after class or function definition, found (0) | -567 | # Two spaces before comments, too. -568 | if a(): +587 | # Two spaces before comments, too. +588 | if a(): | ^^ E305 -569 | a() -570 | # end +589 | a() +590 | # end | = help: Add missing blank line(s) ℹ Safe fix -565 565 | print -566 566 | -567 567 | # Two spaces before comments, too. - 568 |+ - 569 |+ -568 570 | if a(): -569 571 | a() -570 572 | # end +585 585 | print +586 586 | +587 587 | # Two spaces before comments, too. + 588 |+ + 589 |+ +588 590 | if a(): +589 591 | a() +590 592 | # end -E30.py:581:1: E305 [*] expected 2 blank lines after class or function definition, found (1) +E30.py:601:1: E305 [*] expected 2 blank lines after class or function definition, found (1) | -579 | blah, blah -580 | -581 | if __name__ == '__main__': +599 | blah, blah +600 | +601 | if __name__ == '__main__': | ^^ E305 -582 | main() -583 | # end +602 | main() +603 | # end | = help: Add missing blank line(s) ℹ Safe fix -578 578 | def main(): -579 579 | blah, blah -580 580 | - 581 |+ -581 582 | if __name__ == '__main__': -582 583 | main() -583 584 | # end +598 598 | def main(): +599 599 | blah, blah +600 600 | + 601 |+ +601 602 | if __name__ == '__main__': +602 603 | main() +603 604 | # end diff --git a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E306_E30.py.snap b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E306_E30.py.snap index 1e2b4cfa1ac8e..d045a07886b18 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E306_E30.py.snap +++ b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E306_E30.py.snap @@ -1,263 +1,263 @@ --- source: crates/ruff_linter/src/rules/pycodestyle/mod.rs --- -E30.py:589:5: E306 [*] Expected 1 blank line before a nested definition, found 0 +E30.py:609:5: E306 [*] Expected 1 blank line before a nested definition, found 0 | -587 | def a(): -588 | x = 1 -589 | def b(): +607 | def a(): +608 | x = 1 +609 | def b(): | ^^^ E306 -590 | pass -591 | # end +610 | pass +611 | # end | = help: Add missing blank line ℹ Safe fix -586 586 | # E306:3:5 -587 587 | def a(): -588 588 | x = 1 - 589 |+ -589 590 | def b(): -590 591 | pass -591 592 | # end +606 606 | # E306:3:5 +607 607 | def a(): +608 608 | x = 1 + 609 |+ +609 610 | def b(): +610 611 | pass +611 612 | # end -E30.py:597:5: E306 [*] Expected 1 blank line before a nested definition, found 0 +E30.py:617:5: E306 [*] Expected 1 blank line before a nested definition, found 0 | -595 | async def a(): -596 | x = 1 -597 | def b(): +615 | async def a(): +616 | x = 1 +617 | def b(): | ^^^ E306 -598 | pass -599 | # end +618 | pass +619 | # end | = help: Add missing blank line ℹ Safe fix -594 594 | #: E306:3:5 -595 595 | async def a(): -596 596 | x = 1 - 597 |+ -597 598 | def b(): -598 599 | pass -599 600 | # end +614 614 | #: E306:3:5 +615 615 | async def a(): +616 616 | x = 1 + 617 |+ +617 618 | def b(): +618 619 | pass +619 620 | # end -E30.py:605:5: E306 [*] Expected 1 blank line before a nested definition, found 0 +E30.py:625:5: E306 [*] Expected 1 blank line before a nested definition, found 0 | -603 | def a(): -604 | x = 2 -605 | def b(): +623 | def a(): +624 | x = 2 +625 | def b(): | ^^^ E306 -606 | x = 1 -607 | def c(): +626 | x = 1 +627 | def c(): | = help: Add missing blank line ℹ Safe fix -602 602 | #: E306:3:5 E306:5:9 -603 603 | def a(): -604 604 | x = 2 - 605 |+ -605 606 | def b(): -606 607 | x = 1 -607 608 | def c(): +622 622 | #: E306:3:5 E306:5:9 +623 623 | def a(): +624 624 | x = 2 + 625 |+ +625 626 | def b(): +626 627 | x = 1 +627 628 | def c(): -E30.py:607:9: E306 [*] Expected 1 blank line before a nested definition, found 0 +E30.py:627:9: E306 [*] Expected 1 blank line before a nested definition, found 0 | -605 | def b(): -606 | x = 1 -607 | def c(): +625 | def b(): +626 | x = 1 +627 | def c(): | ^^^ E306 -608 | pass -609 | # end +628 | pass +629 | # end | = help: Add missing blank line ℹ Safe fix -604 604 | x = 2 -605 605 | def b(): -606 606 | x = 1 - 607 |+ -607 608 | def c(): -608 609 | pass -609 610 | # end +624 624 | x = 2 +625 625 | def b(): +626 626 | x = 1 + 627 |+ +627 628 | def c(): +628 629 | pass +629 630 | # end -E30.py:615:5: E306 [*] Expected 1 blank line before a nested definition, found 0 +E30.py:635:5: E306 [*] Expected 1 blank line before a nested definition, found 0 | -613 | def a(): -614 | x = 1 -615 | class C: +633 | def a(): +634 | x = 1 +635 | class C: | ^^^^^ E306 -616 | pass -617 | x = 2 +636 | pass +637 | x = 2 | = help: Add missing blank line ℹ Safe fix -612 612 | # E306:3:5 E306:6:5 -613 613 | def a(): -614 614 | x = 1 - 615 |+ -615 616 | class C: -616 617 | pass -617 618 | x = 2 +632 632 | # E306:3:5 E306:6:5 +633 633 | def a(): +634 634 | x = 1 + 635 |+ +635 636 | class C: +636 637 | pass +637 638 | x = 2 -E30.py:618:5: E306 [*] Expected 1 blank line before a nested definition, found 0 +E30.py:638:5: E306 [*] Expected 1 blank line before a nested definition, found 0 | -616 | pass -617 | x = 2 -618 | def b(): +636 | pass +637 | x = 2 +638 | def b(): | ^^^ E306 -619 | pass -620 | # end +639 | pass +640 | # end | = help: Add missing blank line ℹ Safe fix -615 615 | class C: -616 616 | pass -617 617 | x = 2 - 618 |+ -618 619 | def b(): -619 620 | pass -620 621 | # end +635 635 | class C: +636 636 | pass +637 637 | x = 2 + 638 |+ +638 639 | def b(): +639 640 | pass +640 641 | # end -E30.py:625:5: E306 [*] Expected 1 blank line before a nested definition, found 0 +E30.py:645:5: E306 [*] Expected 1 blank line before a nested definition, found 0 | -623 | # E306 -624 | def foo(): -625 | def bar(): +643 | # E306 +644 | def foo(): +645 | def bar(): | ^^^ E306 -626 | pass -627 | def baz(): pass +646 | pass +647 | def baz(): pass | = help: Add missing blank line ℹ Safe fix -622 622 | -623 623 | # E306 -624 624 | def foo(): - 625 |+ -625 626 | def bar(): -626 627 | pass -627 628 | def baz(): pass +642 642 | +643 643 | # E306 +644 644 | def foo(): + 645 |+ +645 646 | def bar(): +646 647 | pass +647 648 | def baz(): pass -E30.py:627:5: E306 [*] Expected 1 blank line before a nested definition, found 0 +E30.py:647:5: E306 [*] Expected 1 blank line before a nested definition, found 0 | -625 | def bar(): -626 | pass -627 | def baz(): pass +645 | def bar(): +646 | pass +647 | def baz(): pass | ^^^ E306 -628 | # end +648 | # end | = help: Add missing blank line ℹ Safe fix -624 624 | def foo(): -625 625 | def bar(): -626 626 | pass - 627 |+ -627 628 | def baz(): pass -628 629 | # end -629 630 | +644 644 | def foo(): +645 645 | def bar(): +646 646 | pass + 647 |+ +647 648 | def baz(): pass +648 649 | # end +649 650 | -E30.py:634:5: E306 [*] Expected 1 blank line before a nested definition, found 0 +E30.py:654:5: E306 [*] Expected 1 blank line before a nested definition, found 0 | -632 | def foo(): -633 | def bar(): pass -634 | def baz(): +652 | def foo(): +653 | def bar(): pass +654 | def baz(): | ^^^ E306 -635 | pass -636 | # end +655 | pass +656 | # end | = help: Add missing blank line ℹ Safe fix -631 631 | # E306:3:5 -632 632 | def foo(): -633 633 | def bar(): pass - 634 |+ -634 635 | def baz(): -635 636 | pass -636 637 | # end +651 651 | # E306:3:5 +652 652 | def foo(): +653 653 | def bar(): pass + 654 |+ +654 655 | def baz(): +655 656 | pass +656 657 | # end -E30.py:641:5: E306 [*] Expected 1 blank line before a nested definition, found 0 +E30.py:661:5: E306 [*] Expected 1 blank line before a nested definition, found 0 | -639 | # E306 -640 | def f(): -641 | def f(): +659 | # E306 +660 | def f(): +661 | def f(): | ^^^ E306 -642 | pass -643 | # end +662 | pass +663 | # end | = help: Add missing blank line ℹ Safe fix -638 638 | -639 639 | # E306 -640 640 | def f(): - 641 |+ -641 642 | def f(): -642 643 | pass -643 644 | # end +658 658 | +659 659 | # E306 +660 660 | def f(): + 661 |+ +661 662 | def f(): +662 663 | pass +663 664 | # end -E30.py:649:5: E306 [*] Expected 1 blank line before a nested definition, found 0 +E30.py:669:5: E306 [*] Expected 1 blank line before a nested definition, found 0 | -647 | def a(): -648 | x = 2 -649 | @decorator +667 | def a(): +668 | x = 2 +669 | @decorator | ^ E306 -650 | def b(): -651 | pass +670 | def b(): +671 | pass | = help: Add missing blank line ℹ Safe fix -646 646 | # E306 -647 647 | def a(): -648 648 | x = 2 - 649 |+ -649 650 | @decorator -650 651 | def b(): -651 652 | pass +666 666 | # E306 +667 667 | def a(): +668 668 | x = 2 + 669 |+ +669 670 | @decorator +670 671 | def b(): +671 672 | pass -E30.py:658:5: E306 [*] Expected 1 blank line before a nested definition, found 0 +E30.py:678:5: E306 [*] Expected 1 blank line before a nested definition, found 0 | -656 | def a(): -657 | x = 2 -658 | @decorator +676 | def a(): +677 | x = 2 +678 | @decorator | ^ E306 -659 | async def b(): -660 | pass +679 | async def b(): +680 | pass | = help: Add missing blank line ℹ Safe fix -655 655 | # E306 -656 656 | def a(): -657 657 | x = 2 - 658 |+ -658 659 | @decorator -659 660 | async def b(): -660 661 | pass +675 675 | # E306 +676 676 | def a(): +677 677 | x = 2 + 678 |+ +678 679 | @decorator +679 680 | async def b(): +680 681 | pass -E30.py:667:5: E306 [*] Expected 1 blank line before a nested definition, found 0 +E30.py:687:5: E306 [*] Expected 1 blank line before a nested definition, found 0 | -665 | def a(): -666 | x = 2 -667 | async def b(): +685 | def a(): +686 | x = 2 +687 | async def b(): | ^^^^^ E306 -668 | pass -669 | # end +688 | pass +689 | # end | = help: Add missing blank line ℹ Safe fix -664 664 | # E306 -665 665 | def a(): -666 666 | x = 2 - 667 |+ -667 668 | async def b(): -668 669 | pass -669 670 | # end +684 684 | # E306 +685 685 | def a(): +686 686 | x = 2 + 687 |+ +687 688 | async def b(): +688 689 | pass +689 690 | # end