From 7e37d0c8346bacc254a189aae8beb164dc3e08e0 Mon Sep 17 00:00:00 2001 From: augustelalande Date: Sun, 21 Jul 2024 14:01:46 -0400 Subject: [PATCH 1/4] fix panic --- .../resources/test/fixtures/pydoclint/DOC501.py | 8 ++++++++ crates/ruff_linter/src/rules/pydoclint/mod.rs | 11 +++++++++++ .../src/rules/pydoclint/rules/check_docstring.rs | 2 +- ..._tests__docstring-missing-exception_DOC501.py.snap | 4 ++++ 4 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 crates/ruff_linter/resources/test/fixtures/pydoclint/DOC501.py create mode 100644 crates/ruff_linter/src/rules/pydoclint/snapshots/ruff_linter__rules__pydoclint__tests__docstring-missing-exception_DOC501.py.snap diff --git a/crates/ruff_linter/resources/test/fixtures/pydoclint/DOC501.py b/crates/ruff_linter/resources/test/fixtures/pydoclint/DOC501.py new file mode 100644 index 0000000000000..fd3a371080a3f --- /dev/null +++ b/crates/ruff_linter/resources/test/fixtures/pydoclint/DOC501.py @@ -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]) + """ diff --git a/crates/ruff_linter/src/rules/pydoclint/mod.rs b/crates/ruff_linter/src/rules/pydoclint/mod.rs index 539f310b91e51..99fff1322d304 100644 --- a/crates/ruff_linter/src/rules/pydoclint/mod.rs +++ b/crates/ruff_linter/src/rules/pydoclint/mod.rs @@ -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<()> { diff --git a/crates/ruff_linter/src/rules/pydoclint/rules/check_docstring.rs b/crates/ruff_linter/src/rules/pydoclint/rules/check_docstring.rs index e85d91fd1cca2..cb8227666c106 100644 --- a/crates/ruff_linter/src/rules/pydoclint/rules/check_docstring.rs +++ b/crates/ruff_linter/src/rules/pydoclint/rules/check_docstring.rs @@ -210,7 +210,7 @@ fn parse_entries_numpy(content: &str) -> Vec { for potential in split { if let Some(first_char) = potential.chars().nth(indentation) { if !first_char.is_whitespace() { - let entry = potential[indentation..].trim(); + let entry = potential.trim(); entries.push(QualifiedName::user_defined(entry)); } } diff --git a/crates/ruff_linter/src/rules/pydoclint/snapshots/ruff_linter__rules__pydoclint__tests__docstring-missing-exception_DOC501.py.snap b/crates/ruff_linter/src/rules/pydoclint/snapshots/ruff_linter__rules__pydoclint__tests__docstring-missing-exception_DOC501.py.snap new file mode 100644 index 0000000000000..d3c56b22a3cc2 --- /dev/null +++ b/crates/ruff_linter/src/rules/pydoclint/snapshots/ruff_linter__rules__pydoclint__tests__docstring-missing-exception_DOC501.py.snap @@ -0,0 +1,4 @@ +--- +source: crates/ruff_linter/src/rules/pydoclint/mod.rs +--- + From 1c8dde41b1bbc0c124303a914695dfad76f36595 Mon Sep 17 00:00:00 2001 From: augustelalande Date: Sun, 21 Jul 2024 14:49:26 -0400 Subject: [PATCH 2/4] use lines instead of split --- .../src/rules/pydoclint/rules/check_docstring.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/ruff_linter/src/rules/pydoclint/rules/check_docstring.rs b/crates/ruff_linter/src/rules/pydoclint/rules/check_docstring.rs index cb8227666c106..924f65bef1298 100644 --- a/crates/ruff_linter/src/rules/pydoclint/rules/check_docstring.rs +++ b/crates/ruff_linter/src/rules/pydoclint/rules/check_docstring.rs @@ -180,7 +180,7 @@ fn parse_entries(content: &str, style: SectionStyle) -> Vec { /// ``` fn parse_entries_google(content: &str) -> Vec { let mut entries: Vec = Vec::new(); - for potential in content.split('\n') { + for potential in content.lines() { let Some(colon_idx) = potential.find(':') else { continue; }; @@ -202,12 +202,12 @@ fn parse_entries_google(content: &str) -> Vec { /// ``` fn parse_entries_numpy(content: &str) -> Vec { let mut entries: Vec = 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 { + for potential in lines { if let Some(first_char) = potential.chars().nth(indentation) { if !first_char.is_whitespace() { let entry = potential.trim(); From 5dcfd195f58317109d2f5c81deb154fadbf72d74 Mon Sep 17 00:00:00 2001 From: augustelalande Date: Sun, 21 Jul 2024 15:05:04 -0400 Subject: [PATCH 3/4] use strip_prefix --- .../src/rules/pydoclint/rules/check_docstring.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/crates/ruff_linter/src/rules/pydoclint/rules/check_docstring.rs b/crates/ruff_linter/src/rules/pydoclint/rules/check_docstring.rs index 924f65bef1298..1a648f39c543c 100644 --- a/crates/ruff_linter/src/rules/pydoclint/rules/check_docstring.rs +++ b/crates/ruff_linter/src/rules/pydoclint/rules/check_docstring.rs @@ -208,10 +208,11 @@ fn parse_entries_numpy(content: &str) -> Vec { }; let indentation = dashes.len() - dashes.trim_start().len(); for potential in lines { - if let Some(first_char) = potential.chars().nth(indentation) { - if !first_char.is_whitespace() { - let entry = potential.trim(); - entries.push(QualifiedName::user_defined(entry)); + if let Some(entry) = potential.strip_prefix(&dashes[..indentation]) { + if let Some(first_char) = entry.chars().next() { + if !first_char.is_whitespace() { + entries.push(QualifiedName::user_defined(entry.trim_end())); + } } } } From 91abe4faaf76edbed44429a23821c1e540142b2f Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sun, 21 Jul 2024 15:26:01 -0400 Subject: [PATCH 4/4] Rename --- .../ruff_linter/src/rules/pydoclint/rules/check_docstring.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/ruff_linter/src/rules/pydoclint/rules/check_docstring.rs b/crates/ruff_linter/src/rules/pydoclint/rules/check_docstring.rs index 1a648f39c543c..10d486bd3fb5a 100644 --- a/crates/ruff_linter/src/rules/pydoclint/rules/check_docstring.rs +++ b/crates/ruff_linter/src/rules/pydoclint/rules/check_docstring.rs @@ -206,9 +206,9 @@ fn parse_entries_numpy(content: &str) -> Vec { let Some(dashes) = lines.next() else { return entries; }; - let indentation = dashes.len() - dashes.trim_start().len(); + let indentation = &dashes[..dashes.len() - dashes.trim_start().len()]; for potential in lines { - if let Some(entry) = potential.strip_prefix(&dashes[..indentation]) { + 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()));