mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-23 21:15:19 +00:00
Extend multi-line noqa directives to start-of-line (#4490)
This commit is contained in:
parent
ddd541b198
commit
a8d080c825
3 changed files with 24 additions and 7 deletions
|
@ -88,3 +88,12 @@ import sys # noqa: F401, RUF100
|
||||||
print(sys.path)
|
print(sys.path)
|
||||||
|
|
||||||
"shape: (6,)\nSeries: '' [duration[μs]]\n[\n\t0µs\n\t1µs\n\t2µs\n\t3µs\n\t4µs\n\t5µs\n]" # noqa: F401
|
"shape: (6,)\nSeries: '' [duration[μs]]\n[\n\t0µs\n\t1µs\n\t2µs\n\t3µs\n\t4µs\n\t5µs\n]" # noqa: F401
|
||||||
|
|
||||||
|
|
||||||
|
def f():
|
||||||
|
# Ensure that the `noqa` applies to both the overlong line _and_ the unused
|
||||||
|
# variable.
|
||||||
|
a = """Lorem ipsum dolor sit amet.
|
||||||
|
|
||||||
|
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
|
||||||
|
""" # noqa
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
//! Extract `# noqa` and `# isort: skip` directives from tokenized source.
|
//! Extract `# noqa` and `# isort: skip` directives from tokenized source.
|
||||||
|
|
||||||
use crate::noqa::NoqaMapping;
|
|
||||||
use bitflags::bitflags;
|
use bitflags::bitflags;
|
||||||
use ruff_python_ast::source_code::{Indexer, Locator};
|
|
||||||
use ruff_text_size::{TextLen, TextRange, TextSize};
|
use ruff_text_size::{TextLen, TextRange, TextSize};
|
||||||
use rustpython_parser::lexer::LexResult;
|
use rustpython_parser::lexer::LexResult;
|
||||||
use rustpython_parser::Tok;
|
use rustpython_parser::Tok;
|
||||||
|
|
||||||
|
use ruff_python_ast::source_code::{Indexer, Locator};
|
||||||
|
|
||||||
|
use crate::noqa::NoqaMapping;
|
||||||
use crate::settings::Settings;
|
use crate::settings::Settings;
|
||||||
|
|
||||||
bitflags! {
|
bitflags! {
|
||||||
|
@ -102,7 +103,10 @@ pub fn extract_noqa_line_for(
|
||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
if locator.contains_line_break(*range) {
|
if locator.contains_line_break(*range) {
|
||||||
string_mappings.push(*range);
|
string_mappings.push(TextRange::new(
|
||||||
|
locator.line_start(range.start()),
|
||||||
|
range.end(),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -219,11 +223,12 @@ pub fn extract_isort_directives(lxr: &[LexResult], locator: &Locator) -> IsortDi
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use ruff_python_ast::source_code::{Indexer, Locator};
|
|
||||||
use ruff_text_size::{TextLen, TextRange, TextSize};
|
use ruff_text_size::{TextLen, TextRange, TextSize};
|
||||||
use rustpython_parser::lexer::LexResult;
|
use rustpython_parser::lexer::LexResult;
|
||||||
use rustpython_parser::{lexer, Mode};
|
use rustpython_parser::{lexer, Mode};
|
||||||
|
|
||||||
|
use ruff_python_ast::source_code::{Indexer, Locator};
|
||||||
|
|
||||||
use crate::directives::{extract_isort_directives, extract_noqa_line_for};
|
use crate::directives::{extract_isort_directives, extract_noqa_line_for};
|
||||||
use crate::noqa::NoqaMapping;
|
use crate::noqa::NoqaMapping;
|
||||||
|
|
||||||
|
@ -271,7 +276,7 @@ y = 2
|
||||||
z = x + 1";
|
z = x + 1";
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
noqa_mappings(contents),
|
noqa_mappings(contents),
|
||||||
NoqaMapping::from_iter([TextRange::new(TextSize::from(4), TextSize::from(22)),])
|
NoqaMapping::from_iter([TextRange::new(TextSize::from(0), TextSize::from(22)),])
|
||||||
);
|
);
|
||||||
|
|
||||||
let contents = "x = 1
|
let contents = "x = 1
|
||||||
|
@ -282,7 +287,7 @@ ghi
|
||||||
z = 2";
|
z = 2";
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
noqa_mappings(contents),
|
noqa_mappings(contents),
|
||||||
NoqaMapping::from_iter([TextRange::new(TextSize::from(10), TextSize::from(28))])
|
NoqaMapping::from_iter([TextRange::new(TextSize::from(6), TextSize::from(28))])
|
||||||
);
|
);
|
||||||
|
|
||||||
let contents = "x = 1
|
let contents = "x = 1
|
||||||
|
@ -292,7 +297,7 @@ ghi
|
||||||
'''";
|
'''";
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
noqa_mappings(contents),
|
noqa_mappings(contents),
|
||||||
NoqaMapping::from_iter([TextRange::new(TextSize::from(10), TextSize::from(28))])
|
NoqaMapping::from_iter([TextRange::new(TextSize::from(6), TextSize::from(28))])
|
||||||
);
|
);
|
||||||
|
|
||||||
let contents = r#"x = \
|
let contents = r#"x = \
|
||||||
|
|
|
@ -260,5 +260,8 @@ RUF100_0.py:90:92: RUF100 [*] Unused `noqa` directive (unused: `F401`)
|
||||||
89 89 |
|
89 89 |
|
||||||
90 |-"shape: (6,)\nSeries: '' [duration[μs]]\n[\n\t0µs\n\t1µs\n\t2µs\n\t3µs\n\t4µs\n\t5µs\n]" # noqa: F401
|
90 |-"shape: (6,)\nSeries: '' [duration[μs]]\n[\n\t0µs\n\t1µs\n\t2µs\n\t3µs\n\t4µs\n\t5µs\n]" # noqa: F401
|
||||||
90 |+"shape: (6,)\nSeries: '' [duration[μs]]\n[\n\t0µs\n\t1µs\n\t2µs\n\t3µs\n\t4µs\n\t5µs\n]"
|
90 |+"shape: (6,)\nSeries: '' [duration[μs]]\n[\n\t0µs\n\t1µs\n\t2µs\n\t3µs\n\t4µs\n\t5µs\n]"
|
||||||
|
91 91 |
|
||||||
|
92 92 |
|
||||||
|
93 93 | def f():
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue