Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pycodestyle] Add blank line(s) rules (E301, E302, E303, E304, E305, E306) #4694

Closed
wants to merge 49 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
7a1e782
Added test fixture.
hoel-bagard May 25, 2023
569dae9
Start working on blank lines using logical lines. Added violations.
hoel-bagard May 25, 2023
119520d
Began looking at logic.
hoel-bagard May 25, 2023
7667d39
Began looking at the logic.
hoel-bagard May 25, 2023
e7f28ac
Removed debugs
hoel-bagard May 25, 2023
41cddde
Minor cleanup.
hoel-bagard May 25, 2023
47788de
Add blank line rules to KNOWN_FORMATTING_VIOLATIONS
hoel-bagard May 25, 2023
3ee9186
Fixed errors created by not ignoring empty lines anymore.
hoel-bagard May 26, 2023
8f60ac9
Fixed parser test expectation for E302 since support is added for it.
hoel-bagard May 26, 2023
42c4737
Add snapshot for E304.
hoel-bagard May 26, 2023
4bf7326
Count blank characters and use them to remove lines in order to handl…
hoel-bagard May 26, 2023
e082747
Finished E303.
hoel-bagard May 27, 2023
4f369cc
Added E306
hoel-bagard May 27, 2023
e1c3df0
Added is_in_class variable.
hoel-bagard May 27, 2023
ccd7468
Added E301.
hoel-bagard May 27, 2023
44b146c
Added E302.
hoel-bagard May 28, 2023
6ba1bb7
Added E305.
hoel-bagard May 28, 2023
d20fd0c
Linting fixes.
hoel-bagard May 28, 2023
0dc80d5
Put variables used to track the current state into a struct.
hoel-bagard May 28, 2023
95cb064
Added clippy allow
hoel-bagard May 28, 2023
a8242c1
Add rules to ruff.schema.json
hoel-bagard May 28, 2023
210940a
Put errors in order.
hoel-bagard May 28, 2023
00cfdce
Fixed E303
hoel-bagard May 28, 2023
6183a1c
Add E301 snapshot
hoel-bagard May 28, 2023
e2923e0
Add E302 snapshot
hoel-bagard May 28, 2023
db678a8
Add E305 snapshot
hoel-bagard May 28, 2023
5c4b75b
Add E306 snapshot
hoel-bagard May 28, 2023
88a1fa4
Add E303 snapshot
hoel-bagard May 28, 2023
c4d4713
Fixed E302. Fixed is_in_class and is_in_fn being updated too late.
hoel-bagard May 28, 2023
4b0933f
Updated snapshots.
hoel-bagard May 28, 2023
0b74e07
Use breaks instead of returns to limit code repetition.
hoel-bagard May 28, 2023
c7c6420
Merge branch 'main' into add_blank_lines_E30
hoel-bagard May 28, 2023
f63ea5b
Do not enter the indention check for empty lines.
hoel-bagard May 28, 2023
fa3a30b
Sorted KNOWN_FORMATTING_VIOLATIONS
hoel-bagard May 28, 2023
47063d4
Count number of chars instead of number of bytes.
hoel-bagard May 30, 2023
2b4e959
Use Fix::automatic
hoel-bagard May 30, 2023
2fc94f8
Fixed some titles/docstring.
hoel-bagard May 30, 2023
da9d3ee
Updated the snapshots to reflect 2b4e959
hoel-bagard May 30, 2023
57b320c
Merge branch 'main' into add_blank_lines_E30
charliermarsh Jun 13, 2023
4cf44a3
Update fixtures
charliermarsh Jun 13, 2023
6e8c1dc
One violation per line
charliermarsh Jun 13, 2023
0b6e962
Track the number of preceding blank lines in the logical lines builde…
hoel-bagard Jun 16, 2023
53e94f0
Fix E302 not taking into account nested functions.
hoel-bagard Jun 29, 2023
071849e
Ignore comment only lines.
hoel-bagard Jul 6, 2023
75b11a9
Fix E302 not detecting issues with classes
hoel-bagard Jul 6, 2023
9f74835
Revert "Ignore comment only lines."
hoel-bagard Jul 7, 2023
10c9571
Update E30 fixture
hoel-bagard Aug 22, 2023
a12a57c
Fix false positive 301 on def following @ and class definition (E306).
hoel-bagard Aug 26, 2023
3072418
Fix non-comment related E302 errors.
hoel-bagard Aug 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
568 changes: 568 additions & 0 deletions crates/ruff/resources/test/fixtures/pycodestyle/E30.py

Large diffs are not rendered by default.

20 changes: 16 additions & 4 deletions crates/ruff/src/checkers/logical_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ use ruff_python_ast::token_kind::TokenKind;

use crate::registry::{AsRule, Rule};
use crate::rules::pycodestyle::rules::logical_lines::{
extraneous_whitespace, indentation, missing_whitespace, missing_whitespace_after_keyword,
missing_whitespace_around_operator, space_around_operator, whitespace_around_keywords,
whitespace_around_named_parameter_equals, whitespace_before_comment,
whitespace_before_parameters, LogicalLines, TokenFlags,
blank_lines, extraneous_whitespace, indentation, missing_whitespace,
missing_whitespace_after_keyword, missing_whitespace_around_operator, space_around_operator,
whitespace_around_keywords, whitespace_around_named_parameter_equals,
whitespace_before_comment, whitespace_before_parameters, BlankLinesTrackingVars, LogicalLines,
TokenFlags,
};
use crate::settings::Settings;

Expand Down Expand Up @@ -49,6 +50,7 @@ pub(crate) fn check_logical_lines(
let should_fix_whitespace_before_punctuation =
settings.rules.should_fix(Rule::WhitespaceBeforePunctuation);

let mut blank_lines_tracking_vars = BlankLinesTrackingVars::default();
let mut prev_line = None;
let mut prev_indent_level = None;
let indent_char = stylist.indentation().as_char();
Expand Down Expand Up @@ -119,6 +121,16 @@ pub(crate) fn check_logical_lines(
}
}

blank_lines(
&line,
prev_line.as_ref(),
&mut blank_lines_tracking_vars,
indent_level,
locator,
stylist,
&mut context,
);

if !line.is_comment_only() {
prev_line = Some(line);
prev_indent_level = Some(indent_level);
Expand Down
6 changes: 6 additions & 0 deletions crates/ruff/src/codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
(Pycodestyle, "E273") => (RuleGroup::Nursery, rules::pycodestyle::rules::logical_lines::TabAfterKeyword),
(Pycodestyle, "E274") => (RuleGroup::Nursery, rules::pycodestyle::rules::logical_lines::TabBeforeKeyword),
(Pycodestyle, "E275") => (RuleGroup::Nursery, rules::pycodestyle::rules::logical_lines::MissingWhitespaceAfterKeyword),
(Pycodestyle, "E301") => (RuleGroup::Nursery, rules::pycodestyle::rules::logical_lines::BlankLineBetweenMethods),
(Pycodestyle, "E302") => (RuleGroup::Nursery, rules::pycodestyle::rules::logical_lines::BlankLinesTopLevel),
(Pycodestyle, "E303") => (RuleGroup::Nursery, rules::pycodestyle::rules::logical_lines::TooManyBlankLines),
(Pycodestyle, "E304") => (RuleGroup::Nursery, rules::pycodestyle::rules::logical_lines::BlankLineAfterDecorator),
(Pycodestyle, "E305") => (RuleGroup::Nursery, rules::pycodestyle::rules::logical_lines::BlankLinesAfterFunctionOrClass),
(Pycodestyle, "E306") => (RuleGroup::Nursery, rules::pycodestyle::rules::logical_lines::BlankLinesBeforeNestedDefinition),
(Pycodestyle, "E401") => (RuleGroup::Unspecified, rules::pycodestyle::rules::MultipleImportsOnOneLine),
(Pycodestyle, "E402") => (RuleGroup::Unspecified, rules::pycodestyle::rules::ModuleImportNotAtTopOfFile),
(Pycodestyle, "E501") => (RuleGroup::Unspecified, rules::pycodestyle::rules::LineTooLong),
Expand Down
4 changes: 4 additions & 0 deletions crates/ruff/src/flake8_to_ruff/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ mod tests {
pattern: "examples/*".to_string(),
prefix: codes::Pyflakes::_841.into(),
},
PatternPrefixPair {
pattern: "*.pyi".to_string(),
prefix: codes::Pycodestyle::E302.into(),
},
];
assert_eq!(actual, expected);

Expand Down
8 changes: 7 additions & 1 deletion crates/ruff/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,12 @@ impl Rule {
Rule::IOError => LintSource::Io,
Rule::UnsortedImports | Rule::MissingRequiredImport => LintSource::Imports,
Rule::ImplicitNamespacePackage | Rule::InvalidModuleName => LintSource::Filesystem,
Rule::IndentationWithInvalidMultiple
Rule::BlankLineAfterDecorator
| Rule::BlankLineBetweenMethods
| Rule::BlankLinesAfterFunctionOrClass
| Rule::BlankLinesBeforeNestedDefinition
| Rule::BlankLinesTopLevel
| Rule::IndentationWithInvalidMultiple
| Rule::IndentationWithInvalidMultipleComment
| Rule::MissingWhitespace
| Rule::MissingWhitespaceAfterKeyword
Expand All @@ -334,6 +339,7 @@ impl Rule {
| Rule::TabBeforeKeyword
| Rule::TabBeforeOperator
| Rule::TooFewSpacesBeforeInlineComment
| Rule::TooManyBlankLines
| Rule::UnexpectedIndentation
| Rule::UnexpectedIndentationComment
| Rule::UnexpectedSpacesAroundKeywordParameterEquals
Expand Down
6 changes: 6 additions & 0 deletions crates/ruff/src/rules/pycodestyle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ mod tests {
Path::new("E25.py")
)]
#[test_case(Rule::MissingWhitespaceAroundParameterEquals, Path::new("E25.py"))]
#[test_case(Rule::BlankLineBetweenMethods, Path::new("E30.py"))]
#[test_case(Rule::BlankLinesTopLevel, Path::new("E30.py"))]
#[test_case(Rule::TooManyBlankLines, Path::new("E30.py"))]
#[test_case(Rule::BlankLineAfterDecorator, Path::new("E30.py"))]
#[test_case(Rule::BlankLinesAfterFunctionOrClass, Path::new("E30.py"))]
#[test_case(Rule::BlankLinesBeforeNestedDefinition, Path::new("E30.py"))]
fn logical(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path(
Expand Down
Loading