Try both 'Raises' section styles when convention is unspecified (#12649)

## Summary

Closes https://github.com/astral-sh/ruff/issues/12647.
This commit is contained in:
Charlie Marsh 2024-08-02 21:04:46 -04:00 committed by GitHub
parent daccb3f4f3
commit 38e178e914
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 31 additions and 8 deletions

View file

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

View file

@ -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(),
@ -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() {
@ -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
}
}
}
}
@ -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 = {