Skip to content

Commit

Permalink
[pydoclint] Fix DOC501 panic astral-sh#12428 (astral-sh#12435)
Browse files Browse the repository at this point in the history
## Summary

Fix panic reported in astral-sh#12428. Where a string would sometimes get split
within a character boundary. This bypasses the need to split the string.

This does not guarantee the correct formatting of the docstring, but
neither did the previous implementation.

Resolves astral-sh#12428 

## Test Plan

Test case added to fixture
  • Loading branch information
augustelalande authored Jul 21, 2024
1 parent 0532436 commit 3a742c1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# https://github.com/astral-sh/ruff/issues/12428
def parse_bool(x, default=_parse_bool_sentinel):
"""Parse a boolean value
bool or type(default)
Raises
`ValueError`
ê>>> all(parse_bool(x) for x in [True, "yes", "Yes", "true", "True", "on", "ON", "1", 1])
"""
11 changes: 11 additions & 0 deletions crates/ruff_linter/src/rules/pydoclint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ mod tests {
use crate::test::test_path;
use crate::{assert_messages, settings};

#[test_case(Rule::DocstringMissingException, Path::new("DOC501.py"))]
fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.as_ref(), path.to_string_lossy());
let diagnostics = test_path(
Path::new("pydoclint").join(path).as_path(),
&settings::LinterSettings::for_rule(rule_code),
)?;
assert_messages!(snapshot, diagnostics);
Ok(())
}

#[test_case(Rule::DocstringMissingException, Path::new("DOC501_google.py"))]
#[test_case(Rule::DocstringExtraneousException, Path::new("DOC502_google.py"))]
fn rules_google_style(rule_code: Rule, path: &Path) -> Result<()> {
Expand Down
19 changes: 10 additions & 9 deletions crates/ruff_linter/src/rules/pydoclint/rules/check_docstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ fn parse_entries(content: &str, style: SectionStyle) -> Vec<QualifiedName> {
/// ```
fn parse_entries_google(content: &str) -> Vec<QualifiedName> {
let mut entries: Vec<QualifiedName> = Vec::new();
for potential in content.split('\n') {
for potential in content.lines() {
let Some(colon_idx) = potential.find(':') else {
continue;
};
Expand All @@ -202,16 +202,17 @@ fn parse_entries_google(content: &str) -> Vec<QualifiedName> {
/// ```
fn parse_entries_numpy(content: &str) -> Vec<QualifiedName> {
let mut entries: Vec<QualifiedName> = Vec::new();
let mut split = content.split('\n');
let Some(dashes) = split.next() else {
let mut lines = content.lines();
let Some(dashes) = lines.next() else {
return entries;
};
let indentation = dashes.len() - dashes.trim_start().len();
for potential in split {
if let Some(first_char) = potential.chars().nth(indentation) {
if !first_char.is_whitespace() {
let entry = potential[indentation..].trim();
entries.push(QualifiedName::user_defined(entry));
let indentation = &dashes[..dashes.len() - dashes.trim_start().len()];
for potential in lines {
if let Some(entry) = potential.strip_prefix(indentation) {
if let Some(first_char) = entry.chars().next() {
if !first_char.is_whitespace() {
entries.push(QualifiedName::user_defined(entry.trim_end()));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
source: crates/ruff_linter/src/rules/pydoclint/mod.rs
---

0 comments on commit 3a742c1

Please sign in to comment.