Include package inference during --add-noqa command (#2832)

This commit is contained in:
Charlie Marsh
2023-02-12 17:45:39 -05:00
committed by GitHub
parent e2051ef72f
commit 46c184600f
2 changed files with 20 additions and 5 deletions

View File

@@ -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,

View File

@@ -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());