From 4982694b54e941f437740b9e75a9b1be58560e77 Mon Sep 17 00:00:00 2001 From: T-256 <132141463+T-256@users.noreply.github.com> Date: Fri, 3 Nov 2023 20:19:50 +0330 Subject: [PATCH] `D300`: prevent autofix when both triples are in body (#8462) ## Summary Addresses https://github.com/astral-sh/ruff/issues/8402#issuecomment-1788782750 ## Test Plan Added associated test --- .../resources/test/fixtures/pydocstyle/D300.py | 16 ++++++++++++++++ .../src/rules/pydocstyle/rules/triple_quotes.rs | 6 ++++-- ...pydocstyle__tests__preview__D300_D300.py.snap | 4 ++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/pydocstyle/D300.py b/crates/ruff_linter/resources/test/fixtures/pydocstyle/D300.py index eb9b4c57307da..75c5f4d4e8b6a 100644 --- a/crates/ruff_linter/resources/test/fixtures/pydocstyle/D300.py +++ b/crates/ruff_linter/resources/test/fixtures/pydocstyle/D300.py @@ -8,3 +8,19 @@ def ends_in_quote(): def contains_quote(): 'Sum"\\mary.' + + +# OK +def contains_triples(t): + """('''|\""")""" + + +# OK +def contains_triples(t): + '''(\'''|""")''' + + +# TODO: here should raise D300 for using dobule quotes instead, +# because escaped double quote does allow us. +def contains_triples(t): + '''(\""")''' diff --git a/crates/ruff_linter/src/rules/pydocstyle/rules/triple_quotes.rs b/crates/ruff_linter/src/rules/pydocstyle/rules/triple_quotes.rs index eb7caaec2460c..d9db36f397799 100644 --- a/crates/ruff_linter/src/rules/pydocstyle/rules/triple_quotes.rs +++ b/crates/ruff_linter/src/rules/pydocstyle/rules/triple_quotes.rs @@ -61,12 +61,14 @@ impl Violation for TripleSingleQuotes { pub(crate) fn triple_quotes(checker: &mut Checker, docstring: &Docstring) { let leading_quote = docstring.leading_quote(); - let prefixes = docstring - .leading_quote() + let prefixes = leading_quote .trim_end_matches(|c| c == '\'' || c == '"') .to_owned(); let expected_quote = if docstring.body().contains("\"\"\"") { + if docstring.body().contains("\'\'\'") { + return; + } Quote::Single } else { Quote::Double diff --git a/crates/ruff_linter/src/rules/pydocstyle/snapshots/ruff_linter__rules__pydocstyle__tests__preview__D300_D300.py.snap b/crates/ruff_linter/src/rules/pydocstyle/snapshots/ruff_linter__rules__pydocstyle__tests__preview__D300_D300.py.snap index 3b1b637e90a4c..2be3a13bfa4d7 100644 --- a/crates/ruff_linter/src/rules/pydocstyle/snapshots/ruff_linter__rules__pydocstyle__tests__preview__D300_D300.py.snap +++ b/crates/ruff_linter/src/rules/pydocstyle/snapshots/ruff_linter__rules__pydocstyle__tests__preview__D300_D300.py.snap @@ -1,5 +1,6 @@ --- source: crates/ruff_linter/src/rules/pydocstyle/mod.rs +assertion_line: 134 --- D300.py:6:5: D300 Use triple double quotes `"""` | @@ -23,5 +24,8 @@ D300.py:10:5: D300 [*] Use triple double quotes `"""` 9 9 | def contains_quote(): 10 |- 'Sum"\\mary.' 10 |+ """Sum"\\mary.""" +11 11 | +12 12 | +13 13 | # OK