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

Respect Q00* ignores in flake8-quotes rules #10728

Merged
merged 1 commit into from
Apr 2, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""This is a docstring."""

this_is_an_inline_string = "double quote string"

this_is_a_multiline_string = """
double quote string
"""
57 changes: 57 additions & 0 deletions crates/ruff_linter/src/rules/flake8_quotes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,61 @@ mod tests {
assert_messages!(snapshot, diagnostics);
Ok(())
}

#[test_case(Path::new("doubles_all.py"))]
fn only_inline(path: &Path) -> Result<()> {
let snapshot = format!("only_inline_{}", path.to_string_lossy());
let diagnostics = test_path(
Path::new("flake8_quotes").join(path).as_path(),
&LinterSettings {
flake8_quotes: super::settings::Settings {
inline_quotes: Quote::Single,
multiline_quotes: Quote::Single,
docstring_quotes: Quote::Single,
avoid_escape: true,
},
..LinterSettings::for_rules(vec![Rule::BadQuotesInlineString])
},
)?;
assert_messages!(snapshot, diagnostics);
Ok(())
}

#[test_case(Path::new("doubles_all.py"))]
fn only_multiline(path: &Path) -> Result<()> {
let snapshot = format!("only_multiline_{}", path.to_string_lossy());
let diagnostics = test_path(
Path::new("flake8_quotes").join(path).as_path(),
&LinterSettings {
flake8_quotes: super::settings::Settings {
inline_quotes: Quote::Single,
multiline_quotes: Quote::Single,
docstring_quotes: Quote::Single,
avoid_escape: true,
},
..LinterSettings::for_rules(vec![Rule::BadQuotesMultilineString])
},
)?;
assert_messages!(snapshot, diagnostics);
Ok(())
}

#[test_case(Path::new("doubles_all.py"))]
fn only_docstring(path: &Path) -> Result<()> {
let snapshot = format!("only_docstring_{}", path.to_string_lossy());
let diagnostics = test_path(
Path::new("flake8_quotes").join(path).as_path(),
&LinterSettings {
flake8_quotes: super::settings::Settings {
inline_quotes: Quote::Single,
multiline_quotes: Quote::Single,
docstring_quotes: Quote::Single,
avoid_escape: true,
},
..LinterSettings::for_rules(vec![Rule::BadQuotesDocstring])
},
)?;
assert_messages!(snapshot, diagnostics);
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use ruff_source_file::Locator;
use ruff_text_size::{Ranged, TextRange};

use crate::checkers::ast::Checker;
use crate::registry::Rule;

use super::super::settings::Quote;

Expand Down Expand Up @@ -334,6 +335,11 @@ fn strings(checker: &mut Checker, sequence: &[TextRange]) {

for (range, trivia) in sequence.iter().zip(trivia) {
if trivia.is_multiline {
// If multiline strings aren't enforced, ignore it.
if !checker.enabled(Rule::BadQuotesMultilineString) {
continue;
}

// If our string is or contains a known good string, ignore it.
if trivia
.raw_text
Expand Down Expand Up @@ -375,6 +381,11 @@ fn strings(checker: &mut Checker, sequence: &[TextRange]) {
// If we're not using the preferred type, only allow use to avoid escapes.
&& !relax_quote
{
// If inline strings aren't enforced, ignore it.
if !checker.enabled(Rule::BadQuotesInlineString) {
continue;
}

if trivia.has_empty_text()
&& text_ends_at_quote(locator, *range, quotes_settings.inline_quotes)
{
Expand Down Expand Up @@ -455,10 +466,14 @@ pub(crate) fn check_string_quotes(checker: &mut Checker, string_like: StringLike
};

if checker.semantic().in_docstring() {
for range in ranges {
docstring(checker, range);
if checker.enabled(Rule::BadQuotesDocstring) {
for range in ranges {
docstring(checker, range);
}
}
} else {
strings(checker, &ranges);
if checker.any_enabled(&[Rule::BadQuotesInlineString, Rule::BadQuotesMultilineString]) {
strings(checker, &ranges);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: crates/ruff_linter/src/rules/flake8_quotes/mod.rs
---
doubles_all.py:1:1: Q002 [*] Double quote docstring found but single quotes preferred
|
1 | """This is a docstring."""
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ Q002
2 |
3 | this_is_an_inline_string = "double quote string"
|
= help: Replace double quotes docstring with single quotes

ℹ Safe fix
1 |-"""This is a docstring."""
1 |+'''This is a docstring.'''
2 2 |
3 3 | this_is_an_inline_string = "double quote string"
4 4 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
source: crates/ruff_linter/src/rules/flake8_quotes/mod.rs
---
doubles_all.py:3:28: Q000 [*] Double quotes found but single quotes preferred
|
1 | """This is a docstring."""
2 |
3 | this_is_an_inline_string = "double quote string"
| ^^^^^^^^^^^^^^^^^^^^^ Q000
4 |
5 | this_is_a_multiline_string = """
|
= help: Replace double quotes with single quotes

ℹ Safe fix
1 1 | """This is a docstring."""
2 2 |
3 |-this_is_an_inline_string = "double quote string"
3 |+this_is_an_inline_string = 'double quote string'
4 4 |
5 5 | this_is_a_multiline_string = """
6 6 | double quote string
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
source: crates/ruff_linter/src/rules/flake8_quotes/mod.rs
---
doubles_all.py:5:30: Q001 [*] Double quote multiline found but single quotes preferred
|
3 | this_is_an_inline_string = "double quote string"
4 |
5 | this_is_a_multiline_string = """
| ______________________________^
6 | | double quote string
7 | | """
| |___^ Q001
|
= help: Replace double multiline quotes with single quotes

ℹ Safe fix
2 2 |
3 3 | this_is_an_inline_string = "double quote string"
4 4 |
5 |-this_is_a_multiline_string = """
5 |+this_is_a_multiline_string = '''
6 6 | double quote string
7 |-"""
7 |+'''