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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 5 deletions

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