Include package inference during --add-noqa command (#2832)
This commit is contained in:
@@ -220,8 +220,8 @@ pub fn check_path(
|
||||
|
||||
const MAX_ITERATIONS: usize = 100;
|
||||
|
||||
/// Add any missing `#noqa` pragmas to the source code at the given `Path`.
|
||||
pub fn add_noqa_to_path(path: &Path, settings: &Settings) -> Result<usize> {
|
||||
/// Add any missing `# noqa` pragmas to the source code at the given `Path`.
|
||||
pub fn add_noqa_to_path(path: &Path, package: Option<&Path>, settings: &Settings) -> Result<usize> {
|
||||
// Read the file from disk.
|
||||
let contents = fs::read_file(path)?;
|
||||
|
||||
@@ -247,7 +247,7 @@ pub fn add_noqa_to_path(path: &Path, settings: &Settings) -> Result<usize> {
|
||||
error,
|
||||
} = check_path(
|
||||
path,
|
||||
None,
|
||||
package,
|
||||
&contents,
|
||||
tokens,
|
||||
&locator,
|
||||
|
||||
@@ -8,7 +8,7 @@ use rayon::prelude::*;
|
||||
|
||||
use ruff::linter::add_noqa_to_path;
|
||||
use ruff::resolver::PyprojectDiscovery;
|
||||
use ruff::{resolver, warn_user_once};
|
||||
use ruff::{packaging, resolver, warn_user_once};
|
||||
|
||||
use crate::args::Overrides;
|
||||
use crate::iterators::par_iter;
|
||||
@@ -30,13 +30,28 @@ pub fn add_noqa(
|
||||
return Ok(0);
|
||||
}
|
||||
|
||||
// Discover the package root for each Python file.
|
||||
let package_roots = packaging::detect_package_roots(
|
||||
&paths
|
||||
.iter()
|
||||
.flatten()
|
||||
.map(ignore::DirEntry::path)
|
||||
.collect::<Vec<_>>(),
|
||||
&resolver,
|
||||
pyproject_strategy,
|
||||
);
|
||||
|
||||
let start = Instant::now();
|
||||
let modifications: usize = par_iter(&paths)
|
||||
.flatten()
|
||||
.filter_map(|entry| {
|
||||
let path = entry.path();
|
||||
let package = path
|
||||
.parent()
|
||||
.and_then(|parent| package_roots.get(parent))
|
||||
.and_then(|package| *package);
|
||||
let settings = resolver.resolve(path, pyproject_strategy);
|
||||
match add_noqa_to_path(path, settings) {
|
||||
match add_noqa_to_path(path, package, settings) {
|
||||
Ok(count) => Some(count),
|
||||
Err(e) => {
|
||||
error!("Failed to add noqa to {}: {e}", path.to_string_lossy());
|
||||
|
||||
Reference in New Issue
Block a user