Skip to content

Commit 4982694

Browse files
authored
D300: prevent autofix when both triples are in body (#8462)
## Summary Addresses #8402 (comment) ## Test Plan Added associated test
1 parent 536ac55 commit 4982694

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

crates/ruff_linter/resources/test/fixtures/pydocstyle/D300.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,19 @@ def ends_in_quote():
88

99
def contains_quote():
1010
'Sum"\\mary.'
11+
12+
13+
# OK
14+
def contains_triples(t):
15+
"""('''|\""")"""
16+
17+
18+
# OK
19+
def contains_triples(t):
20+
'''(\'''|""")'''
21+
22+
23+
# TODO: here should raise D300 for using dobule quotes instead,
24+
# because escaped double quote does allow us.
25+
def contains_triples(t):
26+
'''(\""")'''

crates/ruff_linter/src/rules/pydocstyle/rules/triple_quotes.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,14 @@ impl Violation for TripleSingleQuotes {
6161
pub(crate) fn triple_quotes(checker: &mut Checker, docstring: &Docstring) {
6262
let leading_quote = docstring.leading_quote();
6363

64-
let prefixes = docstring
65-
.leading_quote()
64+
let prefixes = leading_quote
6665
.trim_end_matches(|c| c == '\'' || c == '"')
6766
.to_owned();
6867

6968
let expected_quote = if docstring.body().contains("\"\"\"") {
69+
if docstring.body().contains("\'\'\'") {
70+
return;
71+
}
7072
Quote::Single
7173
} else {
7274
Quote::Double

crates/ruff_linter/src/rules/pydocstyle/snapshots/ruff_linter__rules__pydocstyle__tests__preview__D300_D300.py.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
source: crates/ruff_linter/src/rules/pydocstyle/mod.rs
3+
assertion_line: 134
34
---
45
D300.py:6:5: D300 Use triple double quotes `"""`
56
|
@@ -23,5 +24,8 @@ D300.py:10:5: D300 [*] Use triple double quotes `"""`
2324
9 9 | def contains_quote():
2425
10 |- 'Sum"\\mary.'
2526
10 |+ """Sum"\\mary."""
27+
11 11 |
28+
12 12 |
29+
13 13 | # OK
2630

2731

0 commit comments

Comments
 (0)