mirror of https://github.com/astral-sh/ruff
Implement E902 (IOError)
This commit is contained in:
parent
45db571935
commit
9097bf5ed6
|
|
@ -122,6 +122,7 @@ lint rules that are obviated by Black (e.g., stylistic rules).
|
||||||
| ---- | ----- | ------- |
|
| ---- | ----- | ------- |
|
||||||
| E402 | ModuleImportNotAtTopOfFile | Module level import not at top of file |
|
| E402 | ModuleImportNotAtTopOfFile | Module level import not at top of file |
|
||||||
| E501 | LineTooLong | Line too long |
|
| E501 | LineTooLong | Line too long |
|
||||||
|
| E902 | IOError | No such file or directory: `...` |
|
||||||
| F401 | UnusedImport | `...` imported but unused |
|
| F401 | UnusedImport | `...` imported but unused |
|
||||||
| F403 | ImportStarUsage | Unable to detect undefined names |
|
| F403 | ImportStarUsage | Unable to detect undefined names |
|
||||||
| F541 | FStringMissingPlaceholders | f-string without any placeholders |
|
| F541 | FStringMissingPlaceholders | f-string without any placeholders |
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ fn main() {
|
||||||
CheckKind::DuplicateArgumentName,
|
CheckKind::DuplicateArgumentName,
|
||||||
CheckKind::FStringMissingPlaceholders,
|
CheckKind::FStringMissingPlaceholders,
|
||||||
CheckKind::IfTuple,
|
CheckKind::IfTuple,
|
||||||
|
CheckKind::IOError("...".to_string()),
|
||||||
CheckKind::ImportStarUsage,
|
CheckKind::ImportStarUsage,
|
||||||
CheckKind::LineTooLong,
|
CheckKind::LineTooLong,
|
||||||
CheckKind::ModuleImportNotAtTopOfFile,
|
CheckKind::ModuleImportNotAtTopOfFile,
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ exclude = ["excluded.py", "**/migrations"]
|
||||||
select = [
|
select = [
|
||||||
"E402",
|
"E402",
|
||||||
"E501",
|
"E501",
|
||||||
|
"E902",
|
||||||
"F401",
|
"F401",
|
||||||
"F403",
|
"F403",
|
||||||
"F541",
|
"F541",
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ use serde::{Deserialize, Serialize};
|
||||||
pub enum CheckCode {
|
pub enum CheckCode {
|
||||||
E402,
|
E402,
|
||||||
E501,
|
E501,
|
||||||
|
E902,
|
||||||
F401,
|
F401,
|
||||||
F403,
|
F403,
|
||||||
F541,
|
F541,
|
||||||
|
|
@ -35,6 +36,7 @@ impl FromStr for CheckCode {
|
||||||
match s {
|
match s {
|
||||||
"E402" => Ok(CheckCode::E402),
|
"E402" => Ok(CheckCode::E402),
|
||||||
"E501" => Ok(CheckCode::E501),
|
"E501" => Ok(CheckCode::E501),
|
||||||
|
"E902" => Ok(CheckCode::E902),
|
||||||
"F401" => Ok(CheckCode::F401),
|
"F401" => Ok(CheckCode::F401),
|
||||||
"F403" => Ok(CheckCode::F403),
|
"F403" => Ok(CheckCode::F403),
|
||||||
"F541" => Ok(CheckCode::F541),
|
"F541" => Ok(CheckCode::F541),
|
||||||
|
|
@ -61,6 +63,7 @@ impl CheckCode {
|
||||||
match self {
|
match self {
|
||||||
CheckCode::E402 => "E402",
|
CheckCode::E402 => "E402",
|
||||||
CheckCode::E501 => "E501",
|
CheckCode::E501 => "E501",
|
||||||
|
CheckCode::E902 => "E902",
|
||||||
CheckCode::F401 => "F401",
|
CheckCode::F401 => "F401",
|
||||||
CheckCode::F403 => "F403",
|
CheckCode::F403 => "F403",
|
||||||
CheckCode::F541 => "F541",
|
CheckCode::F541 => "F541",
|
||||||
|
|
@ -85,6 +88,7 @@ impl CheckCode {
|
||||||
match self {
|
match self {
|
||||||
CheckCode::E402 => &LintSource::AST,
|
CheckCode::E402 => &LintSource::AST,
|
||||||
CheckCode::E501 => &LintSource::Lines,
|
CheckCode::E501 => &LintSource::Lines,
|
||||||
|
CheckCode::E902 => &LintSource::FileSystem,
|
||||||
CheckCode::F401 => &LintSource::AST,
|
CheckCode::F401 => &LintSource::AST,
|
||||||
CheckCode::F403 => &LintSource::AST,
|
CheckCode::F403 => &LintSource::AST,
|
||||||
CheckCode::F541 => &LintSource::AST,
|
CheckCode::F541 => &LintSource::AST,
|
||||||
|
|
@ -109,6 +113,7 @@ impl CheckCode {
|
||||||
pub enum LintSource {
|
pub enum LintSource {
|
||||||
AST,
|
AST,
|
||||||
Lines,
|
Lines,
|
||||||
|
FileSystem,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
|
@ -117,6 +122,7 @@ pub enum CheckKind {
|
||||||
DefaultExceptNotLast,
|
DefaultExceptNotLast,
|
||||||
DuplicateArgumentName,
|
DuplicateArgumentName,
|
||||||
FStringMissingPlaceholders,
|
FStringMissingPlaceholders,
|
||||||
|
IOError(String),
|
||||||
IfTuple,
|
IfTuple,
|
||||||
ImportStarUsage,
|
ImportStarUsage,
|
||||||
LineTooLong,
|
LineTooLong,
|
||||||
|
|
@ -141,6 +147,7 @@ impl CheckKind {
|
||||||
CheckKind::DefaultExceptNotLast => "DefaultExceptNotLast",
|
CheckKind::DefaultExceptNotLast => "DefaultExceptNotLast",
|
||||||
CheckKind::DuplicateArgumentName => "DuplicateArgumentName",
|
CheckKind::DuplicateArgumentName => "DuplicateArgumentName",
|
||||||
CheckKind::FStringMissingPlaceholders => "FStringMissingPlaceholders",
|
CheckKind::FStringMissingPlaceholders => "FStringMissingPlaceholders",
|
||||||
|
CheckKind::IOError(_) => "IOError",
|
||||||
CheckKind::IfTuple => "IfTuple",
|
CheckKind::IfTuple => "IfTuple",
|
||||||
CheckKind::ImportStarUsage => "ImportStarUsage",
|
CheckKind::ImportStarUsage => "ImportStarUsage",
|
||||||
CheckKind::LineTooLong => "LineTooLong",
|
CheckKind::LineTooLong => "LineTooLong",
|
||||||
|
|
@ -165,6 +172,7 @@ impl CheckKind {
|
||||||
CheckKind::DefaultExceptNotLast => &CheckCode::F707,
|
CheckKind::DefaultExceptNotLast => &CheckCode::F707,
|
||||||
CheckKind::DuplicateArgumentName => &CheckCode::F831,
|
CheckKind::DuplicateArgumentName => &CheckCode::F831,
|
||||||
CheckKind::FStringMissingPlaceholders => &CheckCode::F541,
|
CheckKind::FStringMissingPlaceholders => &CheckCode::F541,
|
||||||
|
CheckKind::IOError(_) => &CheckCode::E902,
|
||||||
CheckKind::IfTuple => &CheckCode::F634,
|
CheckKind::IfTuple => &CheckCode::F634,
|
||||||
CheckKind::ImportStarUsage => &CheckCode::F403,
|
CheckKind::ImportStarUsage => &CheckCode::F403,
|
||||||
CheckKind::LineTooLong => &CheckCode::E501,
|
CheckKind::LineTooLong => &CheckCode::E501,
|
||||||
|
|
@ -197,6 +205,9 @@ impl CheckKind {
|
||||||
CheckKind::FStringMissingPlaceholders => {
|
CheckKind::FStringMissingPlaceholders => {
|
||||||
"f-string without any placeholders".to_string()
|
"f-string without any placeholders".to_string()
|
||||||
}
|
}
|
||||||
|
CheckKind::IOError(name) => {
|
||||||
|
format!("No such file or directory: `{name}`")
|
||||||
|
}
|
||||||
CheckKind::IfTuple => "If test is a tuple, which is always `True`".to_string(),
|
CheckKind::IfTuple => "If test is a tuple, which is always `True`".to_string(),
|
||||||
CheckKind::ImportStarUsage => "Unable to detect undefined names".to_string(),
|
CheckKind::ImportStarUsage => "Unable to detect undefined names".to_string(),
|
||||||
CheckKind::LineTooLong => "Line too long".to_string(),
|
CheckKind::LineTooLong => "Line too long".to_string(),
|
||||||
|
|
@ -242,6 +253,7 @@ impl CheckKind {
|
||||||
CheckKind::DuplicateArgumentName => false,
|
CheckKind::DuplicateArgumentName => false,
|
||||||
CheckKind::FStringMissingPlaceholders => false,
|
CheckKind::FStringMissingPlaceholders => false,
|
||||||
CheckKind::IfTuple => false,
|
CheckKind::IfTuple => false,
|
||||||
|
CheckKind::IOError(_) => false,
|
||||||
CheckKind::ImportStarUsage => false,
|
CheckKind::ImportStarUsage => false,
|
||||||
CheckKind::LineTooLong => false,
|
CheckKind::LineTooLong => false,
|
||||||
CheckKind::ModuleImportNotAtTopOfFile => false,
|
CheckKind::ModuleImportNotAtTopOfFile => false,
|
||||||
|
|
|
||||||
19
src/main.rs
19
src/main.rs
|
|
@ -18,6 +18,7 @@ use ::ruff::logging::set_up_logging;
|
||||||
use ::ruff::message::Message;
|
use ::ruff::message::Message;
|
||||||
use ::ruff::settings::Settings;
|
use ::ruff::settings::Settings;
|
||||||
use ::ruff::tell_user;
|
use ::ruff::tell_user;
|
||||||
|
use ruff::checks::CheckKind;
|
||||||
|
|
||||||
const CARGO_PKG_NAME: &str = env!("CARGO_PKG_NAME");
|
const CARGO_PKG_NAME: &str = env!("CARGO_PKG_NAME");
|
||||||
const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
|
const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
|
|
@ -62,7 +63,7 @@ fn run_once(
|
||||||
) -> Result<Vec<Message>> {
|
) -> Result<Vec<Message>> {
|
||||||
// Collect all the files to check.
|
// Collect all the files to check.
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let files: Vec<DirEntry> = files
|
let paths: Vec<DirEntry> = files
|
||||||
.iter()
|
.iter()
|
||||||
.flat_map(|path| iter_python_files(path, &settings.exclude))
|
.flat_map(|path| iter_python_files(path, &settings.exclude))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
@ -70,7 +71,7 @@ fn run_once(
|
||||||
debug!("Identified files to lint in: {:?}", duration);
|
debug!("Identified files to lint in: {:?}", duration);
|
||||||
|
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let mut messages: Vec<Message> = files
|
let mut messages: Vec<Message> = paths
|
||||||
.par_iter()
|
.par_iter()
|
||||||
.map(|entry| {
|
.map(|entry| {
|
||||||
lint_path(entry.path(), settings, &cache.into(), &autofix.into()).unwrap_or_else(|e| {
|
lint_path(entry.path(), settings, &cache.into(), &autofix.into()).unwrap_or_else(|e| {
|
||||||
|
|
@ -80,6 +81,20 @@ fn run_once(
|
||||||
})
|
})
|
||||||
.flatten()
|
.flatten()
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
if settings.select.contains(&CheckCode::E902) {
|
||||||
|
for file in files {
|
||||||
|
if !file.exists() {
|
||||||
|
messages.push(Message {
|
||||||
|
kind: CheckKind::IOError(file.to_string_lossy().to_string()),
|
||||||
|
fixed: false,
|
||||||
|
location: Default::default(),
|
||||||
|
filename: file.to_string_lossy().to_string(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
messages.sort_unstable();
|
messages.sort_unstable();
|
||||||
let duration = start.elapsed();
|
let duration = start.elapsed();
|
||||||
debug!("Checked files in: {:?}", duration);
|
debug!("Checked files in: {:?}", duration);
|
||||||
|
|
|
||||||
|
|
@ -239,6 +239,7 @@ other-attribute = 1
|
||||||
select: Some(BTreeSet::from([
|
select: Some(BTreeSet::from([
|
||||||
CheckCode::E402,
|
CheckCode::E402,
|
||||||
CheckCode::E501,
|
CheckCode::E501,
|
||||||
|
CheckCode::E902,
|
||||||
CheckCode::F401,
|
CheckCode::F401,
|
||||||
CheckCode::F403,
|
CheckCode::F403,
|
||||||
CheckCode::F541,
|
CheckCode::F541,
|
||||||
|
|
|
||||||
|
|
@ -46,6 +46,7 @@ impl Settings {
|
||||||
BTreeSet::from([
|
BTreeSet::from([
|
||||||
CheckCode::E402,
|
CheckCode::E402,
|
||||||
CheckCode::E501,
|
CheckCode::E501,
|
||||||
|
CheckCode::E902,
|
||||||
CheckCode::F401,
|
CheckCode::F401,
|
||||||
CheckCode::F403,
|
CheckCode::F403,
|
||||||
CheckCode::F541,
|
CheckCode::F541,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue