mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-14 12:29:40 +00:00
fix-18959
This commit is contained in:
parent
2c916562ba
commit
d97f7ea763
2 changed files with 51 additions and 6 deletions
|
@ -81,3 +81,16 @@ def calculate_speed(distance: float, time: float) -> float:
|
|||
except TypeError:
|
||||
print("Not a number? Shame on you!")
|
||||
raise
|
||||
|
||||
# DOC502 regression for Sphinx directive after Raises (issue #18959)
|
||||
def foo():
|
||||
"""First line.
|
||||
|
||||
Raises:
|
||||
ValueError:
|
||||
some text
|
||||
|
||||
.. versionadded:: 0.7.0
|
||||
The ``init_kwargs`` argument.
|
||||
"""
|
||||
raise ValueError
|
||||
|
|
|
@ -476,13 +476,45 @@ fn parse_entries(content: &str, style: Option<SectionStyle>) -> Vec<QualifiedNam
|
|||
/// ```
|
||||
fn parse_entries_google(content: &str) -> Vec<QualifiedName<'_>> {
|
||||
let mut entries: Vec<QualifiedName> = Vec::new();
|
||||
for potential in content.lines() {
|
||||
let Some(colon_idx) = potential.find(':') else {
|
||||
continue;
|
||||
};
|
||||
let entry = potential[..colon_idx].trim();
|
||||
entries.push(QualifiedName::user_defined(entry));
|
||||
|
||||
// Determine the indentation of the entries from the first non-empty line.
|
||||
// Google-style entries are indented relative to the "Raises:" header, e.g.:
|
||||
// " ValueError: explanation".
|
||||
let lines = content.lines();
|
||||
let mut expected_indent: Option<&str> = None;
|
||||
|
||||
for raw in lines {
|
||||
let line = raw.trim_end_matches('\r');
|
||||
|
||||
// Stop if we encounter an unindented line or a Sphinx directive starting with ".. ".
|
||||
if !line.trim().is_empty() {
|
||||
// Compute indentation of current line
|
||||
let indent_len = line.len() - line.trim_start().len();
|
||||
let indent = &line[..indent_len];
|
||||
|
||||
// If this looks like a Sphinx directive or any unindented content, the section ends
|
||||
if indent_len == 0 || line.trim_start().starts_with(".. ") {
|
||||
break;
|
||||
}
|
||||
|
||||
// Establish expected indentation based on the first valid entry line
|
||||
if expected_indent.is_none() {
|
||||
expected_indent = Some(indent);
|
||||
} else if Some(indent) != expected_indent {
|
||||
// Different indentation likely starts a new sub-block; stop collecting
|
||||
break;
|
||||
}
|
||||
|
||||
// Parse only lines that contain a colon and where the token before the colon is non-empty
|
||||
if let Some(colon_idx) = line.find(':') {
|
||||
let entry = line[..colon_idx].trim();
|
||||
if !entry.is_empty() {
|
||||
entries.push(QualifiedName::user_defined(entry));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
entries
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue