mirror of https://github.com/astral-sh/ruff
Ignore single quote docstrings with newline escape (#7173)
This commit is contained in:
parent
2e58ad437e
commit
e3114a144c
|
|
@ -639,3 +639,8 @@ def starts_with_space_then_this():
|
||||||
class SameLine: """This is a docstring on the same line"""
|
class SameLine: """This is a docstring on the same line"""
|
||||||
|
|
||||||
def same_line(): """This is a docstring on the same line"""
|
def same_line(): """This is a docstring on the same line"""
|
||||||
|
|
||||||
|
|
||||||
|
def single_line_docstring_with_an_escaped_backslash():
|
||||||
|
"\
|
||||||
|
"
|
||||||
|
|
|
||||||
|
|
@ -90,13 +90,21 @@ pub(crate) fn ends_with_period(checker: &mut Checker, docstring: &Docstring) {
|
||||||
let line = lines.nth(index).unwrap();
|
let line = lines.nth(index).unwrap();
|
||||||
let trimmed = line.trim_end();
|
let trimmed = line.trim_end();
|
||||||
|
|
||||||
|
if trimmed.ends_with('\\') {
|
||||||
|
// Ignore the edge case whether a single quoted string is multiple lines through an
|
||||||
|
// escape (https://github.com/astral-sh/ruff/issues/7139). Single quote docstrings are
|
||||||
|
// flagged by D300.
|
||||||
|
// ```python
|
||||||
|
// "\
|
||||||
|
// "
|
||||||
|
// ```
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if !trimmed.ends_with('.') {
|
if !trimmed.ends_with('.') {
|
||||||
let mut diagnostic = Diagnostic::new(EndsInPeriod, docstring.range());
|
let mut diagnostic = Diagnostic::new(EndsInPeriod, docstring.range());
|
||||||
// Best-effort autofix: avoid adding a period after other punctuation marks.
|
// Best-effort autofix: avoid adding a period after other punctuation marks.
|
||||||
if checker.patch(diagnostic.kind.rule())
|
if checker.patch(diagnostic.kind.rule()) && !trimmed.ends_with([':', ';']) {
|
||||||
&& !trimmed.ends_with(':')
|
|
||||||
&& !trimmed.ends_with(';')
|
|
||||||
{
|
|
||||||
diagnostic.set_fix(Fix::suggested(Edit::insertion(
|
diagnostic.set_fix(Fix::suggested(Edit::insertion(
|
||||||
".".to_string(),
|
".".to_string(),
|
||||||
line.start() + trimmed.text_len(),
|
line.start() + trimmed.text_len(),
|
||||||
|
|
|
||||||
|
|
@ -89,6 +89,17 @@ pub(crate) fn ends_with_punctuation(checker: &mut Checker, docstring: &Docstring
|
||||||
let line = lines.next().unwrap();
|
let line = lines.next().unwrap();
|
||||||
let trimmed = line.trim_end();
|
let trimmed = line.trim_end();
|
||||||
|
|
||||||
|
if trimmed.ends_with('\\') {
|
||||||
|
// Ignore the edge case whether a single quoted string is multiple lines through an
|
||||||
|
// escape (https://github.com/astral-sh/ruff/issues/7139). Single quote docstrings are
|
||||||
|
// flagged by D300.
|
||||||
|
// ```python
|
||||||
|
// "\
|
||||||
|
// "
|
||||||
|
// ```
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if !trimmed.ends_with(['.', '!', '?']) {
|
if !trimmed.ends_with(['.', '!', '?']) {
|
||||||
let mut diagnostic = Diagnostic::new(EndsInPunctuation, docstring.range());
|
let mut diagnostic = Diagnostic::new(EndsInPunctuation, docstring.range());
|
||||||
// Best-effort autofix: avoid adding a period after other punctuation marks.
|
// Best-effort autofix: avoid adding a period after other punctuation marks.
|
||||||
|
|
|
||||||
|
|
@ -151,6 +151,15 @@ pub(crate) fn multi_line_summary_start(checker: &mut Checker, docstring: &Docstr
|
||||||
}
|
}
|
||||||
checker.diagnostics.push(diagnostic);
|
checker.diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
} else if first_line.as_str().ends_with('\\') {
|
||||||
|
// Ignore the edge case whether a single quoted string is multiple lines through an
|
||||||
|
// escape (https://github.com/astral-sh/ruff/issues/7139). Single quote docstrings are
|
||||||
|
// flagged by D300.
|
||||||
|
// ```python
|
||||||
|
// "\
|
||||||
|
// "
|
||||||
|
// ```
|
||||||
|
return;
|
||||||
} else {
|
} else {
|
||||||
if checker.enabled(Rule::MultiLineSummarySecondLine) {
|
if checker.enabled(Rule::MultiLineSummarySecondLine) {
|
||||||
let mut diagnostic = Diagnostic::new(MultiLineSummarySecondLine, docstring.range());
|
let mut diagnostic = Diagnostic::new(MultiLineSummarySecondLine, docstring.range());
|
||||||
|
|
|
||||||
|
|
@ -97,4 +97,14 @@ D.py:624:5: D200 One-line docstring should fit on one line
|
||||||
|
|
|
|
||||||
= help: Reformat to one line
|
= help: Reformat to one line
|
||||||
|
|
||||||
|
D.py:645:5: D200 One-line docstring should fit on one line
|
||||||
|
|
|
||||||
|
644 | def single_line_docstring_with_an_escaped_backslash():
|
||||||
|
645 | "\
|
||||||
|
| _____^
|
||||||
|
646 | | "
|
||||||
|
| |_____^ D200
|
||||||
|
|
|
||||||
|
= help: Reformat to one line
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -41,4 +41,13 @@ D.py:328:5: D300 Use triple double quotes `"""`
|
||||||
| ^^^^^^^^^^^^ D300
|
| ^^^^^^^^^^^^ D300
|
||||||
|
|
|
|
||||||
|
|
||||||
|
D.py:645:5: D300 Use triple double quotes `"""`
|
||||||
|
|
|
||||||
|
644 | def single_line_docstring_with_an_escaped_backslash():
|
||||||
|
645 | "\
|
||||||
|
| _____^
|
||||||
|
646 | | "
|
||||||
|
| |_____^ D300
|
||||||
|
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -289,6 +289,7 @@ D.py:639:17: D400 [*] First line should end with a period
|
||||||
639 |+class SameLine: """This is a docstring on the same line."""
|
639 |+class SameLine: """This is a docstring on the same line."""
|
||||||
640 640 |
|
640 640 |
|
||||||
641 641 | def same_line(): """This is a docstring on the same line"""
|
641 641 | def same_line(): """This is a docstring on the same line"""
|
||||||
|
642 642 |
|
||||||
|
|
||||||
D.py:641:18: D400 [*] First line should end with a period
|
D.py:641:18: D400 [*] First line should end with a period
|
||||||
|
|
|
|
||||||
|
|
@ -305,5 +306,8 @@ D.py:641:18: D400 [*] First line should end with a period
|
||||||
640 640 |
|
640 640 |
|
||||||
641 |-def same_line(): """This is a docstring on the same line"""
|
641 |-def same_line(): """This is a docstring on the same line"""
|
||||||
641 |+def same_line(): """This is a docstring on the same line."""
|
641 |+def same_line(): """This is a docstring on the same line."""
|
||||||
|
642 642 |
|
||||||
|
643 643 |
|
||||||
|
644 644 | def single_line_docstring_with_an_escaped_backslash():
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -271,6 +271,7 @@ D.py:639:17: D415 [*] First line should end with a period, question mark, or exc
|
||||||
639 |+class SameLine: """This is a docstring on the same line."""
|
639 |+class SameLine: """This is a docstring on the same line."""
|
||||||
640 640 |
|
640 640 |
|
||||||
641 641 | def same_line(): """This is a docstring on the same line"""
|
641 641 | def same_line(): """This is a docstring on the same line"""
|
||||||
|
642 642 |
|
||||||
|
|
||||||
D.py:641:18: D415 [*] First line should end with a period, question mark, or exclamation point
|
D.py:641:18: D415 [*] First line should end with a period, question mark, or exclamation point
|
||||||
|
|
|
|
||||||
|
|
@ -287,5 +288,8 @@ D.py:641:18: D415 [*] First line should end with a period, question mark, or exc
|
||||||
640 640 |
|
640 640 |
|
||||||
641 |-def same_line(): """This is a docstring on the same line"""
|
641 |-def same_line(): """This is a docstring on the same line"""
|
||||||
641 |+def same_line(): """This is a docstring on the same line."""
|
641 |+def same_line(): """This is a docstring on the same line."""
|
||||||
|
642 642 |
|
||||||
|
643 643 |
|
||||||
|
644 644 | def single_line_docstring_with_an_escaped_backslash():
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue