Skip to content

Commit

Permalink
Add filename to noqa warnings (#5856)
Browse files Browse the repository at this point in the history
## Summary

Before:

```
» ruff litestar tests --fix
warning: Invalid `# noqa` directive on line 19: expected a comma-separated list of codes (e.g., `# noqa: F401, F841`).
warning: Invalid `# noqa` directive on line 65: expected a comma-separated list of codes (e.g., `# noqa: F401, F841`).
warning: Invalid `# noqa` directive on line 74: expected a comma-separated list of codes (e.g., `# noqa: F401, F841`).
warning: Invalid `# noqa` directive on line 22: expected a comma-separated list of codes (e.g., `# noqa: F401, F841`).
warning: Invalid `# noqa` directive on line 66: expected a comma-separated list of codes (e.g., `# noqa: F401, F841`).
warning: Invalid `# noqa` directive on line 75: expected a comma-separated list of codes (e.g., `# noqa: F401, F841`).
```

After:

```
» cargo run --bin ruff ../litestar/litestar ../litestar/tests
    Finished dev [unoptimized + debuginfo] target(s) in 0.15s
     Running `target/debug/ruff ../litestar/litestar ../litestar/tests`
warning: Detected debug build without --no-cache.
warning: Invalid `# noqa` directive on /Users/sobolev/Desktop/litestar/tests/unit/test_contrib/test_sqlalchemy/models_bigint.py:19: expected a comma-separated list of codes (e.g., `# noqa: F401, F841`).
warning: Invalid `# noqa` directive on /Users/sobolev/Desktop/litestar/tests/unit/test_contrib/test_sqlalchemy/models_bigint.py:65: expected a comma-separated list of codes (e.g., `# noqa: F401, F841`).
warning: Invalid `# noqa` directive on /Users/sobolev/Desktop/litestar/tests/unit/test_contrib/test_sqlalchemy/models_bigint.py:74: expected a comma-separated list of codes (e.g., `# noqa: F401, F841`).
warning: Invalid `# noqa` directive on /Users/sobolev/Desktop/litestar/tests/unit/test_contrib/test_sqlalchemy/models_uuid.py:22: expected a comma-separated list of codes (e.g., `# noqa: F401, F841`).
warning: Invalid `# noqa` directive on /Users/sobolev/Desktop/litestar/tests/unit/test_contrib/test_sqlalchemy/models_uuid.py:66: expected a comma-separated list of codes (e.g., `# noqa: F401, F841`).
warning: Invalid `# noqa` directive on /Users/sobolev/Desktop/litestar/tests/unit/test_contrib/test_sqlalchemy/models_uuid.py:75: expected a comma-separated list of codes (e.g., `# noqa: F401, F841`).
```

## Test Plan

I didn't find any existing tests with this warning.

Closes #5855
  • Loading branch information
sobolevn authored and konstin committed Jul 19, 2023
1 parent a9ce793 commit 914e02a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
7 changes: 5 additions & 2 deletions crates/ruff/src/checkers/noqa.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! `NoQA` enforcement and validation.

use std::path::Path;

use itertools::Itertools;
use ruff_text_size::{TextLen, TextRange, TextSize};
use rustpython_parser::ast::Ranged;
Expand All @@ -16,17 +18,18 @@ use crate::settings::Settings;

pub(crate) fn check_noqa(
diagnostics: &mut Vec<Diagnostic>,
path: &Path,
locator: &Locator,
comment_ranges: &[TextRange],
noqa_line_for: &NoqaMapping,
analyze_directives: bool,
settings: &Settings,
) -> Vec<usize> {
// Identify any codes that are globally exempted (within the current file).
let exemption = FileExemption::try_extract(locator.contents(), comment_ranges, locator);
let exemption = FileExemption::try_extract(locator.contents(), comment_ranges, path, locator);

// Extract all `noqa` directives.
let mut noqa_directives = NoqaDirectives::from_commented_ranges(comment_ranges, locator);
let mut noqa_directives = NoqaDirectives::from_commented_ranges(comment_ranges, path, locator);

// Indices of diagnostics that were ignored by a `noqa` directive.
let mut ignored_diagnostics = vec![];
Expand Down
1 change: 1 addition & 0 deletions crates/ruff/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ pub fn check_path(
{
let ignored = check_noqa(
&mut diagnostics,
path,
locator,
indexer.comment_ranges(),
&directives.noqa_line_for,
Expand Down
23 changes: 19 additions & 4 deletions crates/ruff/src/noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use ruff_python_ast::source_code::Locator;
use ruff_python_whitespace::LineEnding;

use crate::codes::NoqaCode;
use crate::fs::relativize_path;
use crate::registry::{AsRule, Rule, RuleSet};
use crate::rule_redirects::get_redirect_target;

Expand Down Expand Up @@ -225,6 +226,7 @@ impl FileExemption {
pub(crate) fn try_extract(
contents: &str,
comment_ranges: &[TextRange],
path: &Path,
locator: &Locator,
) -> Option<Self> {
let mut exempt_codes: Vec<NoqaCode> = vec![];
Expand All @@ -234,7 +236,8 @@ impl FileExemption {
Err(err) => {
#[allow(deprecated)]
let line = locator.compute_line_index(range.start());
warn!("Invalid `# noqa` directive on line {line}: {err}");
let path_display = path.display();
warn!("Invalid `# noqa` directive on {path_display}:{line}: {err}");
}
Ok(Some(ParsedFileExemption::All)) => {
return Some(Self::All);
Expand Down Expand Up @@ -437,6 +440,7 @@ pub(crate) fn add_noqa(
line_ending: LineEnding,
) -> Result<usize> {
let (count, output) = add_noqa_inner(
path,
diagnostics,
locator,
commented_lines,
Expand All @@ -448,6 +452,7 @@ pub(crate) fn add_noqa(
}

fn add_noqa_inner(
path: &Path,
diagnostics: &[Diagnostic],
locator: &Locator,
commented_ranges: &[TextRange],
Expand All @@ -460,8 +465,8 @@ fn add_noqa_inner(

// Whether the file is exempted from all checks.
// Codes that are globally exempted (within the current file).
let exemption = FileExemption::try_extract(locator.contents(), commented_ranges, locator);
let directives = NoqaDirectives::from_commented_ranges(commented_ranges, locator);
let exemption = FileExemption::try_extract(locator.contents(), commented_ranges, path, locator);
let directives = NoqaDirectives::from_commented_ranges(commented_ranges, path, locator);

// Mark any non-ignored diagnostics.
for diagnostic in diagnostics {
Expand Down Expand Up @@ -625,6 +630,7 @@ pub(crate) struct NoqaDirectives<'a> {
impl<'a> NoqaDirectives<'a> {
pub(crate) fn from_commented_ranges(
comment_ranges: &[TextRange],
path: &Path,
locator: &'a Locator<'a>,
) -> Self {
let mut directives = Vec::new();
Expand All @@ -634,7 +640,8 @@ impl<'a> NoqaDirectives<'a> {
Err(err) => {
#[allow(deprecated)]
let line = locator.compute_line_index(range.start());
warn!("Invalid `# noqa` directive on line {line}: {err}");
let path_display = relativize_path(path);
warn!("Invalid `# noqa` directive on {path_display}:{line}: {err}");
}
Ok(Some(directive)) => {
// noqa comments are guaranteed to be single line.
Expand Down Expand Up @@ -758,6 +765,8 @@ impl FromIterator<TextRange> for NoqaMapping {

#[cfg(test)]
mod tests {
use std::path::Path;

use insta::assert_debug_snapshot;
use ruff_text_size::{TextRange, TextSize};

Expand Down Expand Up @@ -946,9 +955,12 @@ mod tests {

#[test]
fn modification() {
let path = Path::new("/tmp/foo.txt");

let contents = "x = 1";
let noqa_line_for = NoqaMapping::default();
let (count, output) = add_noqa_inner(
path,
&[],
&Locator::new(contents),
&[],
Expand All @@ -968,6 +980,7 @@ mod tests {
let contents = "x = 1";
let noqa_line_for = NoqaMapping::default();
let (count, output) = add_noqa_inner(
path,
&diagnostics,
&Locator::new(contents),
&[],
Expand All @@ -992,6 +1005,7 @@ mod tests {
let contents = "x = 1 # noqa: E741\n";
let noqa_line_for = NoqaMapping::default();
let (count, output) = add_noqa_inner(
path,
&diagnostics,
&Locator::new(contents),
&[TextRange::new(TextSize::from(7), TextSize::from(19))],
Expand All @@ -1016,6 +1030,7 @@ mod tests {
let contents = "x = 1 # noqa";
let noqa_line_for = NoqaMapping::default();
let (count, output) = add_noqa_inner(
path,
&diagnostics,
&Locator::new(contents),
&[TextRange::new(TextSize::from(7), TextSize::from(13))],
Expand Down

0 comments on commit 914e02a

Please sign in to comment.