diff --git a/crates/ruff/resources/test/fixtures/pydocstyle/D301.py b/crates/ruff/resources/test/fixtures/pydocstyle/D301.py new file mode 100644 index 0000000000..1e6c8eef07 --- /dev/null +++ b/crates/ruff/resources/test/fixtures/pydocstyle/D301.py @@ -0,0 +1,29 @@ +def double_quotes_backslash(): + """Sum\\mary.""" + + +def double_quotes_backslash_raw(): + r"""Sum\mary.""" + + +def double_quotes_backslash_uppercase(): + R"""Sum\\mary.""" + + +def make_unique_pod_id(pod_id: str) -> str | None: + r""" + Generate a unique Pod name. + + Kubernetes pod names must consist of one or more lowercase + rfc1035/rfc1123 labels separated by '.' with a maximum length of 253 + characters. + + Name must pass the following regex for validation + ``^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$`` + + For more details, see: + https://github.com/kubernetes/kubernetes/blob/release-1.1/docs/design/identifiers.md + + :param pod_id: requested pod name + :return: ``str`` valid Pod name of appropriate length + """ diff --git a/crates/ruff/src/rules/pydocstyle/mod.rs b/crates/ruff/src/rules/pydocstyle/mod.rs index 9dc0e45fa5..ca0035e418 100644 --- a/crates/ruff/src/rules/pydocstyle/mod.rs +++ b/crates/ruff/src/rules/pydocstyle/mod.rs @@ -82,6 +82,7 @@ mod tests { #[test_case(Rule::SectionUnderlineNotOverIndented, Path::new("sections.py"))] #[test_case(Rule::OverloadWithDocstring, Path::new("D.py"))] #[test_case(Rule::EscapeSequenceInDocstring, Path::new("D.py"))] + #[test_case(Rule::EscapeSequenceInDocstring, Path::new("D301.py"))] #[test_case(Rule::TripleSingleQuotes, Path::new("D.py"))] fn rules(rule_code: Rule, path: &Path) -> Result<()> { let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy()); diff --git a/crates/ruff/src/rules/pydocstyle/rules/backslashes.rs b/crates/ruff/src/rules/pydocstyle/rules/backslashes.rs index 9bc972a4e5..88cbdf2b58 100644 --- a/crates/ruff/src/rules/pydocstyle/rules/backslashes.rs +++ b/crates/ruff/src/rules/pydocstyle/rules/backslashes.rs @@ -54,14 +54,14 @@ impl Violation for EscapeSequenceInDocstring { /// D301 pub(crate) fn backslashes(checker: &mut Checker, docstring: &Docstring) { - let body = docstring.body(); - // Docstring is already raw. - if body.starts_with('r') || body.starts_with("ur") { + let contents = docstring.contents; + if contents.starts_with('r') || contents.starts_with("ur") { return; } // Docstring contains at least one backslash. + let body = docstring.body(); let bytes = body.as_bytes(); if memchr_iter(b'\\', bytes).any(|position| { let escaped_char = bytes.get(position.saturating_add(1)); diff --git a/crates/ruff/src/rules/pydocstyle/snapshots/ruff__rules__pydocstyle__tests__D301_D301.py.snap b/crates/ruff/src/rules/pydocstyle/snapshots/ruff__rules__pydocstyle__tests__D301_D301.py.snap new file mode 100644 index 0000000000..9f8919505c --- /dev/null +++ b/crates/ruff/src/rules/pydocstyle/snapshots/ruff__rules__pydocstyle__tests__D301_D301.py.snap @@ -0,0 +1,18 @@ +--- +source: crates/ruff/src/rules/pydocstyle/mod.rs +--- +D301.py:2:5: D301 Use `r"""` if any backslashes in a docstring + | +1 | def double_quotes_backslash(): +2 | """Sum\\mary.""" + | ^^^^^^^^^^^^^^^^ D301 + | + +D301.py:10:5: D301 Use `r"""` if any backslashes in a docstring + | + 9 | def double_quotes_backslash_uppercase(): +10 | R"""Sum\\mary.""" + | ^^^^^^^^^^^^^^^^^ D301 + | + +