Skip to content

Commit

Permalink
Try both 'Raises' section styles when convention is unspecified (#12649)
Browse files Browse the repository at this point in the history
## Summary

Closes #12647.
  • Loading branch information
charliermarsh authored Aug 3, 2024
1 parent daccb3f commit 38e178e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
12 changes: 12 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pydoclint/DOC501.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,15 @@ def parse_bool(x, default=_parse_bool_sentinel):
`ValueError`
ê>>> all(parse_bool(x) for x in [True, "yes", "Yes", "true", "True", "on", "ON", "1", 1])
"""


# https://github.com/astral-sh/ruff/issues/12647
def get_bar(self) -> str:
"""Print and return bar.
Raises:
ValueError: bar is not bar.
Returns:
str: bar value.
"""
27 changes: 19 additions & 8 deletions crates/ruff_linter/src/rules/pydoclint/rules/check_docstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ impl Ranged for RaisesSection<'_> {
impl<'a> RaisesSection<'a> {
/// Return the raised exceptions for the docstring, or `None` if the docstring does not contain
/// a `Raises` section.
fn from_section(section: &SectionContext<'a>, style: SectionStyle) -> Self {
fn from_section(section: &SectionContext<'a>, style: Option<SectionStyle>) -> Self {
Self {
raised_exceptions: parse_entries(section.following_lines_str(), style),
range: section.range(),
Expand All @@ -393,7 +393,7 @@ struct DocstringSections<'a> {
}

impl<'a> DocstringSections<'a> {
fn from_sections(sections: &'a SectionContexts, style: SectionStyle) -> Self {
fn from_sections(sections: &'a SectionContexts, style: Option<SectionStyle>) -> Self {
let mut docstring_sections = Self::default();
for section in sections {
match section.kind() {
Expand All @@ -414,10 +414,21 @@ impl<'a> DocstringSections<'a> {
}

/// Parse the entries in a `Raises` section of a docstring.
fn parse_entries(content: &str, style: SectionStyle) -> Vec<QualifiedName> {
///
/// Attempts to parse using the specified [`SectionStyle`], falling back to the other style if no
/// entries are found.
fn parse_entries(content: &str, style: Option<SectionStyle>) -> Vec<QualifiedName> {
match style {
SectionStyle::Google => parse_entries_google(content),
SectionStyle::Numpy => parse_entries_numpy(content),
Some(SectionStyle::Google) => parse_entries_google(content),
Some(SectionStyle::Numpy) => parse_entries_numpy(content),
None => {
let entries = parse_entries_google(content);
if entries.is_empty() {
parse_entries_numpy(content)
} else {
entries
}
}
}
}

Expand Down Expand Up @@ -660,12 +671,12 @@ pub(crate) fn check_docstring(
// Prioritize the specified convention over the determined style.
let docstring_sections = match convention {
Some(Convention::Google) => {
DocstringSections::from_sections(section_contexts, SectionStyle::Google)
DocstringSections::from_sections(section_contexts, Some(SectionStyle::Google))
}
Some(Convention::Numpy) => {
DocstringSections::from_sections(section_contexts, SectionStyle::Numpy)
DocstringSections::from_sections(section_contexts, Some(SectionStyle::Numpy))
}
_ => DocstringSections::from_sections(section_contexts, section_contexts.style()),
Some(Convention::Pep257) | None => DocstringSections::from_sections(section_contexts, None),
};

let body_entries = {
Expand Down

0 comments on commit 38e178e

Please sign in to comment.