Split pycodestyle import rules into separate files (#9600)

This commit is contained in:
Charlie Marsh 2024-01-21 14:30:00 -05:00 committed by GitHub
parent 9e5f3f1b1b
commit b64aa1e86d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 48 additions and 41 deletions

View file

@ -6,14 +6,14 @@ pub(crate) use compound_statements::*;
pub(crate) use doc_line_too_long::*; pub(crate) use doc_line_too_long::*;
pub(crate) use errors::*; pub(crate) use errors::*;
pub use errors::{IOError, SyntaxError}; pub use errors::{IOError, SyntaxError};
pub(crate) use imports::*;
pub(crate) use invalid_escape_sequence::*; pub(crate) use invalid_escape_sequence::*;
pub(crate) use lambda_assignment::*; pub(crate) use lambda_assignment::*;
pub(crate) use line_too_long::*; pub(crate) use line_too_long::*;
pub(crate) use literal_comparisons::*; pub(crate) use literal_comparisons::*;
pub(crate) use missing_newline_at_end_of_file::*; pub(crate) use missing_newline_at_end_of_file::*;
pub(crate) use mixed_spaces_and_tabs::*; pub(crate) use mixed_spaces_and_tabs::*;
pub(crate) use module_import_not_at_top_of_file::*;
pub(crate) use multiple_imports_on_one_line::*;
pub(crate) use not_tests::*; pub(crate) use not_tests::*;
pub(crate) use tab_indentation::*; pub(crate) use tab_indentation::*;
pub(crate) use trailing_whitespace::*; pub(crate) use trailing_whitespace::*;
@ -26,7 +26,6 @@ mod bare_except;
mod compound_statements; mod compound_statements;
mod doc_line_too_long; mod doc_line_too_long;
mod errors; mod errors;
mod imports;
mod invalid_escape_sequence; mod invalid_escape_sequence;
mod lambda_assignment; mod lambda_assignment;
mod line_too_long; mod line_too_long;
@ -34,6 +33,8 @@ mod literal_comparisons;
pub(crate) mod logical_lines; pub(crate) mod logical_lines;
mod missing_newline_at_end_of_file; mod missing_newline_at_end_of_file;
mod mixed_spaces_and_tabs; mod mixed_spaces_and_tabs;
mod module_import_not_at_top_of_file;
mod multiple_imports_on_one_line;
mod not_tests; mod not_tests;
mod tab_indentation; mod tab_indentation;
mod trailing_whitespace; mod trailing_whitespace;

View file

@ -1,38 +1,10 @@
use ruff_diagnostics::{Diagnostic, Violation}; use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation}; use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::{Alias, PySourceType, Stmt}; use ruff_python_ast::{PySourceType, Stmt};
use ruff_text_size::Ranged; use ruff_text_size::Ranged;
use crate::checkers::ast::Checker; use crate::checkers::ast::Checker;
/// ## What it does
/// Check for multiple imports on one line.
///
/// ## Why is this bad?
/// According to [PEP 8], "imports should usually be on separate lines."
///
/// ## Example
/// ```python
/// import sys, os
/// ```
///
/// Use instead:
/// ```python
/// import os
/// import sys
/// ```
///
/// [PEP 8]: https://peps.python.org/pep-0008/#imports
#[violation]
pub struct MultipleImportsOnOneLine;
impl Violation for MultipleImportsOnOneLine {
#[derive_message_formats]
fn message(&self) -> String {
format!("Multiple imports on one line")
}
}
/// ## What it does /// ## What it does
/// Checks for imports that are not at the top of the file. For Jupyter notebooks, this /// Checks for imports that are not at the top of the file. For Jupyter notebooks, this
/// checks for imports that are not at the top of the cell. /// checks for imports that are not at the top of the cell.
@ -82,15 +54,6 @@ impl Violation for ModuleImportNotAtTopOfFile {
} }
} }
/// E401
pub(crate) fn multiple_imports_on_one_line(checker: &mut Checker, stmt: &Stmt, names: &[Alias]) {
if names.len() > 1 {
checker
.diagnostics
.push(Diagnostic::new(MultipleImportsOnOneLine, stmt.range()));
}
}
/// E402 /// E402
pub(crate) fn module_import_not_at_top_of_file(checker: &mut Checker, stmt: &Stmt) { pub(crate) fn module_import_not_at_top_of_file(checker: &mut Checker, stmt: &Stmt) {
if checker.semantic().seen_import_boundary() && checker.semantic().at_top_level() { if checker.semantic().seen_import_boundary() && checker.semantic().at_top_level() {

View file

@ -0,0 +1,43 @@
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::{Alias, Stmt};
use ruff_text_size::Ranged;
use crate::checkers::ast::Checker;
/// ## What it does
/// Check for multiple imports on one line.
///
/// ## Why is this bad?
/// According to [PEP 8], "imports should usually be on separate lines."
///
/// ## Example
/// ```python
/// import sys, os
/// ```
///
/// Use instead:
/// ```python
/// import os
/// import sys
/// ```
///
/// [PEP 8]: https://peps.python.org/pep-0008/#imports
#[violation]
pub struct MultipleImportsOnOneLine;
impl Violation for MultipleImportsOnOneLine {
#[derive_message_formats]
fn message(&self) -> String {
format!("Multiple imports on one line")
}
}
/// E401
pub(crate) fn multiple_imports_on_one_line(checker: &mut Checker, stmt: &Stmt, names: &[Alias]) {
if names.len() > 1 {
checker
.diagnostics
.push(Diagnostic::new(MultipleImportsOnOneLine, stmt.range()));
}
}