Skip to content

Commit

Permalink
Modify E303 to use top-levelness instead of class/def.
Browse files Browse the repository at this point in the history
  • Loading branch information
hoel-bagard committed Nov 21, 2023
1 parent 46b0c46 commit 3eb5c62
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 248 deletions.
20 changes: 20 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pycodestyle/E30.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 |


Original file line number Diff line number Diff line change
@@ -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


Original file line number Diff line number Diff line change
@@ -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


Loading

0 comments on commit 3eb5c62

Please sign in to comment.