Add tests for escape-sequence-in-docstring (#5362)

## Summary

Looks like I added a regression in #5360. This PR fixes it and adds
dedicated tests to avoid it in the future.
This commit is contained in:
Charlie Marsh 2023-06-25 22:42:12 -04:00 committed by GitHub
parent 18c73c1f9b
commit dce6a046b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 3 deletions

View file

@ -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
"""

View file

@ -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());

View file

@ -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));

View file

@ -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
|