Skip to content

Commit

Permalink
handle exception calls, i.e., Exception()
Browse files Browse the repository at this point in the history
  • Loading branch information
augustelalande committed May 20, 2024
1 parent 596bf41 commit 3413e59
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@ def calculate_speed(distance: float, time: float) -> float:
raise AnotherError


# DAR401
def calculate_speed(distance: float, time: float) -> float:
"""Calculate speed as distance divided by time.
Args:
distance: Distance traveled.
time: Time spent traveling.
Returns:
Speed as distance divided by time.
"""
raise AnotherError()


# DAR401, but can't resolve the error
def calculate_speed(distance: float, time: float) -> float:
"""Calculate speed as distance divided by time.
Expand Down
37 changes: 28 additions & 9 deletions crates/ruff_linter/src/rules/darglint/rules/check_docstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,16 +221,35 @@ impl<'a> BodyVisitor<'a> {
impl Visitor<'_> for BodyVisitor<'_> {
fn visit_stmt(&mut self, stmt: &Stmt) {
if let Stmt::Raise(ast::StmtRaise { exc: Some(exc), .. }) = stmt {
if let Expr::Name(ast::ExprName { id, range, .. }) = exc.as_ref() {
// SemanticModel will resolve qualified_name for local Class definitions,
// or imported definitions, but not variables which we want to ignore.
if self.semantic.resolve_qualified_name(exc.as_ref()).is_some() {
self.raised_exceptions.push(Entry {
id: id.to_string(),
range: *range,
});
match exc.as_ref() {
Expr::Name(ast::ExprName { id, range, .. }) => {
// SemanticModel will resolve qualified_name for local Class definitions,
// or imported definitions, but not variables which we want to ignore.
if self.semantic.resolve_qualified_name(exc.as_ref()).is_some() {
self.raised_exceptions.push(Entry {
id: id.to_string(),
range: *range,
});
}
}
}
Expr::Call(ast::ExprCall { func, range, .. }) => {
if let Expr::Name(ast::ExprName { id, .. }) = func.as_ref() {
// SemanticModel will resolve qualified_name for local Class definitions,
// or imported definitions, but not variables which we want to ignore.
if self
.semantic
.resolve_qualified_name(func.as_ref())
.is_some()
{
self.raised_exceptions.push(Entry {
id: id.to_string(),
range: *range,
});
}
}
}
_ => {}
};
}
visitor::walk_stmt(self, stmt);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,11 @@ DAR401_google.py:114:11: DAR401 Raised exception `AnotherError` missing from doc
114 | raise AnotherError
| ^^^^^^^^^^^^ DAR401
|

DAR401_google.py:128:11: DAR401 Raised exception `AnotherError` missing from docstring
|
126 | Speed as distance divided by time.
127 | """
128 | raise AnotherError()
| ^^^^^^^^^^^^^^ DAR401
|

0 comments on commit 3413e59

Please sign in to comment.