mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 11:59:10 +00:00
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:
parent
18c73c1f9b
commit
dce6a046b0
4 changed files with 51 additions and 3 deletions
29
crates/ruff/resources/test/fixtures/pydocstyle/D301.py
vendored
Normal file
29
crates/ruff/resources/test/fixtures/pydocstyle/D301.py
vendored
Normal 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
|
||||||
|
"""
|
|
@ -82,6 +82,7 @@ mod tests {
|
||||||
#[test_case(Rule::SectionUnderlineNotOverIndented, Path::new("sections.py"))]
|
#[test_case(Rule::SectionUnderlineNotOverIndented, Path::new("sections.py"))]
|
||||||
#[test_case(Rule::OverloadWithDocstring, Path::new("D.py"))]
|
#[test_case(Rule::OverloadWithDocstring, Path::new("D.py"))]
|
||||||
#[test_case(Rule::EscapeSequenceInDocstring, 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"))]
|
#[test_case(Rule::TripleSingleQuotes, Path::new("D.py"))]
|
||||||
fn rules(rule_code: Rule, path: &Path) -> Result<()> {
|
fn rules(rule_code: Rule, path: &Path) -> Result<()> {
|
||||||
let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
|
let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
|
||||||
|
|
|
@ -54,14 +54,14 @@ impl Violation for EscapeSequenceInDocstring {
|
||||||
|
|
||||||
/// D301
|
/// D301
|
||||||
pub(crate) fn backslashes(checker: &mut Checker, docstring: &Docstring) {
|
pub(crate) fn backslashes(checker: &mut Checker, docstring: &Docstring) {
|
||||||
let body = docstring.body();
|
|
||||||
|
|
||||||
// Docstring is already raw.
|
// 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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Docstring contains at least one backslash.
|
// Docstring contains at least one backslash.
|
||||||
|
let body = docstring.body();
|
||||||
let bytes = body.as_bytes();
|
let bytes = body.as_bytes();
|
||||||
if memchr_iter(b'\\', bytes).any(|position| {
|
if memchr_iter(b'\\', bytes).any(|position| {
|
||||||
let escaped_char = bytes.get(position.saturating_add(1));
|
let escaped_char = bytes.get(position.saturating_add(1));
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue