mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-03 15:15:33 +00:00
Allow overhang in Google-style docstring arguments (#1668)
Resolves #1662.
This commit is contained in:
parent
e6611c4830
commit
d34e6c02a1
4 changed files with 45 additions and 28 deletions
|
@ -1413,24 +1413,56 @@ fn missing_args(checker: &mut Checker, docstring: &Docstring, docstrings_args: &
|
||||||
|
|
||||||
// See: `GOOGLE_ARGS_REGEX` in `pydocstyle/checker.py`.
|
// See: `GOOGLE_ARGS_REGEX` in `pydocstyle/checker.py`.
|
||||||
static GOOGLE_ARGS_REGEX: Lazy<Regex> =
|
static GOOGLE_ARGS_REGEX: Lazy<Regex> =
|
||||||
Lazy::new(|| Regex::new(r"^\s*(\*?\*?\w+)\s*(\(.*?\))?\s*:.+").unwrap());
|
Lazy::new(|| Regex::new(r"^\s*(\*?\*?\w+)\s*(\(.*?\))?\s*:\n?\s*.+").unwrap());
|
||||||
|
|
||||||
fn args_section(checker: &mut Checker, docstring: &Docstring, context: &SectionContext) {
|
fn args_section(checker: &mut Checker, docstring: &Docstring, context: &SectionContext) {
|
||||||
let mut matches = Vec::new();
|
if context.following_lines.is_empty() {
|
||||||
for line in context.following_lines {
|
missing_args(checker, docstring, &FxHashSet::default());
|
||||||
if let Some(captures) = GOOGLE_ARGS_REGEX.captures(line) {
|
return;
|
||||||
matches.push(captures);
|
}
|
||||||
|
|
||||||
|
// Normalize leading whitespace, by removing any lines with less indentation
|
||||||
|
// than the first.
|
||||||
|
let leading_space = whitespace::leading_space(context.following_lines[0]);
|
||||||
|
let relevant_lines = context
|
||||||
|
.following_lines
|
||||||
|
.iter()
|
||||||
|
.filter(|line| line.starts_with(leading_space) || line.is_empty())
|
||||||
|
.join("\n");
|
||||||
|
let args_content = textwrap::dedent(&relevant_lines);
|
||||||
|
|
||||||
|
// Reformat each section.
|
||||||
|
let mut args_sections: Vec<String> = vec![];
|
||||||
|
for line in args_content.trim().lines() {
|
||||||
|
if line.chars().next().map_or(true, char::is_whitespace) {
|
||||||
|
// This is a continuation of the documentation for the previous parameter,
|
||||||
|
// because it starts with whitespace.
|
||||||
|
if let Some(last) = args_sections.last_mut() {
|
||||||
|
last.push_str(line);
|
||||||
|
last.push('\n');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// This line is the start of documentation for the next parameter, because it
|
||||||
|
// doesn't start with any whitespace.
|
||||||
|
let mut line = line.to_string();
|
||||||
|
line.push('\n');
|
||||||
|
args_sections.push(line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
missing_args(
|
// Extract the argument name from each section.
|
||||||
checker,
|
let mut matches = Vec::new();
|
||||||
docstring,
|
for section in &args_sections {
|
||||||
&matches
|
if let Some(captures) = GOOGLE_ARGS_REGEX.captures(section) {
|
||||||
.iter()
|
matches.push(captures);
|
||||||
.filter_map(|captures| captures.get(1).map(|arg_name| arg_name.as_str()))
|
}
|
||||||
.collect(),
|
}
|
||||||
);
|
let docstrings_args = matches
|
||||||
|
.iter()
|
||||||
|
.filter_map(|captures| captures.get(1).map(|arg_name| arg_name.as_str()))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
missing_args(checker, docstring, &docstrings_args);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parameters_section(checker: &mut Checker, docstring: &Docstring, context: &SectionContext) {
|
fn parameters_section(checker: &mut Checker, docstring: &Docstring, context: &SectionContext) {
|
||||||
|
|
|
@ -75,18 +75,6 @@ expression: checks
|
||||||
column: 11
|
column: 11
|
||||||
fix: ~
|
fix: ~
|
||||||
parent: ~
|
parent: ~
|
||||||
- kind:
|
|
||||||
DocumentAllArguments:
|
|
||||||
- skip
|
|
||||||
- verbose
|
|
||||||
location:
|
|
||||||
row: 370
|
|
||||||
column: 4
|
|
||||||
end_location:
|
|
||||||
row: 382
|
|
||||||
column: 11
|
|
||||||
fix: ~
|
|
||||||
parent: ~
|
|
||||||
- kind:
|
- kind:
|
||||||
DocumentAllArguments:
|
DocumentAllArguments:
|
||||||
- y
|
- y
|
||||||
|
|
|
@ -16,7 +16,6 @@ expression: checks
|
||||||
parent: ~
|
parent: ~
|
||||||
- kind:
|
- kind:
|
||||||
DocumentAllArguments:
|
DocumentAllArguments:
|
||||||
- x
|
|
||||||
- y
|
- y
|
||||||
- z
|
- z
|
||||||
location:
|
location:
|
||||||
|
@ -29,7 +28,6 @@ expression: checks
|
||||||
parent: ~
|
parent: ~
|
||||||
- kind:
|
- kind:
|
||||||
DocumentAllArguments:
|
DocumentAllArguments:
|
||||||
- x
|
|
||||||
- y
|
- y
|
||||||
- z
|
- z
|
||||||
location:
|
location:
|
||||||
|
|
|
@ -4,7 +4,6 @@ expression: checks
|
||||||
---
|
---
|
||||||
- kind:
|
- kind:
|
||||||
DocumentAllArguments:
|
DocumentAllArguments:
|
||||||
- x
|
|
||||||
- y
|
- y
|
||||||
- z
|
- z
|
||||||
location:
|
location:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue