-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[flake8-class-newline
] Add new flake8 plugin.
#10033
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
21e5687
add class_newline rule
augustelalande 7762e7c
Merge branch 'main' into class_newline
augustelalande 4b2c194
implement MissingClassNewLine
augustelalande 8527cf0
create test cases
augustelalande 166d7ef
update schema
augustelalande 9607dd5
add class_newline snapshot
augustelalande a7b2423
add rule description
augustelalande ee3e433
update license
augustelalande 1939eed
linting
augustelalande bed876c
Merge branch 'main' into class_newline
augustelalande 4282d15
exclude stub files from rule
augustelalande File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
crates/ruff_linter/resources/test/fixtures/flake8_class_newline/CNL100.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class A: # CNL100 | ||
def __init__(self): | ||
pass | ||
|
||
|
||
class B: # correct | ||
|
||
def __init__(self): | ||
pass | ||
|
||
|
||
class C: # CNL100 | ||
async def foo(self): | ||
pass | ||
|
||
|
||
class D: # correct | ||
|
||
async def foo(self): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//! Rules from [flake8-class-newline](https://pypi.org/project/flake8-class-newline/). | ||
pub(crate) mod rules; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::path::Path; | ||
|
||
use anyhow::Result; | ||
use test_case::test_case; | ||
|
||
use crate::registry::Rule; | ||
use crate::test::test_path; | ||
use crate::{assert_messages, settings}; | ||
|
||
#[test_case(Path::new("CNL100.py"))] | ||
fn rules(path: &Path) -> Result<()> { | ||
let snapshot = path.to_string_lossy().into_owned(); | ||
let diagnostics = test_path( | ||
Path::new("flake8_class_newline").join(path).as_path(), | ||
&settings::LinterSettings::for_rule(Rule::MissingClassNewLine), | ||
)?; | ||
assert_messages!(snapshot, diagnostics); | ||
Ok(()) | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
crates/ruff_linter/src/rules/flake8_class_newline/rules/missing_class_newline.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
use ruff_diagnostics::AlwaysFixableViolation; | ||
use ruff_diagnostics::Diagnostic; | ||
use ruff_diagnostics::Edit; | ||
use ruff_diagnostics::Fix; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_codegen::Stylist; | ||
use ruff_python_parser::lexer::LexResult; | ||
use ruff_source_file::Locator; | ||
|
||
use crate::line_width::IndentWidth; | ||
use crate::rules::pycodestyle::rules::{LinePreprocessor, LogicalLineInfo, LogicalLineKind}; | ||
|
||
/// ## What it does | ||
/// Checks for a missing blank line between a class definition and its first method. | ||
/// | ||
/// ## Why is this bad? | ||
/// PEP8 says we should surround every class method with a single blank line. | ||
/// However it is ambiguous about the first method having a blank line above it. | ||
/// This rule enforces a single blank line, for consistency across classes. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// class MyClass(object): | ||
/// def func1(): | ||
/// pass | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// class MyClass(object): | ||
/// | ||
/// def func1(): | ||
/// pass | ||
/// ``` | ||
/// | ||
/// ## References | ||
/// - [PEP 8](https://peps.python.org/pep-0008/#blank-lines) | ||
#[violation] | ||
pub struct MissingClassNewLine; | ||
|
||
impl AlwaysFixableViolation for MissingClassNewLine { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Expected 1 blank line after class declaration, found 0") | ||
} | ||
|
||
fn fix_title(&self) -> String { | ||
"Add missing blank line".to_string() | ||
} | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, Default)] | ||
enum Follows { | ||
#[default] | ||
Class, | ||
Other, | ||
} | ||
|
||
/// Contains variables used for the linting of blank lines. | ||
#[derive(Debug, Default)] | ||
pub(crate) struct BlankLinesChecker { | ||
follows: Follows, | ||
} | ||
|
||
impl BlankLinesChecker { | ||
pub(crate) fn check_lines( | ||
&mut self, | ||
tokens: &[LexResult], | ||
locator: &Locator, | ||
stylist: &Stylist, | ||
indent_width: IndentWidth, | ||
diagnostics: &mut Vec<Diagnostic>, | ||
) { | ||
let line_preprocessor = LinePreprocessor::new(tokens, locator, indent_width); | ||
|
||
for logical_line in line_preprocessor { | ||
self.check_new_line_after_class_declaration( | ||
&logical_line, | ||
locator, | ||
stylist, | ||
diagnostics, | ||
); | ||
} | ||
} | ||
|
||
fn check_new_line_after_class_declaration( | ||
&mut self, | ||
line: &LogicalLineInfo, | ||
locator: &Locator, | ||
stylist: &Stylist, | ||
diagnostics: &mut Vec<Diagnostic>, | ||
) { | ||
if (matches!(self.follows, Follows::Class) | ||
&& matches!( | ||
line.kind, | ||
LogicalLineKind::Function | LogicalLineKind::Decorator | ||
) | ||
&& line.preceding_blank_lines == 0) | ||
{ | ||
let mut diagnostic = Diagnostic::new(MissingClassNewLine, line.first_token_range); | ||
diagnostic.set_fix(Fix::safe_edit(Edit::insertion( | ||
stylist.line_ending().to_string(), | ||
locator.line_start(line.first_token_range.start()), | ||
))); | ||
|
||
diagnostics.push(diagnostic); | ||
} | ||
|
||
// Update the `self.follows` state based on the current line | ||
match line.kind { | ||
LogicalLineKind::Class => self.follows = Follows::Class, | ||
_ => self.follows = Follows::Other, | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
crates/ruff_linter/src/rules/flake8_class_newline/rules/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub(crate) use missing_class_newline::*; | ||
|
||
mod missing_class_newline; |
38 changes: 38 additions & 0 deletions
38
...8_class_newline/snapshots/ruff_linter__rules__flake8_class_newline__tests__CNL100.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_class_newline/mod.rs | ||
--- | ||
CNL100.py:2:5: CNL100 [*] Expected 1 blank line after class declaration, found 0 | ||
| | ||
1 | class A: # CNL100 | ||
2 | def __init__(self): | ||
| ^^^ CNL100 | ||
3 | pass | ||
| | ||
= help: Add missing blank line | ||
|
||
ℹ Safe fix | ||
1 1 | class A: # CNL100 | ||
2 |+ | ||
2 3 | def __init__(self): | ||
3 4 | pass | ||
4 5 | | ||
|
||
CNL100.py:13:5: CNL100 [*] Expected 1 blank line after class declaration, found 0 | ||
| | ||
12 | class C: # CNL100 | ||
13 | async def foo(self): | ||
| ^^^^^ CNL100 | ||
14 | pass | ||
| | ||
= help: Add missing blank line | ||
|
||
ℹ Safe fix | ||
10 10 | | ||
11 11 | | ||
12 12 | class C: # CNL100 | ||
13 |+ | ||
13 14 | async def foo(self): | ||
14 15 | pass | ||
15 16 | | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should exclude
pyi
files from this rule because empty lines in stub files should only be used for grouping methods.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done