mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-02 04:48:06 +00:00
[pydoclint] Deduplicate collected exceptions after traversing function bodies (#12642)
This commit is contained in:
parent
c858afe03a
commit
daccb3f4f3
5 changed files with 96 additions and 3 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue