Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pylint] Implement misplaced-bare-raise (E0704) #7961

Merged
merged 3 commits into from Oct 17, 2023

Conversation

clemux
Copy link
Contributor

@clemux clemux commented Oct 14, 2023

Related issue: #970

Summary

What it does

This rule triggers an error when a bare raise statement is not in an except or finally block.

Why is this bad?

If raise statement is not in an except or finally block, there is no active exception to
re-raise, so it will fail with a RuntimeError exception.

Example

def validate_positive(x):
   if x <= 0:
       raise

Use instead:

def validate_positive(x):
   if x <= 0:
       raise ValueError(f"{x} is not positive")

Test Plan

Added unit test and snapshot.
Manually compared ruff and pylint outputs on pylint's tests.

References

@clemux clemux force-pushed the pylint_misplaced_bare_raise branch 3 times, most recently from a9a4e0c to 4b1ece8 Compare October 14, 2023 18:21
@github-actions
Copy link

github-actions bot commented Oct 14, 2023

PR Check Results

Ecosystem

✅ ecosystem check detected no changes.

@charliermarsh
Copy link
Member

Thanks! Do you mind taking a look at some of the violations flagged in the ecosystem check, and seeing how they compare to pylint's behavior?

@clemux
Copy link
Contributor Author

clemux commented Oct 14, 2023

They are the same with pylint. I had to run pylint on each file though, I'm not sure what I'm doing wrong. For example, with pip:

(venv) clemux@Nardole:~/dev/pip/src$ pylint --disable=all --enable=misplaced-bare-raise .
(venv) clemux@Nardole:~/dev/pip/src$ pylint --disable=all --enable=misplaced-bare-raise pip/_internal/utils/misc.py
************* Module pip._internal.utils.misc
pip/_internal/utils/misc.py:157:4: E0704: The raise statement is not inside an except clause (misplaced-bare-raise)

------------------------------------------------------------------
Your code has been rated at 9.85/10 (previous run: 9.85/10, +0.00)

Comment on lines 42 to 58
#[derive(Default)]
struct RaiseFinder<'a> {
raises: Vec<&'a Stmt>,
}

impl<'a, 'b> StatementVisitor<'b> for RaiseFinder<'a>
where
'b: 'a,
{
fn visit_stmt(&mut self, stmt: &'b Stmt) {
match stmt {
Stmt::Raise(_) => self.raises.push(stmt),
_ => statement_visitor::walk_stmt(self, stmt),
}
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to use ruff_python_ast::helpers::RaiseStatementVisitor, but it doesn't store the raise statement itself.

It's only used in two rules, so maybe I can refactor that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah -- can we change RaiseStatementVisitor to store Vec<&'a ast::StmtRaise>?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall I do that in a separate PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be cool with including it here, but either is fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed anymore, so I guess I won't touch RaiseStatementVisitor.

@clemux clemux force-pushed the pylint_misplaced_bare_raise branch from 4b1ece8 to 7adbd0e Compare October 16, 2023 18:47
@clemux clemux force-pushed the pylint_misplaced_bare_raise branch from 7adbd0e to d887d2a Compare October 16, 2023 20:47
@clemux clemux force-pushed the pylint_misplaced_bare_raise branch from d887d2a to 6f33ee4 Compare October 16, 2023 20:57
@charliermarsh charliermarsh self-requested a review October 17, 2023 02:09
@charliermarsh charliermarsh added the rule Implementing or modifying a lint rule label Oct 17, 2023
@charliermarsh charliermarsh enabled auto-merge (squash) October 17, 2023 03:05
@charliermarsh charliermarsh merged commit bf0e578 into astral-sh:main Oct 17, 2023
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants