Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a CLI flag to force-ignore noqa directives #3296

Merged
merged 1 commit into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/ruff/src/lib_wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ pub fn check(contents: &str, options: JsValue) -> Result<JsValue, JsValue> {
&indexer,
&directives,
&settings,
flags::Autofix::Enabled,
flags::Noqa::Enabled,
flags::Autofix::Enabled,
);

let messages: Vec<ExpandedMessage> = diagnostics
Expand Down
10 changes: 6 additions & 4 deletions crates/ruff/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ pub fn check_path(
indexer: &Indexer,
directives: &Directives,
settings: &Settings,
autofix: flags::Autofix,
noqa: flags::Noqa,
autofix: flags::Autofix,
) -> LinterResult<Vec<Diagnostic>> {
// Aggregate all diagnostics.
let mut diagnostics = vec![];
Expand Down Expand Up @@ -255,8 +255,8 @@ pub fn add_noqa_to_path(path: &Path, package: Option<&Path>, settings: &Settings
&indexer,
&directives,
settings,
flags::Autofix::Disabled,
flags::Noqa::Disabled,
flags::Autofix::Disabled,
);

// Log any parse errors.
Expand Down Expand Up @@ -287,6 +287,7 @@ pub fn lint_only(
path: &Path,
package: Option<&Path>,
settings: &Settings,
noqa: flags::Noqa,
autofix: flags::Autofix,
) -> LinterResult<Vec<Message>> {
// Tokenize once.
Expand Down Expand Up @@ -316,8 +317,8 @@ pub fn lint_only(
&indexer,
&directives,
settings,
noqa,
autofix,
flags::Noqa::Enabled,
);

// Convert from diagnostics to messages.
Expand Down Expand Up @@ -345,6 +346,7 @@ pub fn lint_fix<'a>(
contents: &'a str,
path: &Path,
package: Option<&Path>,
noqa: flags::Noqa,
settings: &Settings,
) -> Result<(LinterResult<Vec<Message>>, Cow<'a, str>, FixTable)> {
let mut transformed = Cow::Borrowed(contents);
Expand Down Expand Up @@ -387,8 +389,8 @@ pub fn lint_fix<'a>(
&indexer,
&directives,
settings,
noqa,
flags::Autofix::Enabled,
flags::Noqa::Enabled,
);

if iterations == 0 {
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/rules/pandas_vet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ mod tests {
&indexer,
&directives,
&settings,
flags::Autofix::Enabled,
flags::Noqa::Enabled,
flags::Autofix::Enabled,
);
let actual = diagnostics
.iter()
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/rules/pyflakes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ mod tests {
&indexer,
&directives,
&settings,
flags::Autofix::Enabled,
flags::Noqa::Enabled,
flags::Autofix::Enabled,
);
diagnostics.sort_by_key(|diagnostic| diagnostic.location);
let actual = diagnostics
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ pub fn test_path(path: &Path, settings: &Settings) -> Result<Vec<Diagnostic>> {
&indexer,
&directives,
settings,
flags::Autofix::Enabled,
flags::Noqa::Enabled,
flags::Autofix::Enabled,
);

// Detect autofixes that don't converge after multiple iterations.
Expand Down Expand Up @@ -76,8 +76,8 @@ pub fn test_path(path: &Path, settings: &Settings) -> Result<Vec<Diagnostic>> {
&indexer,
&directives,
settings,
flags::Autofix::Enabled,
flags::Noqa::Enabled,
flags::Autofix::Enabled,
);
if let Some((fixed_contents, _)) = fix_file(&diagnostics, &locator) {
if iterations < max_iterations {
Expand Down
8 changes: 8 additions & 0 deletions crates/ruff_cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ pub struct CheckArgs {
fix_only: bool,
#[clap(long, overrides_with("fix_only"), hide = true)]
no_fix_only: bool,
/// Ignore any `# noqa` comments.
#[arg(long)]
ignore_noqa: bool,
/// Output serialization format for violations.
#[arg(long, value_enum, env = "RUFF_FORMAT")]
pub format: Option<SerializationFormat>,
Expand Down Expand Up @@ -258,6 +261,7 @@ pub struct CheckArgs {
conflicts_with = "show_files",
conflicts_with = "show_settings",
// Unsupported default-command arguments.
conflicts_with = "ignore_noqa",
conflicts_with = "statistics",
conflicts_with = "stdin_filename",
conflicts_with = "watch",
Expand All @@ -272,6 +276,7 @@ pub struct CheckArgs {
// conflicts_with = "show_files",
conflicts_with = "show_settings",
// Unsupported default-command arguments.
conflicts_with = "ignore_noqa",
conflicts_with = "statistics",
conflicts_with = "stdin_filename",
conflicts_with = "watch",
Expand All @@ -285,6 +290,7 @@ pub struct CheckArgs {
conflicts_with = "show_files",
// conflicts_with = "show_settings",
// Unsupported default-command arguments.
conflicts_with = "ignore_noqa",
conflicts_with = "statistics",
conflicts_with = "stdin_filename",
conflicts_with = "watch",
Expand Down Expand Up @@ -357,6 +363,7 @@ impl CheckArgs {
exit_zero: self.exit_zero,
exit_non_zero_on_fix: self.exit_non_zero_on_fix,
files: self.files,
ignore_noqa: self.ignore_noqa,
isolated: self.isolated,
no_cache: self.no_cache,
show_files: self.show_files,
Expand Down Expand Up @@ -415,6 +422,7 @@ pub struct Arguments {
pub exit_zero: bool,
pub exit_non_zero_on_fix: bool,
pub files: Vec<PathBuf>,
pub ignore_noqa: bool,
pub isolated: bool,
pub no_cache: bool,
pub show_files: bool,
Expand Down
3 changes: 2 additions & 1 deletion crates/ruff_cli/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub fn run(
pyproject_strategy: &PyprojectDiscovery,
overrides: &Overrides,
cache: flags::Cache,
noqa: flags::Noqa,
autofix: fix::FixMode,
) -> Result<Diagnostics> {
// Collect all the Python files to check.
Expand Down Expand Up @@ -84,7 +85,7 @@ pub fn run(
.and_then(|parent| package_roots.get(parent))
.and_then(|package| *package);
let settings = resolver.resolve_all(path, pyproject_strategy);
lint_path(path, package, settings, cache, autofix)
lint_path(path, package, settings, cache, noqa, autofix)
.map_err(|e| (Some(path.to_owned()), e.to_string()))
}
Err(e) => Err((
Expand Down
4 changes: 3 additions & 1 deletion crates/ruff_cli/src/commands/run_stdin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::path::Path;
use anyhow::Result;

use ruff::resolver::PyprojectDiscovery;
use ruff::settings::flags;
use ruff::{fix, packaging, resolver};

use crate::args::Overrides;
Expand All @@ -21,6 +22,7 @@ pub fn run_stdin(
filename: Option<&Path>,
pyproject_strategy: &PyprojectDiscovery,
overrides: &Overrides,
noqa: flags::Noqa,
autofix: fix::FixMode,
) -> Result<Diagnostics> {
if let Some(filename) = filename {
Expand All @@ -33,7 +35,7 @@ pub fn run_stdin(
.and_then(Path::parent)
.and_then(|path| packaging::detect_package_root(path, &settings.lib.namespace_packages));
let stdin = read_from_stdin()?;
let mut diagnostics = lint_stdin(filename, package_root, &stdin, &settings.lib, autofix)?;
let mut diagnostics = lint_stdin(filename, package_root, &stdin, &settings.lib, noqa, autofix)?;
diagnostics.messages.sort_unstable();
Ok(diagnostics)
}
30 changes: 26 additions & 4 deletions crates/ruff_cli/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub fn lint_path(
package: Option<&Path>,
settings: &AllSettings,
cache: flags::Cache,
noqa: flags::Noqa,
autofix: fix::FixMode,
) -> Result<Diagnostics> {
// Check the cache.
Expand All @@ -65,7 +66,9 @@ pub fn lint_path(
// to cache `fixer::Mode::Apply`, since a file either has no fixes, or we'll
// write the fixes to disk, thus invalidating the cache. But it's a bit hard
// to reason about. We need to come up with a better solution here.)
let metadata = if cache.into() && matches!(autofix, fix::FixMode::None | fix::FixMode::Generate)
let metadata = if cache.into()
&& noqa.into()
&& matches!(autofix, fix::FixMode::None | fix::FixMode::Generate)
{
let metadata = path.metadata()?;
if let Some(messages) =
Expand All @@ -90,7 +93,8 @@ pub fn lint_path(
},
fixed,
) = if matches!(autofix, fix::FixMode::Apply | fix::FixMode::Diff) {
if let Ok((result, transformed, fixed)) = lint_fix(&contents, path, package, &settings.lib)
if let Ok((result, transformed, fixed)) =
lint_fix(&contents, path, package, noqa, &settings.lib)
{
if !fixed.is_empty() {
if matches!(autofix, fix::FixMode::Apply) {
Expand All @@ -108,12 +112,26 @@ pub fn lint_path(
(result, fixed)
} else {
// If we fail to autofix, lint the original source code.
let result = lint_only(&contents, path, package, &settings.lib, autofix.into());
let result = lint_only(
&contents,
path,
package,
&settings.lib,
noqa,
autofix.into(),
);
let fixed = FxHashMap::default();
(result, fixed)
}
} else {
let result = lint_only(&contents, path, package, &settings.lib, autofix.into());
let result = lint_only(
&contents,
path,
package,
&settings.lib,
noqa,
autofix.into(),
);
let fixed = FxHashMap::default();
(result, fixed)
};
Expand Down Expand Up @@ -158,6 +176,7 @@ pub fn lint_stdin(
package: Option<&Path>,
contents: &str,
settings: &Settings,
noqa: flags::Noqa,
autofix: fix::FixMode,
) -> Result<Diagnostics> {
// Lint the inputs.
Expand All @@ -172,6 +191,7 @@ pub fn lint_stdin(
contents,
path.unwrap_or_else(|| Path::new("-")),
package,
noqa,
settings,
) {
if matches!(autofix, fix::FixMode::Apply) {
Expand Down Expand Up @@ -201,6 +221,7 @@ pub fn lint_stdin(
path.unwrap_or_else(|| Path::new("-")),
package,
settings,
noqa,
autofix.into(),
);
let fixed = FxHashMap::default();
Expand All @@ -218,6 +239,7 @@ pub fn lint_stdin(
path.unwrap_or_else(|| Path::new("-")),
package,
settings,
noqa,
autofix.into(),
);
let fixed = FxHashMap::default();
Expand Down
5 changes: 5 additions & 0 deletions crates/ruff_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ fn check(args: CheckArgs, log_level: LogLevel) -> Result<ExitStatus> {
fix::FixMode::None
};
let cache = !cli.no_cache;
let noqa = !cli.ignore_noqa;
let mut printer_flags = PrinterFlags::empty();
if !(cli.diff || fix_only) {
printer_flags |= PrinterFlags::SHOW_VIOLATIONS;
Expand Down Expand Up @@ -222,6 +223,7 @@ fn check(args: CheckArgs, log_level: LogLevel) -> Result<ExitStatus> {
&pyproject_strategy,
&overrides,
cache.into(),
noqa.into(),
fix::FixMode::None,
)?;
printer.write_continuously(&messages)?;
Expand Down Expand Up @@ -251,6 +253,7 @@ fn check(args: CheckArgs, log_level: LogLevel) -> Result<ExitStatus> {
&pyproject_strategy,
&overrides,
cache.into(),
noqa.into(),
fix::FixMode::None,
)?;
printer.write_continuously(&messages)?;
Expand All @@ -268,6 +271,7 @@ fn check(args: CheckArgs, log_level: LogLevel) -> Result<ExitStatus> {
cli.stdin_filename.map(fs::normalize_path).as_deref(),
&pyproject_strategy,
&overrides,
noqa.into(),
autofix,
)?
} else {
Expand All @@ -276,6 +280,7 @@ fn check(args: CheckArgs, log_level: LogLevel) -> Result<ExitStatus> {
&pyproject_strategy,
&overrides,
cache.into(),
noqa.into(),
autofix,
)?
};
Expand Down