From 99dcb76cc96a11225971f5806161fff6eeb7fad2 Mon Sep 17 00:00:00 2001 From: Hoel Bagard Date: Sun, 25 Feb 2024 19:25:53 +0900 Subject: [PATCH 1/4] add fixture --- .../test/fixtures/pycodestyle/E30.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/crates/ruff_linter/resources/test/fixtures/pycodestyle/E30.py b/crates/ruff_linter/resources/test/fixtures/pycodestyle/E30.py index 8a53b7303262fc..1957fb1dcecfee 100644 --- a/crates/ruff_linter/resources/test/fixtures/pycodestyle/E30.py +++ b/crates/ruff_linter/resources/test/fixtures/pycodestyle/E30.py @@ -466,6 +466,29 @@ def fn2(): # end +# E301 +class Class: + """Class for minimal repo.""" + + columns = [] + @classmethod + def cls_method(cls) -> None: + pass +# end + + +# E301 +class Class: + """Class for minimal repo.""" + + def method(cls) -> None: + pass + @classmethod + def cls_method(cls) -> None: + pass +# end + + # E302 """Main module.""" def fn(): From fd2c46f882b8438e651b1bbe6ced6480f5fb59bc Mon Sep 17 00:00:00 2001 From: Hoel Bagard Date: Sun, 25 Feb 2024 19:42:03 +0900 Subject: [PATCH 2/4] fix: trigger E301 on decorated methods. fixes #10114 --- .../ruff_linter/src/rules/pycodestyle/rules/blank_lines.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/ruff_linter/src/rules/pycodestyle/rules/blank_lines.rs b/crates/ruff_linter/src/rules/pycodestyle/rules/blank_lines.rs index 59effe7f944dab..9cbbb556f56b3d 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/rules/blank_lines.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/rules/blank_lines.rs @@ -658,8 +658,10 @@ impl BlankLinesChecker { if self.is_not_first_logical_line { if line.preceding_blank_lines == 0 // Only applies to methods. - && matches!(line.kind, LogicalLineKind::Function) - && matches!(self.class_status, Status::Inside(_)) + && matches!(line.kind, LogicalLineKind::Function | LogicalLineKind::Decorator) + // Allow groups of one-liners. + && !(matches!(state.follows, Follows::Def) && !matches!(line.last_token, TokenKind::Colon)) + && matches!(state.class_status, Status::Inside(_)) // The class/parent method's docstring can directly precede the def. // Allow following a decorator (if there is an error it will be triggered on the first decorator). && !matches!(self.follows, Follows::Docstring | Follows::Decorator) From b108ceea77894ac43205b32146132435f2ebfcd8 Mon Sep 17 00:00:00 2001 From: Hoel Bagard Date: Sun, 25 Feb 2024 19:44:47 +0900 Subject: [PATCH 3/4] update snapshots --- ...ules__pycodestyle__tests__E301_E30.py.snap | 39 +++ ...ules__pycodestyle__tests__E302_E30.py.snap | 272 +++++++-------- ...ules__pycodestyle__tests__E303_E30.py.snap | 296 ++++++++-------- ...ules__pycodestyle__tests__E304_E30.py.snap | 80 ++--- ...ules__pycodestyle__tests__E305_E30.py.snap | 126 +++---- ...ules__pycodestyle__tests__E306_E30.py.snap | 324 +++++++++--------- 6 files changed, 588 insertions(+), 549 deletions(-) diff --git a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E301_E30.py.snap b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E301_E30.py.snap index 51a29a3d97d33e..9834432cd9ec60 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E301_E30.py.snap +++ b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E301_E30.py.snap @@ -41,4 +41,43 @@ E30.py:464:5: E301 [*] Expected 1 blank line, found 0 464 465 | def fn2(): 465 466 | pass +E30.py:474:5: E301 [*] Expected 1 blank line, found 0 + | +473 | columns = [] +474 | @classmethod + | ^ E301 +475 | def cls_method(cls) -> None: +476 | pass + | + = help: Add missing blank line + +ℹ Safe fix +471 471 | """Class for minimal repo.""" +472 472 | +473 473 | columns = [] + 474 |+ +474 475 | @classmethod +475 476 | def cls_method(cls) -> None: +476 477 | pass + +E30.py:486:5: E301 [*] Expected 1 blank line, found 0 + | +484 | def method(cls) -> None: +485 | pass +486 | @classmethod + | ^ E301 +487 | def cls_method(cls) -> None: +488 | pass + | + = help: Add missing blank line + +ℹ Safe fix +483 483 | +484 484 | def method(cls) -> None: +485 485 | pass + 486 |+ +486 487 | @classmethod +487 488 | def cls_method(cls) -> None: +488 489 | pass + diff --git a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E302_E30.py.snap b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E302_E30.py.snap index 7af75e5ba98055..622d505ee0766c 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E302_E30.py.snap +++ b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E302_E30.py.snap @@ -1,187 +1,187 @@ --- source: crates/ruff_linter/src/rules/pycodestyle/mod.rs --- -E30.py:471:1: E302 [*] Expected 2 blank lines, found 0 +E30.py:494:1: E302 [*] Expected 2 blank lines, found 0 | -469 | # E302 -470 | """Main module.""" -471 | def fn(): +492 | # E302 +493 | """Main module.""" +494 | def fn(): | ^^^ E302 -472 | pass -473 | # end +495 | pass +496 | # end | = help: Add missing blank line(s) ℹ Safe fix -468 468 | -469 469 | # E302 -470 470 | """Main module.""" - 471 |+ - 472 |+ -471 473 | def fn(): -472 474 | pass -473 475 | # end - -E30.py:478:1: E302 [*] Expected 2 blank lines, found 0 - | -476 | # E302 -477 | import sys -478 | def get_sys_path(): +491 491 | +492 492 | # E302 +493 493 | """Main module.""" + 494 |+ + 495 |+ +494 496 | def fn(): +495 497 | pass +496 498 | # end + +E30.py:501:1: E302 [*] Expected 2 blank lines, found 0 + | +499 | # E302 +500 | import sys +501 | def get_sys_path(): | ^^^ E302 -479 | return sys.path -480 | # end +502 | return sys.path +503 | # end | = help: Add missing blank line(s) ℹ Safe fix -475 475 | -476 476 | # E302 -477 477 | import sys - 478 |+ - 479 |+ -478 480 | def get_sys_path(): -479 481 | return sys.path -480 482 | # end - -E30.py:487:1: E302 [*] Expected 2 blank lines, found 1 - | -485 | pass -486 | -487 | def b(): +498 498 | +499 499 | # E302 +500 500 | import sys + 501 |+ + 502 |+ +501 503 | def get_sys_path(): +502 504 | return sys.path +503 505 | # end + +E30.py:510:1: E302 [*] Expected 2 blank lines, found 1 + | +508 | pass +509 | +510 | def b(): | ^^^ E302 -488 | pass -489 | # end +511 | pass +512 | # end | = help: Add missing blank line(s) ℹ Safe fix -484 484 | def a(): -485 485 | pass -486 486 | - 487 |+ -487 488 | def b(): -488 489 | pass -489 490 | # end - -E30.py:498:1: E302 [*] Expected 2 blank lines, found 1 - | -496 | # comment -497 | -498 | def b(): +507 507 | def a(): +508 508 | pass +509 509 | + 510 |+ +510 511 | def b(): +511 512 | pass +512 513 | # end + +E30.py:521:1: E302 [*] Expected 2 blank lines, found 1 + | +519 | # comment +520 | +521 | def b(): | ^^^ E302 -499 | pass -500 | # end +522 | pass +523 | # end | = help: Add missing blank line(s) ℹ Safe fix -495 495 | -496 496 | # comment -497 497 | - 498 |+ -498 499 | def b(): -499 500 | pass -500 501 | # end - -E30.py:507:1: E302 [*] Expected 2 blank lines, found 1 - | -505 | pass -506 | -507 | async def b(): +518 518 | +519 519 | # comment +520 520 | + 521 |+ +521 522 | def b(): +522 523 | pass +523 524 | # end + +E30.py:530:1: E302 [*] Expected 2 blank lines, found 1 + | +528 | pass +529 | +530 | async def b(): | ^^^^^ E302 -508 | pass -509 | # end +531 | pass +532 | # end | = help: Add missing blank line(s) ℹ Safe fix -504 504 | def a(): -505 505 | pass -506 506 | - 507 |+ -507 508 | async def b(): -508 509 | pass -509 510 | # end - -E30.py:516:1: E302 [*] Expected 2 blank lines, found 1 - | -514 | pass -515 | -516 | async def x(y: int = 1): +527 527 | def a(): +528 528 | pass +529 529 | + 530 |+ +530 531 | async def b(): +531 532 | pass +532 533 | # end + +E30.py:539:1: E302 [*] Expected 2 blank lines, found 1 + | +537 | pass +538 | +539 | async def x(y: int = 1): | ^^^^^ E302 -517 | pass -518 | # end +540 | pass +541 | # end | = help: Add missing blank line(s) ℹ Safe fix -513 513 | async def x(): -514 514 | pass -515 515 | - 516 |+ -516 517 | async def x(y: int = 1): -517 518 | pass -518 519 | # end - -E30.py:524:1: E302 [*] Expected 2 blank lines, found 0 - | -522 | def bar(): -523 | pass -524 | def baz(): pass +536 536 | async def x(): +537 537 | pass +538 538 | + 539 |+ +539 540 | async def x(y: int = 1): +540 541 | pass +541 542 | # end + +E30.py:547:1: E302 [*] Expected 2 blank lines, found 0 + | +545 | def bar(): +546 | pass +547 | def baz(): pass | ^^^ E302 -525 | # end +548 | # end | = help: Add missing blank line(s) ℹ Safe fix -521 521 | # E302 -522 522 | def bar(): -523 523 | pass - 524 |+ - 525 |+ -524 526 | def baz(): pass -525 527 | # end -526 528 | - -E30.py:530:1: E302 [*] Expected 2 blank lines, found 0 - | -528 | # E302 -529 | def bar(): pass -530 | def baz(): +544 544 | # E302 +545 545 | def bar(): +546 546 | pass + 547 |+ + 548 |+ +547 549 | def baz(): pass +548 550 | # end +549 551 | + +E30.py:553:1: E302 [*] Expected 2 blank lines, found 0 + | +551 | # E302 +552 | def bar(): pass +553 | def baz(): | ^^^ E302 -531 | pass -532 | # end +554 | pass +555 | # end | = help: Add missing blank line(s) ℹ Safe fix -527 527 | -528 528 | # E302 -529 529 | def bar(): pass - 530 |+ - 531 |+ -530 532 | def baz(): -531 533 | pass -532 534 | # end - -E30.py:540:1: E302 [*] Expected 2 blank lines, found 1 - | -539 | # comment -540 | @decorator +550 550 | +551 551 | # E302 +552 552 | def bar(): pass + 553 |+ + 554 |+ +553 555 | def baz(): +554 556 | pass +555 557 | # end + +E30.py:563:1: E302 [*] Expected 2 blank lines, found 1 + | +562 | # comment +563 | @decorator | ^ E302 -541 | def g(): -542 | pass +564 | def g(): +565 | pass | = help: Add missing blank line(s) ℹ Safe fix -536 536 | def f(): -537 537 | pass -538 538 | - 539 |+ - 540 |+ -539 541 | # comment -540 542 | @decorator -541 543 | def g(): +559 559 | def f(): +560 560 | pass +561 561 | + 562 |+ + 563 |+ +562 564 | # comment +563 565 | @decorator +564 566 | def g(): 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 c9512126e1b901..1c854a6ad3ad00 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 @@ -1,250 +1,250 @@ --- source: crates/ruff_linter/src/rules/pycodestyle/mod.rs --- -E30.py:555:2: E303 [*] Too many blank lines (2) +E30.py:578:2: E303 [*] Too many blank lines (2) | -555 | def method2(): +578 | def method2(): | ^^^ E303 -556 | return 22 -557 | # end +579 | return 22 +580 | # end | = help: Remove extraneous blank line(s) ℹ Safe fix -551 551 | def method1(): -552 552 | return 1 -553 553 | -554 |- -555 554 | def method2(): -556 555 | return 22 -557 556 | # end +574 574 | def method1(): +575 575 | return 1 +576 576 | +577 |- +578 577 | def method2(): +579 578 | return 22 +580 579 | # end -E30.py:565:5: E303 [*] Too many blank lines (2) +E30.py:588:5: E303 [*] Too many blank lines (2) | -565 | # arbitrary comment +588 | # arbitrary comment | ^^^^^^^^^^^^^^^^^^^ E303 -566 | -567 | def inner(): # E306 not expected (pycodestyle detects E306) +589 | +590 | def inner(): # E306 not expected (pycodestyle detects E306) | = help: Remove extraneous blank line(s) ℹ Safe fix -561 561 | def fn(): -562 562 | _ = None -563 563 | -564 |- -565 564 | # arbitrary comment -566 565 | -567 566 | def inner(): # E306 not expected (pycodestyle detects E306) +584 584 | def fn(): +585 585 | _ = None +586 586 | +587 |- +588 587 | # arbitrary comment +589 588 | +590 589 | def inner(): # E306 not expected (pycodestyle detects E306) -E30.py:577:5: E303 [*] Too many blank lines (2) +E30.py:600:5: E303 [*] Too many blank lines (2) | -577 | # arbitrary comment +600 | # arbitrary comment | ^^^^^^^^^^^^^^^^^^^ E303 -578 | def inner(): # E306 not expected (pycodestyle detects E306) -579 | pass +601 | def inner(): # E306 not expected (pycodestyle detects E306) +602 | pass | = help: Remove extraneous blank line(s) ℹ Safe fix -573 573 | def fn(): -574 574 | _ = None -575 575 | -576 |- -577 576 | # arbitrary comment -578 577 | def inner(): # E306 not expected (pycodestyle detects E306) -579 578 | pass +596 596 | def fn(): +597 597 | _ = None +598 598 | +599 |- +600 599 | # arbitrary comment +601 600 | def inner(): # E306 not expected (pycodestyle detects E306) +602 601 | pass -E30.py:588:1: E303 [*] Too many blank lines (3) +E30.py:611:1: E303 [*] Too many blank lines (3) | -588 | print() +611 | print() | ^^^^^ E303 -589 | # end +612 | # end | = help: Remove extraneous blank line(s) ℹ Safe fix -584 584 | print() -585 585 | -586 586 | -587 |- -588 587 | print() -589 588 | # end -590 589 | +607 607 | print() +608 608 | +609 609 | +610 |- +611 610 | print() +612 611 | # end +613 612 | -E30.py:597:1: E303 [*] Too many blank lines (3) +E30.py:620:1: E303 [*] Too many blank lines (3) | -597 | # comment +620 | # comment | ^^^^^^^^^ E303 -598 | -599 | print() +621 | +622 | print() | = help: Remove extraneous blank line(s) ℹ Safe fix -593 593 | print() -594 594 | -595 595 | -596 |- -597 596 | # comment -598 597 | -599 598 | print() +616 616 | print() +617 617 | +618 618 | +619 |- +620 619 | # comment +621 620 | +622 621 | print() -E30.py:608:5: E303 [*] Too many blank lines (2) +E30.py:631:5: E303 [*] Too many blank lines (2) | -608 | # comment +631 | # comment | ^^^^^^^^^ E303 | = help: Remove extraneous blank line(s) ℹ Safe fix -604 604 | def a(): -605 605 | print() -606 606 | -607 |- -608 607 | # comment -609 608 | -610 609 | +627 627 | def a(): +628 628 | print() +629 629 | +630 |- +631 630 | # comment +632 631 | +633 632 | -E30.py:611:5: E303 [*] Too many blank lines (2) +E30.py:634:5: E303 [*] Too many blank lines (2) | -611 | # another comment +634 | # another comment | ^^^^^^^^^^^^^^^^^ E303 -612 | -613 | print() +635 | +636 | print() | = help: Remove extraneous blank line(s) ℹ Safe fix -607 607 | -608 608 | # comment -609 609 | -610 |- -611 610 | # another comment -612 611 | -613 612 | print() +630 630 | +631 631 | # comment +632 632 | +633 |- +634 633 | # another comment +635 634 | +636 635 | print() -E30.py:622:1: E303 [*] Too many blank lines (3) +E30.py:645:1: E303 [*] Too many blank lines (3) | -622 | / """This class docstring comes on line 5. -623 | | It gives error E303: too many blank lines (3) -624 | | """ +645 | / """This class docstring comes on line 5. +646 | | It gives error E303: too many blank lines (3) +647 | | """ | |___^ E303 -625 | # end +648 | # end | = help: Remove extraneous blank line(s) ℹ Safe fix -618 618 | #!python -619 619 | -620 620 | -621 |- -622 621 | """This class docstring comes on line 5. -623 622 | It gives error E303: too many blank lines (3) -624 623 | """ +641 641 | #!python +642 642 | +643 643 | +644 |- +645 644 | """This class docstring comes on line 5. +646 645 | It gives error E303: too many blank lines (3) +647 646 | """ -E30.py:634:5: E303 [*] Too many blank lines (2) +E30.py:657:5: E303 [*] Too many blank lines (2) | -634 | def b(self): +657 | def b(self): | ^^^ E303 -635 | pass -636 | # end +658 | pass +659 | # end | = help: Remove extraneous blank line(s) ℹ Safe fix -630 630 | def a(self): -631 631 | pass -632 632 | -633 |- -634 633 | def b(self): -635 634 | pass -636 635 | # end +653 653 | def a(self): +654 654 | pass +655 655 | +656 |- +657 656 | def b(self): +658 657 | pass +659 658 | # end -E30.py:644:5: E303 [*] Too many blank lines (2) +E30.py:667:5: E303 [*] Too many blank lines (2) | -644 | a = 2 +667 | a = 2 | ^ E303 -645 | # end +668 | # end | = help: Remove extraneous blank line(s) ℹ Safe fix -640 640 | if True: -641 641 | a = 1 -642 642 | -643 |- -644 643 | a = 2 -645 644 | # end -646 645 | +663 663 | if True: +664 664 | a = 1 +665 665 | +666 |- +667 666 | a = 2 +668 667 | # end +669 668 | -E30.py:652:5: E303 [*] Too many blank lines (2) +E30.py:675:5: E303 [*] Too many blank lines (2) | -652 | # comment +675 | # comment | ^^^^^^^^^ E303 | = help: Remove extraneous blank line(s) ℹ Safe fix -648 648 | # E303 -649 649 | class Test: -650 650 | -651 |- -652 651 | # comment -653 652 | -654 653 | +671 671 | # E303 +672 672 | class Test: +673 673 | +674 |- +675 674 | # comment +676 675 | +677 676 | -E30.py:655:5: E303 [*] Too many blank lines (2) +E30.py:678:5: E303 [*] Too many blank lines (2) | -655 | # another comment +678 | # another comment | ^^^^^^^^^^^^^^^^^ E303 -656 | -657 | def test(self): pass +679 | +680 | def test(self): pass | = help: Remove extraneous blank line(s) ℹ Safe fix -651 651 | -652 652 | # comment -653 653 | -654 |- -655 654 | # another comment -656 655 | -657 656 | def test(self): pass +674 674 | +675 675 | # comment +676 676 | +677 |- +678 677 | # another comment +679 678 | +680 679 | def test(self): pass -E30.py:669:5: E303 [*] Too many blank lines (2) +E30.py:692:5: E303 [*] Too many blank lines (2) | -669 | def b(self): +692 | def b(self): | ^^^ E303 -670 | pass -671 | # end +693 | pass +694 | # end | = help: Remove extraneous blank line(s) ℹ Safe fix -665 665 | -666 666 | # wrongly indented comment -667 667 | -668 |- -669 668 | def b(self): -670 669 | pass -671 670 | # end +688 688 | +689 689 | # wrongly indented comment +690 690 | +691 |- +692 691 | def b(self): +693 692 | pass +694 693 | # end -E30.py:679:5: E303 [*] Too many blank lines (2) +E30.py:702:5: E303 [*] Too many blank lines (2) | -679 | pass +702 | pass | ^^^^ E303 -680 | # end +703 | # end | = help: Remove extraneous blank line(s) ℹ Safe fix -675 675 | def fn(): -676 676 | pass -677 677 | -678 |- -679 678 | pass -680 679 | # end -681 680 | +698 698 | def fn(): +699 699 | pass +700 700 | +701 |- +702 701 | pass +703 702 | # end +704 703 | 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 b9dfe55a59a23b..e7b76e8f2dd1be 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,65 +1,65 @@ --- source: crates/ruff_linter/src/rules/pycodestyle/mod.rs --- -E30.py:686:1: E304 [*] Blank lines found after function decorator (1) +E30.py:709:1: E304 [*] Blank lines found after function decorator (1) | -684 | @decorator -685 | -686 | def function(): +707 | @decorator +708 | +709 | def function(): | ^^^ E304 -687 | pass -688 | # end +710 | pass +711 | # end | = help: Remove extraneous blank line(s) ℹ Safe fix -682 682 | -683 683 | # E304 -684 684 | @decorator -685 |- -686 685 | def function(): -687 686 | pass -688 687 | # end +705 705 | +706 706 | # E304 +707 707 | @decorator +708 |- +709 708 | def function(): +710 709 | pass +711 710 | # end -E30.py:695:1: E304 [*] Blank lines found after function decorator (1) +E30.py:718:1: E304 [*] Blank lines found after function decorator (1) | -694 | # comment E304 not expected -695 | def function(): +717 | # comment E304 not expected +718 | def function(): | ^^^ E304 -696 | pass -697 | # end +719 | pass +720 | # end | = help: Remove extraneous blank line(s) ℹ Safe fix -690 690 | -691 691 | # E304 -692 692 | @decorator -693 |- -694 693 | # comment E304 not expected -695 694 | def function(): -696 695 | pass +713 713 | +714 714 | # E304 +715 715 | @decorator +716 |- +717 716 | # comment E304 not expected +718 717 | def function(): +719 718 | pass -E30.py:707:1: E304 [*] Blank lines found after function decorator (2) +E30.py:730:1: E304 [*] Blank lines found after function decorator (2) | -706 | # second comment E304 not expected -707 | def function(): +729 | # second comment E304 not expected +730 | def function(): | ^^^ E304 -708 | pass -709 | # end +731 | pass +732 | # end | = help: Remove extraneous blank line(s) ℹ Safe fix -699 699 | -700 700 | # E304 -701 701 | @decorator -702 |- -703 702 | # comment E304 not expected -704 |- -705 |- -706 703 | # second comment E304 not expected -707 704 | def function(): -708 705 | pass +722 722 | +723 723 | # E304 +724 724 | @decorator +725 |- +726 725 | # comment E304 not expected +727 |- +728 |- +729 726 | # second comment E304 not expected +730 727 | def function(): +731 728 | pass 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 50df5d638e11a4..a2bfb86dcdfe34 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:719:1: E305 [*] Expected 2 blank lines after class or function definition, found (1) +E30.py:742:1: E305 [*] Expected 2 blank lines after class or function definition, found (1) | -718 | # another comment -719 | fn() +741 | # another comment +742 | fn() | ^^ E305 -720 | # end +743 | # end | = help: Add missing blank line(s) ℹ Safe fix -716 716 | # comment -717 717 | -718 718 | # another comment - 719 |+ - 720 |+ -719 721 | fn() -720 722 | # end -721 723 | +739 739 | # comment +740 740 | +741 741 | # another comment + 742 |+ + 743 |+ +742 744 | fn() +743 745 | # end +744 746 | -E30.py:730:1: E305 [*] Expected 2 blank lines after class or function definition, found (1) +E30.py:753:1: E305 [*] Expected 2 blank lines after class or function definition, found (1) | -729 | # another comment -730 | a = 1 +752 | # another comment +753 | a = 1 | ^ E305 -731 | # end +754 | # end | = help: Add missing blank line(s) ℹ Safe fix -727 727 | # comment -728 728 | -729 729 | # another comment - 730 |+ - 731 |+ -730 732 | a = 1 -731 733 | # end -732 734 | +750 750 | # comment +751 751 | +752 752 | # another comment + 753 |+ + 754 |+ +753 755 | a = 1 +754 756 | # end +755 757 | -E30.py:742:1: E305 [*] Expected 2 blank lines after class or function definition, found (1) +E30.py:765:1: E305 [*] Expected 2 blank lines after class or function definition, found (1) | -740 | # another comment -741 | -742 | try: +763 | # another comment +764 | +765 | try: | ^^^ E305 -743 | fn() -744 | except Exception: +766 | fn() +767 | except Exception: | = help: Add missing blank line(s) ℹ Safe fix -739 739 | -740 740 | # another comment -741 741 | - 742 |+ -742 743 | try: -743 744 | fn() -744 745 | except Exception: +762 762 | +763 763 | # another comment +764 764 | + 765 |+ +765 766 | try: +766 767 | fn() +767 768 | except Exception: -E30.py:754:1: E305 [*] Expected 2 blank lines after class or function definition, found (1) +E30.py:777:1: E305 [*] Expected 2 blank lines after class or function definition, found (1) | -753 | # Two spaces before comments, too. -754 | if a(): +776 | # Two spaces before comments, too. +777 | if a(): | ^^ E305 -755 | a() -756 | # end +778 | a() +779 | # end | = help: Add missing blank line(s) ℹ Safe fix -751 751 | print() -752 752 | -753 753 | # Two spaces before comments, too. - 754 |+ - 755 |+ -754 756 | if a(): -755 757 | a() -756 758 | # end +774 774 | print() +775 775 | +776 776 | # Two spaces before comments, too. + 777 |+ + 778 |+ +777 779 | if a(): +778 780 | a() +779 781 | # end -E30.py:767:1: E305 [*] Expected 2 blank lines after class or function definition, found (1) +E30.py:790:1: E305 [*] Expected 2 blank lines after class or function definition, found (1) | -765 | blah, blah -766 | -767 | if __name__ == '__main__': +788 | blah, blah +789 | +790 | if __name__ == '__main__': | ^^ E305 -768 | main() -769 | # end +791 | main() +792 | # end | = help: Add missing blank line(s) ℹ Safe fix -764 764 | def main(): -765 765 | blah, blah -766 766 | - 767 |+ -767 768 | if __name__ == '__main__': -768 769 | main() -769 770 | # end +787 787 | def main(): +788 788 | blah, blah +789 789 | + 790 |+ +790 791 | if __name__ == '__main__': +791 792 | main() +792 793 | # 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 05bc3788a22898..34bbe343e8f2bd 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,223 +1,223 @@ --- source: crates/ruff_linter/src/rules/pycodestyle/mod.rs --- -E30.py:775:5: E306 [*] Expected 1 blank line before a nested definition, found 0 +E30.py:798:5: E306 [*] Expected 1 blank line before a nested definition, found 0 | -773 | def a(): -774 | x = 1 -775 | def b(): +796 | def a(): +797 | x = 1 +798 | def b(): | ^^^ E306 -776 | pass -777 | # end +799 | pass +800 | # end | = help: Add missing blank line ℹ Safe fix -772 772 | # E306:3:5 -773 773 | def a(): -774 774 | x = 1 - 775 |+ -775 776 | def b(): -776 777 | pass -777 778 | # end - -E30.py:783:5: E306 [*] Expected 1 blank line before a nested definition, found 0 - | -781 | async def a(): -782 | x = 1 -783 | def b(): +795 795 | # E306:3:5 +796 796 | def a(): +797 797 | x = 1 + 798 |+ +798 799 | def b(): +799 800 | pass +800 801 | # end + +E30.py:806:5: E306 [*] Expected 1 blank line before a nested definition, found 0 + | +804 | async def a(): +805 | x = 1 +806 | def b(): | ^^^ E306 -784 | pass -785 | # end +807 | pass +808 | # end | = help: Add missing blank line ℹ Safe fix -780 780 | #: E306:3:5 -781 781 | async def a(): -782 782 | x = 1 - 783 |+ -783 784 | def b(): -784 785 | pass -785 786 | # end - -E30.py:791:5: E306 [*] Expected 1 blank line before a nested definition, found 0 - | -789 | def a(): -790 | x = 2 -791 | def b(): +803 803 | #: E306:3:5 +804 804 | async def a(): +805 805 | x = 1 + 806 |+ +806 807 | def b(): +807 808 | pass +808 809 | # end + +E30.py:814:5: E306 [*] Expected 1 blank line before a nested definition, found 0 + | +812 | def a(): +813 | x = 2 +814 | def b(): | ^^^ E306 -792 | x = 1 -793 | def c(): +815 | x = 1 +816 | def c(): | = help: Add missing blank line ℹ Safe fix -788 788 | #: E306:3:5 E306:5:9 -789 789 | def a(): -790 790 | x = 2 - 791 |+ -791 792 | def b(): -792 793 | x = 1 -793 794 | def c(): - -E30.py:793:9: E306 [*] Expected 1 blank line before a nested definition, found 0 - | -791 | def b(): -792 | x = 1 -793 | def c(): +811 811 | #: E306:3:5 E306:5:9 +812 812 | def a(): +813 813 | x = 2 + 814 |+ +814 815 | def b(): +815 816 | x = 1 +816 817 | def c(): + +E30.py:816:9: E306 [*] Expected 1 blank line before a nested definition, found 0 + | +814 | def b(): +815 | x = 1 +816 | def c(): | ^^^ E306 -794 | pass -795 | # end +817 | pass +818 | # end | = help: Add missing blank line ℹ Safe fix -790 790 | x = 2 -791 791 | def b(): -792 792 | x = 1 - 793 |+ -793 794 | def c(): -794 795 | pass -795 796 | # end - -E30.py:801:5: E306 [*] Expected 1 blank line before a nested definition, found 0 - | -799 | def a(): -800 | x = 1 -801 | class C: +813 813 | x = 2 +814 814 | def b(): +815 815 | x = 1 + 816 |+ +816 817 | def c(): +817 818 | pass +818 819 | # end + +E30.py:824:5: E306 [*] Expected 1 blank line before a nested definition, found 0 + | +822 | def a(): +823 | x = 1 +824 | class C: | ^^^^^ E306 -802 | pass -803 | x = 2 +825 | pass +826 | x = 2 | = help: Add missing blank line ℹ Safe fix -798 798 | # E306:3:5 E306:6:5 -799 799 | def a(): -800 800 | x = 1 - 801 |+ -801 802 | class C: -802 803 | pass -803 804 | x = 2 - -E30.py:804:5: E306 [*] Expected 1 blank line before a nested definition, found 0 - | -802 | pass -803 | x = 2 -804 | def b(): +821 821 | # E306:3:5 E306:6:5 +822 822 | def a(): +823 823 | x = 1 + 824 |+ +824 825 | class C: +825 826 | pass +826 827 | x = 2 + +E30.py:827:5: E306 [*] Expected 1 blank line before a nested definition, found 0 + | +825 | pass +826 | x = 2 +827 | def b(): | ^^^ E306 -805 | pass -806 | # end +828 | pass +829 | # end | = help: Add missing blank line ℹ Safe fix -801 801 | class C: -802 802 | pass -803 803 | x = 2 - 804 |+ -804 805 | def b(): -805 806 | pass -806 807 | # end - -E30.py:813:5: E306 [*] Expected 1 blank line before a nested definition, found 0 - | -811 | def bar(): -812 | pass -813 | def baz(): pass +824 824 | class C: +825 825 | pass +826 826 | x = 2 + 827 |+ +827 828 | def b(): +828 829 | pass +829 830 | # end + +E30.py:836:5: E306 [*] Expected 1 blank line before a nested definition, found 0 + | +834 | def bar(): +835 | pass +836 | def baz(): pass | ^^^ E306 -814 | # end +837 | # end | = help: Add missing blank line ℹ Safe fix -810 810 | def foo(): -811 811 | def bar(): -812 812 | pass - 813 |+ -813 814 | def baz(): pass -814 815 | # end -815 816 | - -E30.py:820:5: E306 [*] Expected 1 blank line before a nested definition, found 0 - | -818 | def foo(): -819 | def bar(): pass -820 | def baz(): +833 833 | def foo(): +834 834 | def bar(): +835 835 | pass + 836 |+ +836 837 | def baz(): pass +837 838 | # end +838 839 | + +E30.py:843:5: E306 [*] Expected 1 blank line before a nested definition, found 0 + | +841 | def foo(): +842 | def bar(): pass +843 | def baz(): | ^^^ E306 -821 | pass -822 | # end +844 | pass +845 | # end | = help: Add missing blank line ℹ Safe fix -817 817 | # E306:3:5 -818 818 | def foo(): -819 819 | def bar(): pass - 820 |+ -820 821 | def baz(): -821 822 | pass -822 823 | # end - -E30.py:828:5: E306 [*] Expected 1 blank line before a nested definition, found 0 - | -826 | def a(): -827 | x = 2 -828 | @decorator +840 840 | # E306:3:5 +841 841 | def foo(): +842 842 | def bar(): pass + 843 |+ +843 844 | def baz(): +844 845 | pass +845 846 | # end + +E30.py:851:5: E306 [*] Expected 1 blank line before a nested definition, found 0 + | +849 | def a(): +850 | x = 2 +851 | @decorator | ^ E306 -829 | def b(): -830 | pass +852 | def b(): +853 | pass | = help: Add missing blank line ℹ Safe fix -825 825 | # E306 -826 826 | def a(): -827 827 | x = 2 - 828 |+ -828 829 | @decorator -829 830 | def b(): -830 831 | pass - -E30.py:837:5: E306 [*] Expected 1 blank line before a nested definition, found 0 - | -835 | def a(): -836 | x = 2 -837 | @decorator +848 848 | # E306 +849 849 | def a(): +850 850 | x = 2 + 851 |+ +851 852 | @decorator +852 853 | def b(): +853 854 | pass + +E30.py:860:5: E306 [*] Expected 1 blank line before a nested definition, found 0 + | +858 | def a(): +859 | x = 2 +860 | @decorator | ^ E306 -838 | async def b(): -839 | pass +861 | async def b(): +862 | pass | = help: Add missing blank line ℹ Safe fix -834 834 | # E306 -835 835 | def a(): -836 836 | x = 2 - 837 |+ -837 838 | @decorator -838 839 | async def b(): -839 840 | pass - -E30.py:846:5: E306 [*] Expected 1 blank line before a nested definition, found 0 - | -844 | def a(): -845 | x = 2 -846 | async def b(): +857 857 | # E306 +858 858 | def a(): +859 859 | x = 2 + 860 |+ +860 861 | @decorator +861 862 | async def b(): +862 863 | pass + +E30.py:869:5: E306 [*] Expected 1 blank line before a nested definition, found 0 + | +867 | def a(): +868 | x = 2 +869 | async def b(): | ^^^^^ E306 -847 | pass -848 | # end +870 | pass +871 | # end | = help: Add missing blank line ℹ Safe fix -843 843 | # E306 -844 844 | def a(): -845 845 | x = 2 - 846 |+ -846 847 | async def b(): -847 848 | pass -848 849 | # end +866 866 | # E306 +867 867 | def a(): +868 868 | x = 2 + 869 |+ +869 870 | async def b(): +870 871 | pass +871 872 | # end From bc99066bd795986f178302e39e8adc84f6878c32 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Fri, 1 Mar 2024 10:23:36 +0100 Subject: [PATCH 4/4] Use `self.follows` instead of `state` --- crates/ruff_linter/src/rules/pycodestyle/rules/blank_lines.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/ruff_linter/src/rules/pycodestyle/rules/blank_lines.rs b/crates/ruff_linter/src/rules/pycodestyle/rules/blank_lines.rs index 9cbbb556f56b3d..c1a95551d70407 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/rules/blank_lines.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/rules/blank_lines.rs @@ -660,8 +660,8 @@ impl BlankLinesChecker { // Only applies to methods. && matches!(line.kind, LogicalLineKind::Function | LogicalLineKind::Decorator) // Allow groups of one-liners. - && !(matches!(state.follows, Follows::Def) && !matches!(line.last_token, TokenKind::Colon)) - && matches!(state.class_status, Status::Inside(_)) + && !(matches!(self.follows, Follows::Def) && !matches!(line.last_token, TokenKind::Colon)) + && matches!(self.class_status, Status::Inside(_)) // The class/parent method's docstring can directly precede the def. // Allow following a decorator (if there is an error it will be triggered on the first decorator). && !matches!(self.follows, Follows::Docstring | Follows::Decorator)