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
  • Loading branch information
AlexWaygood committed Aug 2, 2024
1 parent 1c311e4 commit 57d7cbc
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,17 @@ def foo(bar: int):
something.SomeError: Wow.
"""
raise something.SomeError


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 @@ -76,3 +76,19 @@ def calculate_speed(distance: float, time: float) -> float:
raise FasterThanLightError from exc
except:
raise ValueError


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
24 changes: 21 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 @@ -520,10 +520,28 @@ impl<'a> BodyVisitor<'a> {
}

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

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 @@ -56,3 +56,24 @@ DOC501_google.py:139:11: DOC501 Raised exception `SomeError` missing from docstr
| ^^^^^^^^^^^^^^^^^^^ DOC501
|
= help: Add `SomeError` to the docstring

DOC501_google.py:202:15: DOC501 Raised exception `TypeError` missing from docstring
|
200 | """
201 | if True:
202 | raise TypeError # DOC501
| ^^^^^^^^^ DOC501
203 | else:
204 | 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:205:11: DOC501 Raised exception `ValueError` missing from docstring
|
203 | else:
204 | raise TypeError # no DOC501 here because we already emitted a diagnostic for the earlier `raise TypeError`
205 | raise ValueError # DOC501
| ^^^^^^^^^^ DOC501
206 | return 42
|
= help: Add `ValueError` to the docstring
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,24 @@ DOC501_numpy.py:78:15: DOC501 Raised exception `ValueError` missing from docstri
| ^^^^^^^^^^ DOC501
|
= help: Add `ValueError` to the docstring

DOC501_numpy.py:90:15: DOC501 Raised exception `TypeError` missing from docstring
|
88 | """
89 | if True:
90 | raise TypeError # DOC501
| ^^^^^^^^^ DOC501
91 | else:
92 | 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:93:11: DOC501 Raised exception `ValueError` missing from docstring
|
91 | else:
92 | raise TypeError # no DOC501 here because we already emitted a diagnostic for the earlier `raise TypeError`
93 | raise ValueError # DOC501
| ^^^^^^^^^^ DOC501
94 | return 42
|
= help: Add `ValueError` to the docstring

0 comments on commit 57d7cbc

Please sign in to comment.