Skip to content

Commit

Permalink
[flake8-raise] Add Plugin and RSE102 Rule (#2354)
Browse files Browse the repository at this point in the history
  • Loading branch information
saadmk11 authored Jan 31, 2023
1 parent 84a8b62 commit 7c1a6bc
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 1 deletion.
25 changes: 25 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -1005,3 +1005,28 @@ are:
See the License for the specific language governing permissions and
limitations under the License.
"""

- flake8-raise, licensed as follows:
"""
MIT License

Copyright (c) 2020 Jon Dufresne

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ developer of [Zulip](https://github.com/zulip/zulip):
1. [pygrep-hooks (PGH)](#pygrep-hooks-pgh)
1. [Pylint (PL)](#pylint-pl)
1. [tryceratops (TRY)](#tryceratops-try)
1. [flake8-raise (RSE)](#flake8-raise-rse)
1. [Ruff-specific rules (RUF)](#ruff-specific-rules-ruf)<!-- End auto-generated table of contents. -->
1. [Editor Integrations](#editor-integrations)
1. [FAQ](#faq)
Expand Down Expand Up @@ -1357,6 +1358,14 @@ For more, see [tryceratops](https://pypi.org/project/tryceratops/1.1.0/) on PyPI
| TRY301 | raise-within-try | Abstract `raise` to an inner function | |
| TRY400 | error-instead-of-exception | Use `logging.exception` instead of `logging.error` | |

### flake8-raise (RSE)

For more, see [flake8-raise](https://pypi.org/project/flake8-raise/) on PyPI.

| Code | Name | Message | Fix |
| ---- | ---- | ------- | --- |
| RSE102 | unnecessary-paren-on-raise-exception | Unnecessary parentheses on raised exception | |

### Ruff-specific rules (RUF)

| Code | Name | Message | Fix |
Expand Down Expand Up @@ -1648,6 +1657,7 @@ natively, including:
- [`flake8-print`](https://pypi.org/project/flake8-print/)
- [`flake8-pytest-style`](https://pypi.org/project/flake8-pytest-style/)
- [`flake8-quotes`](https://pypi.org/project/flake8-quotes/)
- [`flake8-raise`](https://pypi.org/project/flake8-raise/)
- [`flake8-return`](https://pypi.org/project/flake8-return/)
- [`flake8-simplify`](https://pypi.org/project/flake8-simplify/) ([#998](https://github.com/charliermarsh/ruff/issues/998))
- [`flake8-super`](https://pypi.org/project/flake8-super/)
Expand Down Expand Up @@ -1736,6 +1746,7 @@ Today, Ruff can be used to replace Flake8 when used with any of the following pl
- [`flake8-print`](https://pypi.org/project/flake8-print/)
- [`flake8-pytest-style`](https://pypi.org/project/flake8-pytest-style/)
- [`flake8-quotes`](https://pypi.org/project/flake8-quotes/)
- [`flake8-raise`](https://pypi.org/project/flake8-raise/)
- [`flake8-return`](https://pypi.org/project/flake8-return/)
- [`flake8-simplify`](https://pypi.org/project/flake8-simplify/) ([#998](https://github.com/charliermarsh/ruff/issues/998))
- [`flake8-super`](https://pypi.org/project/flake8-super/)
Expand Down
15 changes: 15 additions & 0 deletions resources/test/fixtures/flake8_raise/RSE102.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
try:
y = 6 + "7"
except TypeError:
raise ValueError() # RSE102

try:
x = 1 / 0
except ZeroDivisionError:
raise

raise TypeError() # RSE102

raise AssertionError

raise AttributeError("test message")
4 changes: 4 additions & 0 deletions ruff.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1739,6 +1739,10 @@
"RET506",
"RET507",
"RET508",
"RSE",
"RSE1",
"RSE10",
"RSE102",
"RUF",
"RUF0",
"RUF00",
Expand Down
11 changes: 10 additions & 1 deletion src/checkers/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use crate::rules::{
flake8_2020, flake8_annotations, flake8_bandit, flake8_blind_except, flake8_boolean_trap,
flake8_bugbear, flake8_builtins, flake8_comprehensions, flake8_datetimez, flake8_debugger,
flake8_errmsg, flake8_implicit_str_concat, flake8_import_conventions, flake8_logging_format,
flake8_pie, flake8_print, flake8_pytest_style, flake8_return, flake8_simplify,
flake8_pie, flake8_print, flake8_pytest_style, flake8_raise, flake8_return, flake8_simplify,
flake8_tidy_imports, flake8_type_checking, flake8_unused_arguments, flake8_use_pathlib, mccabe,
pandas_vet, pep8_naming, pycodestyle, pydocstyle, pyflakes, pygrep_hooks, pylint, pyupgrade,
ruff, tryceratops,
Expand Down Expand Up @@ -1434,6 +1434,15 @@ where
tryceratops::rules::raise_vanilla_args(self, expr);
}
}
if self
.settings
.rules
.enabled(&Rule::UnnecessaryParenOnRaiseException)
{
if let Some(expr) = exc {
flake8_raise::rules::unnecessary_paren_on_raise_exception(self, expr);
}
}
}
StmtKind::AugAssign { target, .. } => {
self.handle_node_load(target);
Expand Down
5 changes: 5 additions & 0 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,8 @@ ruff_macros::define_rule_mapping!(
G101 => rules::flake8_logging_format::violations::LoggingExtraAttrClash,
G201 => rules::flake8_logging_format::violations::LoggingExcInfo,
G202 => rules::flake8_logging_format::violations::LoggingRedundantExcInfo,
// flake8-raise
RSE102 => rules::flake8_raise::rules::UnnecessaryParenOnRaiseException,
// ruff
RUF001 => violations::AmbiguousUnicodeCharacterString,
RUF002 => violations::AmbiguousUnicodeCharacterDocstring,
Expand Down Expand Up @@ -610,6 +612,9 @@ pub enum Linter {
/// [tryceratops](https://pypi.org/project/tryceratops/1.1.0/)
#[prefix = "TRY"]
Tryceratops,
/// [flake8-raise](https://pypi.org/project/flake8-raise/)
#[prefix = "RSE"]
Flake8Raise,
/// Ruff-specific rules
#[prefix = "RUF"]
Ruff,
Expand Down
28 changes: 28 additions & 0 deletions src/rules/flake8_raise/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//! Rules from [flake8-raise](https://pypi.org/project/flake8-raise/).
pub(crate) mod rules;

#[cfg(test)]
mod tests {
use std::convert::AsRef;
use std::path::Path;

use anyhow::Result;
use test_case::test_case;

use crate::linter::test_path;
use crate::registry::Rule;
use crate::{assert_yaml_snapshot, settings};

#[test_case(Rule::UnnecessaryParenOnRaiseException, Path::new("RSE102.py"); "RSE102")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.as_ref(), path.to_string_lossy());
let diagnostics = test_path(
Path::new("./resources/test/fixtures/flake8_raise")
.join(path)
.as_path(),
&settings::Settings::for_rule(rule_code),
)?;
assert_yaml_snapshot!(snapshot, diagnostics);
Ok(())
}
}
5 changes: 5 additions & 0 deletions src/rules/flake8_raise/rules/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub use unnecessary_paren_on_raise_exception::{
unnecessary_paren_on_raise_exception, UnnecessaryParenOnRaiseException,
};

mod unnecessary_paren_on_raise_exception;
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use ruff_macros::derive_message_formats;

use crate::ast::types::Range;
use crate::checkers::ast::Checker;
use crate::define_violation;
use crate::registry::Diagnostic;
use crate::violation::Violation;
use rustpython_ast::{Expr, ExprKind};

define_violation!(
pub struct UnnecessaryParenOnRaiseException;
);
impl Violation for UnnecessaryParenOnRaiseException {
#[derive_message_formats]
fn message(&self) -> String {
format!("Unnecessary parentheses on raised exception")
}
}

/// RSE102
pub fn unnecessary_paren_on_raise_exception(checker: &mut Checker, expr: &Expr) {
match &expr.node {
ExprKind::Call { args, keywords, .. } if args.is_empty() && keywords.is_empty() => {
checker.diagnostics.push(Diagnostic::new(
UnnecessaryParenOnRaiseException,
Range::from_located(expr),
));
}
_ => (),
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
source: src/rules/flake8_raise/mod.rs
expression: diagnostics
---
- kind:
UnnecessaryParenOnRaiseException: ~
location:
row: 4
column: 10
end_location:
row: 4
column: 22
fix: ~
parent: ~
- kind:
UnnecessaryParenOnRaiseException: ~
location:
row: 11
column: 6
end_location:
row: 11
column: 17
fix: ~
parent: ~

1 change: 1 addition & 0 deletions src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub mod flake8_pie;
pub mod flake8_print;
pub mod flake8_pytest_style;
pub mod flake8_quotes;
pub mod flake8_raise;
pub mod flake8_return;
pub mod flake8_simplify;
pub mod flake8_tidy_imports;
Expand Down

0 comments on commit 7c1a6bc

Please sign in to comment.