Skip to content

Commit

Permalink
[pydoclint] Deduplicate collected exceptions after traversing funct…
Browse files Browse the repository at this point in the history
…ion bodies (#12642)
  • Loading branch information
AlexWaygood authored Aug 2, 2024
1 parent c858afe commit daccb3f
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,17 @@ def calculate_speed(distance: float, time: float) -> float:
except Exception as e:
print(f"Oh no, we encountered {e}")
raise


def foo():
"""Foo.
Returns:
42: int.
"""
if True:
raise TypeError # DOC501
else:
raise TypeError # no DOC501 here because we already emitted a diagnostic for the earlier `raise TypeError`
raise ValueError # DOC501
return 42
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,19 @@ def calculate_speed(distance: float, time: float) -> float:
except Exception as e:
print(f"Oh no, we encountered {e}")
raise


def foo():
"""Foo.
Returns
-------
int
42
"""
if True:
raise TypeError # DOC501
else:
raise TypeError # no DOC501 here because we already emitted a diagnostic for the earlier `raise TypeError`
raise ValueError # DOC501
return 42
27 changes: 24 additions & 3 deletions crates/ruff_linter/src/rules/pydoclint/rules/check_docstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,10 +523,31 @@ impl<'a> BodyVisitor<'a> {
}

fn finish(self) -> BodyEntries<'a> {
let BodyVisitor {
returns,
yields,
mut raised_exceptions,
..
} = self;

// Deduplicate exceptions collected:
// no need to complain twice about `raise TypeError` not being documented
// just because there are two separate `raise TypeError` statements in the function
raised_exceptions.sort_unstable_by(|left, right| {
left.qualified_name
.segments()
.cmp(right.qualified_name.segments())
.then_with(|| left.start().cmp(&right.start()))
.then_with(|| left.end().cmp(&right.end()))
});
raised_exceptions.dedup_by(|left, right| {
left.qualified_name.segments() == right.qualified_name.segments()
});

BodyEntries {
returns: self.returns,
yields: self.yields,
raised_exceptions: self.raised_exceptions,
returns,
yields,
raised_exceptions,
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,24 @@ DOC501_google.py:213:9: DOC501 Raised exception `ZeroDivisionError` missing from
215 | print("Not a number? Shame on you!")
|
= help: Add `ZeroDivisionError` to the docstring

DOC501_google.py:244:15: DOC501 Raised exception `TypeError` missing from docstring
|
242 | """
243 | if True:
244 | raise TypeError # DOC501
| ^^^^^^^^^ DOC501
245 | else:
246 | raise TypeError # no DOC501 here because we already emitted a diagnostic for the earlier `raise TypeError`
|
= help: Add `TypeError` to the docstring

DOC501_google.py:247:11: DOC501 Raised exception `ValueError` missing from docstring
|
245 | else:
246 | raise TypeError # no DOC501 here because we already emitted a diagnostic for the earlier `raise TypeError`
247 | raise ValueError # DOC501
| ^^^^^^^^^^ DOC501
248 | return 42
|
= help: Add `ValueError` to the docstring
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,24 @@ DOC501_numpy.py:111:9: DOC501 Raised exception `TypeError` missing from docstrin
| ^^^^^ DOC501
|
= help: Add `TypeError` to the docstring

DOC501_numpy.py:147:15: DOC501 Raised exception `TypeError` missing from docstring
|
145 | """
146 | if True:
147 | raise TypeError # DOC501
| ^^^^^^^^^ DOC501
148 | else:
149 | raise TypeError # no DOC501 here because we already emitted a diagnostic for the earlier `raise TypeError`
|
= help: Add `TypeError` to the docstring

DOC501_numpy.py:150:11: DOC501 Raised exception `ValueError` missing from docstring
|
148 | else:
149 | raise TypeError # no DOC501 here because we already emitted a diagnostic for the earlier `raise TypeError`
150 | raise ValueError # DOC501
| ^^^^^^^^^^ DOC501
151 | return 42
|
= help: Add `ValueError` to the docstring

0 comments on commit daccb3f

Please sign in to comment.